org.scalatest.time

SpanSugar

trait SpanSugar extends AnyRef

Trait providing four implicit conversions that allow you to specify spans of time by invoking "units" methods such as millis, seconds, and minutes on Int, Long, Float, and Double.

This trait enables you to specify a span of time in a clear, boilerplate-free way when you need to provide an instance of Span. This can be used, for example, with the failAfter method of trait Timeouts or the timeLimit field of trait TimeLimitedTests. It can also be used to specify timeouts when using traits Eventually, Futures, Waiter. Here are examples of each unit enabled by this trait:

Int Long Float Double
1 nanosecond 1L nanosecond 1.0F nanosecond 1.0 nanosecond
100 nanoseconds 100L nanoseconds 99.8F nanoseconds 99.8 nanoseconds
1 microsecond 1L microsecond 1.0F microsecond 1.0 microsecond
100 microseconds 100L microseconds 99.8F microseconds 99.8 microseconds
1 millisecond 1L millisecond 1.0F millisecond 1.0 millisecond
100 milliseconds 100L milliseconds 99.8F milliseconds 99.8 milliseconds
100 millis 100L millis 99.8F millis 99.8 millis
1 second 1L second 1.0F second 1.0 second
100 seconds 100L seconds 99.8F seconds 99.8 seconds
1 minute 1L minute 1.0F minute 1.0 minute
100 minutes 100L minutes 99.8F minutes 99.8 minutes
1 hour 1L hour 1.0F hour 1.0 hour
100 hours 100L hours 99.8F hours 99.8 hours
1 day 1L day 1.0F day 1.0 day
100 days 100L days 99.8F days 99.8 days

This trait is not the default way to specify Spans for two reasons. First, it adds four implicits, which would give the compiler more work to do and may conflict with other implicits the user has in scope. Instead, Span provides a clear, concise default way to specify time spans that requires no implicits. Here's an example:

Span(1, Second)

If you already have implicit conversions in scope that provide a similar syntax sugar for expression time spans, you can use that by providing an implicit conversion from the result of those expressions to Span. For example, here's how you might use the Duration class from Akka:

$ scala -cp scalatest-1.8-for-scala-2.9.jar:akka-actor-2.0.jar
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import org.scalatest.time._ import org.scalatest.time.time._

scala> import org.scalatest.concurrent.Eventually._ import org.scalatest.concurrent.Eventually._ * scala> import org.scalatest.matchers.ShouldMatchers._ import org.scalatest.matchers.ShouldMatchers._

scala> import akka.util.Duration import akka.util.Duration

scala> implicit def convertAkkaDuration(akkaDuration: Duration): Span = | if (akkaDuration.isFinite) Span(akkaDuration.toNanos, Nanoseconds) else Span.max convertAkkaDuration: (akkaDuration: akka.util.Duration)org.scalatest.time.Span

scala> eventually(timeout(Duration(100, "millis"))) { 1 + 1 should equal (3) } org.scalatest.TestFailedException: The code passed to eventually never returned normally. Attempted 6 times, sleeping 10 milliseconds between each attempt. Last failure message: 2 did not equal 3. at org.scalatest.concurrent.Eventually$class.tryTryAgain$1(Eventually.scala:346) at org.scalatest.concurrent.Eventually$class.eventually(Eventually.scala:356) at org.scalatest.concurrent.Eventually$.eventually(Eventually.scala:396) ...

scala> import akka.util.duration._ // Use Akka's syntax sugar for expressing time spans, not ScalaTest's import akka.util.duration._

scala> eventually(timeout(100 millis)) { 1 + 1 should equal (3) } org.scalatest.TestFailedException: The code passed to eventually never returned normally. Attempted 7 times, sleeping 10 milliseconds between each attempt. Last failure message: 2 did not equal 3. at org.scalatest.concurrent.Eventually$class.tryTryAgain$1(Eventually.scala:346) at org.scalatest.concurrent.Eventually$class.eventually(Eventually.scala:356) at org.scalatest.concurrent.Eventually$.eventually(Eventually.scala:396) ...

Linear Supertypes
AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Hide All
  2. Show all
  1. SpanSugar
  2. AnyRef
  3. Any
Visibility
  1. Public
  2. All

Type Members

  1. class FloatingGrainOfTime extends AnyRef

    Class containing methods that return a Span time value calculated from the Double value passed to the FloatingGrainOfTime constructor.

  2. class GrainOfTime extends AnyRef

    Class containing methods that return a Span time value calculated from the Long value passed to the GrainOfTime constructor.

Value Members

  1. def != (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  2. def != (arg0: Any): Boolean

    Attributes
    final
    Definition Classes
    Any
  3. def ## (): Int

    Attributes
    final
    Definition Classes
    AnyRef → Any
  4. def == (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  5. def == (arg0: Any): Boolean

    Attributes
    final
    Definition Classes
    Any
  6. def asInstanceOf [T0] : T0

    Attributes
    final
    Definition Classes
    Any
  7. def clone (): AnyRef

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  8. implicit def convertDoubleToGrainOfTime (d: Double): FloatingGrainOfTime

    Implicit conversion that adds time units methods to Doubles.

    Implicit conversion that adds time units methods to Doubles.

    returns

    a FloatingGrainOfTime wrapping the passed Double

    Attributes
    implicit
  9. implicit def convertFloatToGrainOfTime (f: Float): FloatingGrainOfTime

    Implicit conversion that adds time units methods to Floats.

    Implicit conversion that adds time units methods to Floats.

    returns

    a FloatingGrainOfTime wrapping the passed Float

    Attributes
    implicit
  10. implicit def convertIntToGrainOfTime (i: Int): GrainOfTime

    Implicit conversion that adds time units methods to Ints.

    Implicit conversion that adds time units methods to Ints.

    returns

    a GrainOfTime wrapping the passed Int

    Attributes
    implicit
  11. implicit def convertLongToGrainOfTime (i: Long): GrainOfTime

    Implicit conversion that adds time units methods to Longs.

    Implicit conversion that adds time units methods to Longs.

    returns

    a GrainOfTime wrapping the passed Long

    Attributes
    implicit
  12. def eq (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  13. def equals (arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  14. def finalize (): Unit

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  15. def getClass (): java.lang.Class[_]

    Attributes
    final
    Definition Classes
    AnyRef
  16. def hashCode (): Int

    Definition Classes
    AnyRef → Any
  17. def isInstanceOf [T0] : Boolean

    Attributes
    final
    Definition Classes
    Any
  18. def ne (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  19. def notify (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
  20. def notifyAll (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
  21. def synchronized [T0] (arg0: ⇒ T0): T0

    Attributes
    final
    Definition Classes
    AnyRef
  22. def toString (): String

    Definition Classes
    AnyRef → Any
  23. def wait (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()
  24. def wait (arg0: Long, arg1: Int): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()
  25. def wait (arg0: Long): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()

Inherited from AnyRef

Inherited from Any