-
Notifications
You must be signed in to change notification settings - Fork 4
/
expect.gleam
177 lines (164 loc) 路 4.34 KB
/
expect.gleam
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//// Assertions to be used in tests.
import exception
import gleam/float
import gleam/option.{type Option, None, Some}
import gleam/string
import startest/assertion_error.{AssertionError}
/// Asserts that the given value is equal to the expected value.
pub fn to_equal(actual: a, expected expected: a) -> Nil {
case actual == expected {
True -> Nil
False ->
AssertionError(
string.concat([
"Expected ",
string.inspect(actual),
" to equal ",
string.inspect(expected),
]),
string.inspect(actual),
string.inspect(expected),
)
|> assertion_error.raise
}
}
/// Asserts that the given value is not equal to the expected value.
pub fn to_not_equal(actual: a, expected expected: a) -> Nil {
case actual != expected {
True -> Nil
False ->
AssertionError(
string.concat([
"Expected ",
string.inspect(actual),
" to not equal ",
string.inspect(expected),
]),
string.inspect(actual),
string.inspect(expected),
)
|> assertion_error.raise
}
}
/// Asserts that the given value is `True`.
pub fn to_be_true(actual: Bool) -> Nil {
actual
|> to_equal(True)
}
/// Asserts that the given value is `False`.
pub fn to_be_false(actual: Bool) -> Nil {
actual
|> to_equal(False)
}
/// Asserts that the given value is `Ok(_)`.
pub fn to_be_ok(actual: Result(a, err)) -> a {
case actual {
Ok(value) -> value
Error(_) ->
AssertionError(
string.concat(["Expected ", string.inspect(actual), " to be Ok"]),
string.inspect(actual),
"Ok(_)",
)
|> assertion_error.raise
}
}
/// Asserts that the given value is `Error(_)`.
pub fn to_be_error(actual: Result(a, err)) -> err {
case actual {
Error(error) -> error
Ok(_) ->
AssertionError(
string.concat(["Expected ", string.inspect(actual), " to be Error"]),
string.inspect(actual),
"Error(_)",
)
|> assertion_error.raise
}
}
/// Asserts that the given value is `Some(_)`.
pub fn to_be_some(actual: Option(a)) -> a {
case actual {
Some(value) -> value
None ->
AssertionError(
string.concat(["Expected ", string.inspect(actual), " to be Some"]),
string.inspect(actual),
"Some(_)",
)
|> assertion_error.raise
}
}
/// Asserts that the given value is `None`.
pub fn to_be_none(actual: Option(a)) -> Nil {
case actual {
None -> Nil
Some(_) ->
AssertionError(
string.concat(["Expected ", string.inspect(actual), " to be None"]),
string.inspect(actual),
"None",
)
|> assertion_error.raise
}
}
/// Asserts that the given function throws an error.
pub fn to_throw(f: fn() -> a) -> Nil {
case exception.rescue(f) {
Error(_) -> Nil
Ok(value) ->
AssertionError(
string.concat(["Expected ", string.inspect(f), " to throw an error"]),
string.inspect(value),
string.inspect(Nil),
)
|> assertion_error.raise
}
}
/// Asserts that the given function does not throw an error.
pub fn to_not_throw(f: fn() -> a) -> a {
case exception.rescue(f) {
Ok(value) -> value
Error(exception) ->
AssertionError(
string.concat(["Expected ", string.inspect(f), " to not throw an error"]),
string.inspect(Nil),
string.inspect(exception),
)
|> assertion_error.raise
}
}
/// Asserts that the given `Float` is equal to the expected `Float` within the
/// provided tolerance.
pub fn to_loosely_equal(
actual: Float,
expected expected: Float,
tolerating tolerance: Float,
) -> Nil {
case float.loosely_equals(actual, expected, tolerance) {
True -> Nil
False ->
AssertionError(
string.concat([
"Expected ",
format_float(actual),
" to loosely equal ",
format_float(expected),
" with a tolerance of ",
format_float(tolerance),
]),
format_float(actual),
format_float(expected) <> " 卤 " <> format_float(tolerance),
)
|> assertion_error.raise
}
}
/// Formats a `Float` value as a `String` in a way that is consistent
/// across targets.
fn format_float(value: Float) -> String {
let repr = float.to_string(value)
case string.contains(repr, ".") {
True -> repr
False -> repr <> ".0"
}
}