/
Asserts.scala
65 lines (57 loc) · 2.21 KB
/
Asserts.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* scala-exercises - exercises-stdlib
* Copyright (C) 2015-2016 47 Degrees, LLC. <http://www.47deg.com>
*/
package stdlib
import org.scalatest._
/** @param name asserts
*/
object Asserts extends FlatSpec with Matchers with org.scalaexercises.definitions.Section {
/** ScalaTest makes three assertions available by default in any style trait. You can use:
*
* - `assert` for general assertions;
* - `assertResult` to differentiate expected from actual values;
* - `intercept` to ensure a bit of code throws an expected exception.
*
* In any Scala program, you can write assertions by invoking `assert` and passing in a `Boolean` expression:
*
* {{{
* val left = 2
* val right = 1
* assert(left == right)
* }}}
*
* If the passed expression is `true`, `assert` will return normally. If `false`,
* Scala's `assert` will complete abruptly with an `AssertionError`. This behavior is provided by
* the `assert` method defined in object `Predef`, whose members are implicitly imported into every Scala source file.
*
*
* ScalaTest provides a domain specific language (DSL) for expressing assertions in tests
* using the word `should`. ScalaTest matchers provides five different ways to check equality, each designed to address a different need. They are:
*
* {{{
* result should equal (3) // can customize equality
* result should === (3) // can customize equality and enforce type constraints
* result should be (3) // cannot customize equality, so fastest to compile
* result shouldEqual 3 // can customize equality, no parentheses required
* result shouldBe 3 // cannot customize equality, so fastest to compile, no parentheses required
* }}}
*
* Come on, your turn: true and false values can be compared with should matchers:
*/
def scalaTestAsserts(res0: Boolean) {
true should be(res0)
}
/** Booleans in asserts can test equality:
*/
def booleanAsserts(res0: Int) {
val v1 = 4
v1 shouldEqual res0
/** `shouldEqual` is an assertion. It is from ScalaTest, not from the Scala language. */
}
/** Sometimes we expect you to fill in the values:
*/
def valuesAsserts(res0: Int) {
assert(res0 == 1 + 1)
}
}