class Composifier[T] extends AnyRef
Class used via an implicit conversion that adds composeTwice, mapResult, and
mapArgs methods to functions that produce a Matcher.
- Source
- MatcherProducers.scala
- Alphabetic
- By Inheritance
- Composifier
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- def composeTwice[U](g: (U) => T): (U) => Matcher[U]
Produces a new “matcher producer” function of type
U => Matcher[U]from theT => Matcher[T](namedf) passed to theComposifierconstructor and the givenT => Utransformation function,g.Produces a new “matcher producer” function of type
U => Matcher[U]from theT => Matcher[T](namedf) passed to theComposifierconstructor and the givenT => Utransformation function,g.The result of
composeTwiceis the result of the following function composition expression:(f compose g) andThen (_ compose g)
You would use
composeTwiceif you want to create a new matcher producer from an existing one, by transforming both the left and right sides of the expression with the same transformation function. As an example, the expression “be > 7” produces aMatcher[Int]:scala> import org.scalatest._ import org.scalatest._ scala> import Matchers._ import Matchers._ scala> val beGreaterThanSeven = be > 7 beGreaterThanSeven: org.scalatest.matchers.Matcher[Int] = be > 7
Given this
Matcher[Int], you can now use it in ashouldexpression like this:scala> 8 should beGreaterThanSeven scala> 6 should beGreaterThanSeven org.scalatest.exceptions.TestFailedException: 6 was not greater than 7 ...
You can create a more general “matcher producer” function like this:
scala> val beGreaterThan = { (i: Int) => be > i } beGreaterThan: Int => org.scalatest.matchers.Matcher[Int] = <function1> scala> 8 should beGreaterThan (7) scala> 8 should beGreaterThan (9) org.scalatest.exceptions.TestFailedException: 8 was not greater than 9Given
beGreaterThanmatcher producer function, you can create matcher producer function that takes aStringand produces aMatcher[String]given a function fromInt => StringusingcomposeTwice:scala> val stringToInt = { (s: String) => s.toInt } stringToInt: String => Int = <function1> scala> val beAsIntsGreaterThan = beGreaterThan composeTwice stringToInt beAsIntsGreaterThan: String => org.scalatest.matchers.Matcher[String] = <function1> scala> "7" should beAsIntsGreaterThan ("6") scala> "7" should beAsIntsGreaterThan ("8") org.scalatest.exceptions.TestFailedException: 7 was not greater than 8 ...The
composeTwicemethod is just a shorthand for this function composition expression:scala> val beAsIntsGreaterThan = (beGreaterThan compose stringToInt) andThen (_ compose stringToInt) beAsIntsGreaterThan: String => org.scalatest.matchers.Matcher[String] = <function1> scala> "7" should beAsIntsGreaterThan ("6") scala> "7" should beAsIntsGreaterThan ("8") org.scalatest.exceptions.TestFailedException: 7 was not greater than 8The first part of that expression,
beGreaterThancomposestringToInt, gives you a new matcher producer function that given aStringwill produce aMatcher[Int]:scala> val beAsIntGreaterThan = beGreaterThan compose stringToInt beAsIntGreaterThan: String => org.scalatest.matchers.Matcher[Int] = <function1>
This
composemethod is inherited fromFunction1: on anyFunction1,(fcomposeg)(x)meansf(g(x)). You can use this matcher producer like this:scala> 7 should beAsIntGreaterThan ("6") scala> 7 should beAsIntGreaterThan ("8") org.scalatest.exceptions.TestFailedException: 7 was not greater than 8To get a matcher producer that will allow you to put a string on the right-hand-side, you'll need to transform the
String=>Matcher[Int]to aString=>Matcher[String]. To accomplish this you can first just apply the function to get aMatcher[Int], like this:scala> val beGreaterThanEight = beAsIntGreaterThan ("8") beGreaterThanEight: org.scalatest.matchers.Matcher[Int] = be > 8 scala> 9 should beGreaterThanEight scala> 7 should beGreaterThanEight org.scalatest.exceptions.TestFailedException: 7 was not greater than 8To transform
beGreaterThanEight, aMatcher[Int], to aMatcher[String], you can again usecompose. A ScalaTestMatcher[T]is a Scala function typeT=>MatchResult. To get aMatcher[String]therefore, just callcomposeon theMatcher[Int]and pass in a function fromString=>Int:scala> val beAsIntGreaterThanEight = beGreaterThanEight compose stringToInt beAsIntGreaterThanEight: org.scalatest.matchers.Matcher[String] = <function1>
After the second call to
compose, therefore, you have what you want:scala> "9" should beAsIntGreaterThanEight scala> "7" should beAsIntGreaterThanEight org.scalatest.exceptions.TestFailedException: 7 was not greater than 8
So in summary, what the result of
(beGreaterThancomposestringToInt)andThen(_composestringToInt)will do once it is applied to a (right-hand-side)String, is:- Transform
beGreaterThanfrom anInt=>Matcher[Int]to aString=>Matcher[Int]with the firstcompose - Apply the given (right-hand-side)
Stringto that to get aMatcher[Int](the first part ofandThen's behavior) - Pass the resulting
Matcher[Int]to the given function,_composestringToInt, which will transform theMatcher[Int]to aMatcher[String](the second part of theandThenbehavior).
- Transform
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def mapArgs(prettify: (Any) => String): (T) => Matcher[T]
Returns a function that given a
Twill return aMatcher[T]that will produce theMatchResultproduced byf(passed to theComposifierconstructor) with arguments transformed by the givenprettifyfunction.Returns a function that given a
Twill return aMatcher[T]that will produce theMatchResultproduced byf(passed to theComposifierconstructor) with arguments transformed by the givenprettifyfunction.- prettify
a function with which to transform the arguments of error messages.
- returns
a new
Matcherproducer function that produces prettified error messages
- def mapResult(prettify: (MatchResult) => MatchResult): (T) => Matcher[T]
Returns a function that given a
Twill return aMatcher[T]that will produce theMatchResultproduced byf(passed to theComposifierconstructor) transformed by the givenprettifyfunction.Returns a function that given a
Twill return aMatcher[T]that will produce theMatchResultproduced byf(passed to theComposifierconstructor) transformed by the givenprettifyfunction.- prettify
a function with which to transform
MatchResults.- returns
a new
Matcherproducer function that produces prettified error messages
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()