Packages

final class Expect[+R] extends LazyLogging

Expect allows you to invoke CLIs and ensure they return what you expect. You can describe a cli interaction which will be executed when run is invoked.

Each interaction is described inside an ExpectBlock using Whens. Each When states under which circumstances the corresponding actions are to be executed. For example:

import work.martins.simon.core._
import scala.concurrent.ExecutionContext.Implicits.global

val e = new Expect("bc -i", defaultValue = 5)(
  ExpectBlock(
    StringWhen("For details type `warranty'.")(
      Sendln("1 + 2")
    )
  ),
  ExpectBlock(
    RegexWhen("""\n(\d+)\n""".r)(
      SendlnWithRegex { m =>
        val previousAnswer = m.group(1)
        println(s"Got $$previousAnswer")
        s"$$previousAnswer + 3"
      }
    )
  ),
  ExpectBlock(
    RegexWhen("""\n(\d+)\n""".r)(
      ReturningWithRegex(_.group(1).toInt)
    )
  )
)
e.run() //Returns 6 inside a Future[Int]

We are executing the command bc -i which is a cli calculator. bc, when it starts, writes to the terminal (more precisely to the standard out): "For details type warranty'." The expect we created reacts to it and sends to the terminal (more precisely to the standard in) the string "1 + 2". However it only does so when the expect in ran, until then it does nothing, or in other words, it only saves that it has to send "1 + 2" when the string "For details type warranty'." appears on the StdOut.

If this interaction is successful it then proceeds to the next one defined by the next ExpectBlock. This interaction states that when bc outputs text that matches the regex """\n(\d+)\n""" the expect should react by sending the string "3 + 3" where the first 3 is obtained by inspecting the regex match and extracting the first group.

Finally the last interaction is very similar to the second one, with the only difference being the matched digit is not used to send more text to bc but rather to set the value the expect will return in the end of the execution.

If you find the syntax used to create the Expect too verbose you should look at work.martins.simon.expect.fluent.Expect or work.martins.simon.expect.dsl.Expect which provide a more terse syntax, especially dsl.Expect which provides a syntax very similar to Tcl Expect.

R

the type this Expect returns.

Source
Expect.scala
Linear Supertypes
LazyLogging, AnyRef, Any
Type Hierarchy
Ordering
  1. Grouped
  2. Alphabetic
  3. By Inheritance
Inherited
  1. Expect
  2. LazyLogging
  3. AnyRef
  4. Any
Implicitly
  1. by any2stringadd
  2. by StringFormat
  3. by Ensuring
  4. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Expect(command: String, defaultValue: R)(expectBlocks: ExpectBlock[R]*)
  2. new Expect(command: String, defaultValue: R, config: Config)(expectBlocks: ExpectBlock[R]*)
  3. new Expect(command: String, defaultValue: R, settings: Settings)(expectBlocks: ExpectBlock[R]*)
  4. new Expect(command: Seq[String], defaultValue: R, config: Config)(expects: ExpectBlock[R]*)
  5. new Expect(command: Seq[String], defaultValue: R, settings: Settings = new Settings())(expectBlocks: ExpectBlock[R]*)

    command

    the command this Expect will execute.

    defaultValue

    the value that will be returned if no Returning action is executed.

    settings

    the settings that parameterize the execution of this Expect.

    expectBlocks

    the ExpectBlocks that describe the interactions this Expect will execute.

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. def +(other: String): String
    Implicit
    This member is added by an implicit conversion from Expect[R] to any2stringadd[Expect[R]] performed by method any2stringadd in scala.Predef.
    Definition Classes
    any2stringadd
  4. def ->[B](y: B): (Expect[R], B)
    Implicit
    This member is added by an implicit conversion from Expect[R] to ArrowAssoc[Expect[R]] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  5. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  6. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  7. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. def collect[T](pf: PartialFunction[R, T]): Expect[T]

    Creates a new Expect by mapping the result of the current Expect, if the given partial function is defined at that value.

    Creates a new Expect by mapping the result of the current Expect, if the given partial function is defined at that value.

    If the current Expect contains a value for which the partial function is defined, the new Expect will also hold that value. Otherwise, the resulting Expect will fail with a NoSuchElementException.

    T

    the type of the returned Expect

    pf

    the PartialFunction to apply to the result of this Expect

    returns

    an Expect holding the result of application of the PartialFunction or a NoSuchElementException

  9. val command: Seq[String]
  10. val defaultValue: R
  11. def ensuring(cond: (Expect[R]) ⇒ Boolean, msg: ⇒ Any): Expect[R]
    Implicit
    This member is added by an implicit conversion from Expect[R] to Ensuring[Expect[R]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  12. def ensuring(cond: (Expect[R]) ⇒ Boolean): Expect[R]
    Implicit
    This member is added by an implicit conversion from Expect[R] to Ensuring[Expect[R]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  13. def ensuring(cond: Boolean, msg: ⇒ Any): Expect[R]
    Implicit
    This member is added by an implicit conversion from Expect[R] to Ensuring[Expect[R]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  14. def ensuring(cond: Boolean): Expect[R]
    Implicit
    This member is added by an implicit conversion from Expect[R] to Ensuring[Expect[R]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  15. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  16. def equals(other: Any): Boolean

    Returns whether other is an Expect with the same command, the same defaultValue, the same settings and the same expects as this Expect.

    Returns whether other is an Expect with the same command, the same defaultValue, the same settings and the same expects as this Expect.

    In the cases that expects contains an Action with a function, eg. Returning, this method will return false, because equality is not defined for functions.

    The method structurallyEqual can be used to test that two expects contain the same structure.

    other

    the other Expect to compare this Expect to.

    Definition Classes
    Expect → AnyRef → Any
  17. val expectBlocks: ExpectBlock[R]*
  18. def filter(p: (R) ⇒ Boolean): Expect[R]

    Creates a new Expect by filtering its result with a predicate.

    Creates a new Expect by filtering its result with a predicate.

    If the current Expect result satisfies the predicate, the new Expect will also hold that result. Otherwise, the resulting Expect will fail with a NoSuchElementException.

    p

    the predicate to apply to the result of this Expect

  19. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  20. def flatMap[T](f: (R) ⇒ Expect[T]): Expect[T]

    Creates a new Expect by applying a function to the returned result of this Expect, and returns the result of the function as the new Expect.

    Creates a new Expect by applying a function to the returned result of this Expect, and returns the result of the function as the new Expect.

    T

    the type of the returned Expect

    f

    the function which will be applied to the returned result of this Expect

    returns

    the Expect returned as the result of the application of the function f

  21. def flatten[T](implicit ev: <:<[R, Expect[T]]): Expect[T]

    Creates a new Expect with one level of nesting flattened, this method is equivalent to flatMap(identity).

    Creates a new Expect with one level of nesting flattened, this method is equivalent to flatMap(identity).

    T

    the type of the returned Expect

    returns

    an Expect with one level of nesting flattened

  22. def formatted(fmtstr: String): String
    Implicit
    This member is added by an implicit conversion from Expect[R] to StringFormat[Expect[R]] performed by method StringFormat in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @inline()
  23. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
  24. def hashCode(): Int
    Definition Classes
    Expect → AnyRef → Any
  25. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  26. lazy val logger: Logger
    Attributes
    protected
    Definition Classes
    LazyLogging
  27. def map[T](f: (R) ⇒ T): Expect[T]

    Creates a new Expect by applying a function to the returned result of this Expect.

    Creates a new Expect by applying a function to the returned result of this Expect.

    T

    the type of the returned Expect

    f

    the function which will be applied to the returned result of this Expect

    returns

    an Expect which will return the result of the application of the function f

  28. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  29. final def notify(): Unit
    Definition Classes
    AnyRef
  30. final def notifyAll(): Unit
    Definition Classes
    AnyRef
  31. def run(process: RichProcess)(implicit ex: ExecutionContext): Future[R]
  32. def run(settings: Settings)(implicit ex: ExecutionContext): Future[R]
  33. def run(timeout: FiniteDuration = settings.timeout, charset: Charset = settings.charset)(implicit ex: ExecutionContext): Future[R]
  34. val settings: Settings
  35. def structurallyEquals[RR >: R](other: Expect[RR]): Boolean

  36. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  37. def toString(): String
    Definition Classes
    Expect → AnyRef → Any
  38. def transform[T](flatMapPF: PartialFunction[R, Expect[T]], mapPF: PartialFunction[R, T]): Expect[T]

    Transform this Expect result using the following strategy:

    Transform this Expect result using the following strategy:

    • if flatMapPF isDefinedAt for this expect result then the result is flatMapped using flatMapPF.
    • otherwise, if mapPF isDefinedAt for this expect result then the result is mapped using mapPF.
    • otherwise a NoSuchElementException is thrown where the result would be expected.

    This function is very useful when we need to flatMap this Expect for some values of its result type and map this Expect for some other values of its result type.

    To ensure you don't get NoSuchElementException you should take special care in ensuring:

    domain(flatMapPF) ∪ domain(mapPF) == domain(R)

    Remember that in the domain of R the defaultValue is also included.

    T

    the type of the returned Expect

    flatMapPF

    the function that will be applied when a flatMap is needed

    mapPF

    the function that will be applied when a map is needed

    returns

    a new Expect whose result is either flatMapped or mapped according to whether flatMapPF or mapPF is defined for the given result

    Example:
    1. def countFilesInFolder(folder: String): Expect[Either[String, Int]] = {
        val e = new Expect(s"ls -1 $$folder", Left("unknownError"): Either[String, Int])(
          ExpectBlock(
            StringWhen("access denied")(
              Returning(Left("Access denied"))
            ),
            RegexWhen("(?s)(.*)".r)(
              ReturningWithRegex(_.group(1).split("\n").length)
            )
          )
        )
        e
      }
      
      def ensureFolderIsEmpty(folder: String): Expect[Either[String, Unit]] = {
        countFilesInFolder(folder).transform({
          case Right(numberOfFiles) =>
            Expect(s"rm -r $$folder", Left("unknownError"): Either[String, Unit])(
              ExpectBlock(
                StringWhen("access denied")(
                  Returning(Left("Access denied"))
                ),
                EndOfFileWhen(
                  Returning(())
                )
              )
            )
        }, {
          case Left(l) => Left(l)
          case Right(numberOfFiles) if numberOfFiles == 0 => Right(())
        })
      }
  39. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  40. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  41. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  42. def withFilter(p: (R) ⇒ Boolean): Expect[R]

    Used by for-comprehensions.

  43. def zip[T](that: Expect[T]): Expect[(R, T)]

    Zips the results of this and that Expect, and creates a new Expect holding the tuple of their results.

    Zips the results of this and that Expect, and creates a new Expect holding the tuple of their results.

    T

    the type of the returned Expect

    that

    the other Expect

    returns

    an Expect with the results of both Expects

  44. def zipWith[T, U](that: Expect[T])(f: (R, T) ⇒ U): Expect[U]

    Zips the results of this and that Expect using a function f, and creates a new Expect holding the result.

    Zips the results of this and that Expect using a function f, and creates a new Expect holding the result.

    T

    the type of the other Expect

    U

    the type of the resulting Expect

    that

    the other Expect

    f

    the function to apply to the results of this and that

    returns

    an Expect with the result of the application of f to the results of this and that

  45. def [B](y: B): (Expect[R], B)
    Implicit
    This member is added by an implicit conversion from Expect[R] to ArrowAssoc[Expect[R]] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc

Inherited from LazyLogging

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion any2stringadd from Expect[R] to any2stringadd[Expect[R]]

Inherited by implicit conversion StringFormat from Expect[R] to StringFormat[Expect[R]]

Inherited by implicit conversion Ensuring from Expect[R] to Ensuring[Expect[R]]

Inherited by implicit conversion ArrowAssoc from Expect[R] to ArrowAssoc[Expect[R]]

Transformations

Ungrouped