-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAboutOptions.scala
executable file
·63 lines (50 loc) · 1.68 KB
/
AboutOptions.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
package org.functionalkoans.forscala
import support.KoanSuite
class AboutOptions extends KoanSuite {
koan("Option can have one of two values - Some or None") {
val someValue: Option[String] = Some("I am wrapped in something")
someValue.get should be("I am wrapped in something")
val nullValue: Option[String] = None
nullValue should be(None)
}
def maybeItWillReturnSomething(flag: Boolean): Option[String] = {
if (flag) Some("Found value") else None
}
koan("Represent null with None because null is a bad idea") {
val value1 = maybeItWillReturnSomething(true)
val value2 = maybeItWillReturnSomething(false)
value1.get should be("Found value")
intercept[java.util.NoSuchElementException] {
value2.get
}
}
koan("Provide a default value for None") {
val value1 = maybeItWillReturnSomething(true)
val value2 = maybeItWillReturnSomething(false)
value1 getOrElse "No value" should be("Found value")
value2 getOrElse "No value" should be("No value")
value2 getOrElse {
"default function"
} should be("default function")
}
koan("checking whether option has value") {
val value1 = maybeItWillReturnSomething(true)
val value2 = maybeItWillReturnSomething(false)
value1.isEmpty should be(false)
value2.isEmpty should be(true)
}
koan("Option can also be used with pattern matching") {
val someValue: Option[Double] = Some(20.0)
val value = someValue match {
case Some(v) => v
case None => 0.0
}
value should be(20.0)
val noValue: Option[Double] = None
val value1 = noValue match {
case Some(v) => v
case None => 0.0
}
value1 should be(0.0)
}
}