Package

org.scalatest

diagrams

Permalink

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
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. diagrams
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. trait Diagrams extends Assertions

    Permalink

    Sub-trait of Assertions that overrides assert and assume methods to include a diagram showing the values of expression in the error message when the assertion or assumption fails.

    Sub-trait of Assertions that overrides assert and assume methods 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 assert or assume spans more than one line, Diagrams falls back to the default style of error message, since drawing a diagram would be difficult. Here's an example showing how Diagrams will 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 Diagrams was inspired by Peter Niederwieser's work in Spock and Expecty.

Value Members

  1. object Diagrams extends Diagrams

    Permalink

    Companion object that facilitates the importing of Diagrams members as an alternative to mixing it in.

    Companion object that facilitates the importing of Diagrams members as an alternative to mixing it in. One use case is to import Diagrams members 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...
    

Inherited from AnyRef

Inherited from Any

Ungrouped