package diagrams
A single traits and companion object that provides a assertions that draw code diagrams on failure.
This package is released as the scalatest-diagrams module.
- Source
- package.scala
- Alphabetic
- By Inheritance
- diagrams
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- trait DiagrammedExpr[T] extends AnyRef
A trait that represent an expression recorded by
DiagrammedExprMacro, which includes the following members:A trait that represent an expression recorded by
DiagrammedExprMacro, which includes the following members:- a boolean value
- an anchor that records the position of this expression
- anchor values of this expression (including sub-expressions)
DiagrammedExpris used by code generated fromDiagrammedAssertionsMacro, it needs to be public so that the generated code can be compiled. It is expected that ScalaTest users would ever need to useDiagrammedExprdirectly. - trait Diagrams extends Assertions
Sub-trait of
Assertionsthat overridesassertandassumemethods to include a diagram showing the values of expression in the error message when the assertion or assumption fails.Sub-trait of
Assertionsthat overridesassertandassumemethods to include a diagram showing the values of expression in the error message when the assertion or assumption fails.Here are some examples:
scala> import org.scalatest.diagrams.Diagrams._ import org.scalatest.diagrams.Diagrams._ scala> assert(a == b || c >= d) org.scalatest.exceptions.TestFailedException: assert(a == b || c >= d) | | | | | | | 1 | 2 | 3 | 4 | | false | false false at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:422) ... scala> assert(xs.exists(_ == 4)) org.scalatest.exceptions.TestFailedException: assert(xs.exists(_ == 4)) | | | false List(1, 2, 3) at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:422) ... scala> assert("hello".startsWith("h") && "goodbye".endsWith("y")) org.scalatest.exceptions.TestFailedException: assert("hello".startsWith("h") && "goodbye".endsWith("y")) | | | | | | | "hello" true "h" | "goodbye" false "y" false at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:422) ... scala> assert(num.isInstanceOf[Int]) org.scalatest.exceptions.TestFailedException: assert(num.isInstanceOf[Int]) | | 1.0 false at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:422) ... scala> assert(Some(2).isEmpty) org.scalatest.exceptions.TestFailedException: assert(Some(2).isEmpty) | | | | 2 false Some(2) at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:422) ... scala> assert(None.isDefined) org.scalatest.exceptions.TestFailedException: assert(None.isDefined) | | None false at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:422) ... scala> assert(xs.exists(i => i > 10)) org.scalatest.exceptions.TestFailedException: assert(xs.exists(i => i > 10)) | | | false List(1, 2, 3) at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:422) ...If the expression passed to
assertorassumespans more than one line,Diagramsfalls back to the default style of error message, since drawing a diagram would be difficult. Here's an example showing howDiagramswill treat a multi-line assertion (i.e., you don't get a diagram):scala> assert("hello".startsWith("h") && | "goodbye".endsWith("y")) org.scalatest.exceptions.TestFailedException: "hello" started with "h", but "goodbye" did not end with "y" at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:422) ...Also, since an expression diagram essentially represents multi-line ascii art, if a clue string is provided, it appears above the diagram, not after it. It will often also show up in the diagram:
scala> assert(None.isDefined, "Don't do this at home") org.scalatest.exceptions.TestFailedException: Don't do this at home assert(None.isDefined, "Don't do this at home") | | None false at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:422) ... scala> assert(None.isDefined, | "Don't do this at home") org.scalatest.exceptions.TestFailedException: Don't do this at home assert(None.isDefined, | | None false at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:422) ...Trait
Diagramswas inspired by Peter Niederwieser's work in Spock and Expecty.
Value Members
- object DiagrammedExpr
DiagrammedExprcompanion object that provides factory methods to create different sub types ofDiagrammedExprDiagrammedExprcompanion object that provides factory methods to create different sub types ofDiagrammedExprDiagrammedExpris used by code generated fromDiagrammedAssertionsMacro, it needs to be public so that the generated code can be compiled. It is expected that ScalaTest users would ever need to useDiagrammedExprdirectly. - object Diagrams extends Diagrams
Companion object that facilitates the importing of
Diagramsmembers as an alternative to mixing it in.Companion object that facilitates the importing of
Diagramsmembers as an alternative to mixing it in. One use case is to importDiagramsmembers so you can use them in the Scala interpreter:$scala -classpath scalatest.jar Welcome to Scala version 2.10.4.final (Java HotSpot(TM) Client VM, Java 1.6.0_45). Type in expressions to have them evaluated. Type :help for more information. scala> import org.scalatest.Assertions._ import org.scalatest.Assertions._ scala> assert(1 === 2) org.scalatest.exceptions.TestFailedException: assert(1 === 2) | | | 1 | 2 false at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:422) at org.scalatest.Diagrams$.newAssertionFailedException(Diagrams.scala:249) at org.scalatest.Diagrams$DiagramsHelper.macroAssert(Diagrams.scala:111) at .<init>(<console>:20) at .<clinit>(<console>) at .<init>(<console>:7) at .<clinit>(<console>) at $print(<console>) at sun.reflect.NativeMethodAccessorImpl.invoke...