blob: 9dece8d6d87636298478388167cdf998e504869f (
plain) (
blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
import scala.util.{Try, Success, Failure}
// tests the basic combinators on Try
trait TryStandard {
def testForeachSuccess(): Unit = {
val t = Success(1)
var res = 0
t.foreach(x => res = x * 10)
assert(res == 10)
}
def testForeachFailure(): Unit = {
val t = Failure(new Exception("foo"))
t.foreach(x => assert(false))
}
def testFlatMapSuccess(): Unit = {
val t = Success(1)
val n = t.flatMap(x => Try(x * 10))
assert(n.get == 10)
}
def testFlatMapFailure(): Unit = {
val t = Failure(new Exception("foo"))
val n = t.flatMap{ x => assert(false); Try() }
}
def testMapSuccess(): Unit = {
val t = Success(1)
val n = t.map(x => x * 10)
assert(n.get == 10)
}
def testMapFailure(): Unit = {
val t = Failure(new Exception("foo"))
val n = t.map(x => assert(false))
}
def testFilterSuccessTrue(): Unit = {
val t = Success(1)
val n = t.filter(x => x > 0)
assert(n.get == 1)
}
def testFilterSuccessFalse(): Unit = {
val t = Success(1)
val n = t.filter(x => x < 0)
n match {
case Success(v) => assert(false)
case Failure(e: NoSuchElementException) => assert(true)
}
}
def testFilterFailure(): Unit = {
val t = Failure(new Exception("foo"))
val n = t.filter{ x => assert(false); true }
}
def testRescueSuccess(): Unit = {
val t = Success(1)
t.recoverWith{ case x => assert(false); Try() }
}
def testRescueFailure(): Unit = {
val t = Failure(new Exception("foo"))
val n = t.recoverWith{ case x => Try(1) }
assert(n.get == 1)
}
def testRecoverSuccess(): Unit = {
val t = Success(1)
t.recover{ case x => assert(false); 99 }
}
def testRecoverFailure(): Unit = {
val t = Failure(new Exception("foo"))
val n = t.recover{ case x => 1 }
assert(n.get == 1)
}
def testFlattenSuccess(): Unit = {
val f = Failure(new Exception("foo"))
val t = Success(f)
assert(t.flatten == f)
}
def testFailedSuccess(): Unit = {
val t = Success(1)
val n = t.failed
n match {
case Failure(e: UnsupportedOperationException) => assert(true)
case _ => assert(false)
}
}
def testFailedFailure(): Unit = {
val t = Failure(new Exception("foo"))
val n = t.failed
n match {
case Success(e: Exception) => assert(true)
case _ => assert(false)
}
}
testForeachSuccess()
testForeachFailure()
testFlatMapSuccess()
testFlatMapFailure()
testMapSuccess()
testMapFailure()
testFilterSuccessTrue()
testFilterSuccessFalse()
testFilterFailure()
testRescueSuccess()
testRescueFailure()
testRecoverSuccess()
testRecoverFailure()
testFlattenSuccess()
testFailedSuccess()
testFailedFailure()
}
object Test
extends App
with TryStandard {
System.exit(0)
}
|