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
|
import expect
import result
pub fn is_ok_test() {
result:is_ok(Ok(1)) |> expect:true
result:is_ok(Error(1)) |> expect:false
}
pub fn is_error_test() {
result:is_error(Ok(1))
|> expect:false
result:is_error(Error(1))
|> expect:true
}
pub fn map_test() {
Ok(1)
|> result:map(_, fn(x) { x + 1 })
|> expect:equal(_, Ok(2))
Ok(1)
|> result:map(_, fn(_) { "2" })
|> expect:equal(_, Ok("2"))
Error(1)
|> result:map(_, fn(x) { x + 1 })
|> expect:equal(_, Error(1))
}
pub fn map_error_test() {
Ok(1)
|> result:map_error(_, fn(x) { x + 1 })
|> expect:equal(_, Ok(1))
Error(1)
|> result:map_error(_, fn(x) { x + 1 })
|> expect:equal(_, Error(2))
}
pub fn flatten_test() {
Ok(Ok(1))
|> result:flatten
|> expect:equal(_, Ok(1))
Ok(Error(1))
|> result:flatten
|> expect:equal(_, Error(1))
Error(1)
|> result:flatten
|> expect:equal(_, Error(1))
Error(Error(1))
|> result:flatten
|> expect:equal(_, Error(Error(1)))
}
pub fn then_test() {
Error(1)
|> result:then(_, fn(x) { Ok(x + 1) })
|> expect:equal(_, Error(1))
Ok(1)
|> result:then(_, fn(x) { Ok(x + 1) })
|> expect:equal(_, Ok(2))
Ok(1)
|> result:then(_, fn(_) { Ok("type change") })
|> expect:equal(_, Ok("type change"))
Ok(1)
|> result:then(_, fn(_) { Error(1) })
|> expect:equal(_, Error(1))
}
pub fn unwrap_test() {
Ok(1)
|> result:unwrap(_, 50)
|> expect:equal(_, 1)
Error("nope")
|> result:unwrap(_, 50)
|> expect:equal(_, 50)
}
|