How does the ScalaTest syntax work?

I did some programming in Scala, and I know that, for example,

xs map f

is the same as

xs.map(f)

but I don’t know how to generalize this syntax to something like ScalaTest syntax, for example

it should "throw NoSuchElementException if an empty stack is popped" in {
  val emptyStack = new Stack[String]
  evaluating { emptyStack.pop() } should produce [NoSuchElementException]
}

I am mainly interested in things that look like verbose constructs, namely should produce. This is neat.

+5
source share
2 answers

This type of syntax is method calls in operator notation, but they carry over to more than three tokens. As you mentioned:

xs map f

funds:

xs.map(f)

But you can go further and say:

xs map f map g

What means:

xs.map(f).map(g)

For example, in ScalaTest mappers you can say:

result should not be null

This compiles using the compiler:

result.should(not).be(null)

It:

it should "throw an exception" in { ... }

falls into:

it.should("throw an exception").in { ... }

- ( ) in, . . .

, , :

evaluating { ... } should produce [IllegalArgumentException]

:

evaluating {... }, . , "", . , should . , should , , evaluating. should , produce. produce , , [IllegalArgumentException]. , Scala " " . "Manifest" produce java.lang.Class IllegalArgumentException. , should, , , evaluating, java.lang.Class , . , , try, , . , , should TestFailedException. should .

, , :

(evaluating { ... }).should(produce[IllegalArgumentException] (compilerSuppliedManifest))

, , , . , , - , , - . Scala -Xprint:typer Scala, , . , , , , .

+12

, - it should it.should, , ( ), it. !

ItWord, should, BehaveWord. ShouldMatchers trait.

ScalaTest , .

+2

Source: https://habr.com/ru/post/1719953/


All Articles