-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpect.pike
69 lines (55 loc) · 1.39 KB
/
Expect.pike
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
#define THROW_EXPECT_ERR(E1, E2) throw(.Error.ExpectError(E1, E2))
public mixed expression;
protected void create(mixed expression) {
this::expression = expression;
}
public void to_equal(mixed expectation) {
if (!equal(expectation, expression)) {
THROW_EXPECT_ERR(this, expectation);
}
}
public void to_be(mixed expectation) {
if (expression != expectation) {
THROW_EXPECT_ERR(this, expectation);
}
}
public void to_be_truthy() {
if (!expression) {
THROW_EXPECT_ERR(this, "defined");
}
}
public void to_be_falsy() {
if (expression) {
THROW_EXPECT_ERR(this, "undefined");
}
}
public void to_have_been_called() {
if (object_program(expression) != .Fn) {
error("Expression is not a Pest Fn function\n");
}
if (expression->count == 0) {
THROW_EXPECT_ERR(0, "> 0");
}
}
public void to_have_been_called_n_times(int n) {
if (object_program(expression) != .Fn) {
error("Expression is not a Pest Fn function\n");
}
if (expression->count != n) {
THROW_EXPECT_ERR(expression->count, n);
}
}
public void to_throw(string|void message) {
if (!functionp(expression)) {
error("Expression must be a function when calling to_throw()\n");
}
mixed err = catch { expression(); };
if (!err) {
THROW_EXPECT_ERR(this, "to throw");
}
if (message && arrayp(err)) {
if (message != err[0]) {
THROW_EXPECT_ERR(err[0], message);
}
}
}