-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunner.pike
147 lines (120 loc) · 3.08 KB
/
Runner.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
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
public typedef array(object(.Describer)|object(.Test)) RunQueue;
public string file;
public program test_suite_program;
public object test_suite_object;
protected bool is_compiled_ok = false;
protected .Describer current_describer;
protected RunQueue run_queue = ({});
protected void create(string file, program test_suite_program) {
this::file = file;
this::test_suite_program = test_suite_program;
if (mixed err = catch(test_suite_object = test_suite_program())) {
werror("Failed instantiating %q\n", file);
werror("Why no backtrace?: %O\n", err->backtrace());
} else if (!test_suite_object->main) {
werror("Missing main() function in %q\n", file);
} else {
is_compiled_ok = true;
}
}
public void collect_tests() {
if (!is_compiled_ok) {
return;
}
mixed err = catch(test_suite_object->main());
if (err) {
werror("collect_test failed: %s => %O\n", describe_backtrace(err), err);
}
}
public void add_test(string description, function executor, void|bool skip) {
.Test t = .Test(description, executor);
if (skip) {
t->skip = true;
}
if (current_describer) {
current_describer->add_test(t);
} else {
run_queue += ({ t });
}
}
public void add_describer(
string description,
function executor,
void|bool skip
) {
.Describer d = .Describer(description, executor);
if (skip) {
d->skip = true;
}
current_describer = d;
d->run();
run_queue += ({ d });
current_describer = 0;
}
public int get_number_of_tests() {
int n_tests = 0;
map(run_queue, lambda (object t) {
if (object_program(t) == .Test) {
n_tests += 1;
} else {
n_tests += sizeof(t->tests);
}
});
return n_tests;
}
public void execute(.GlobArg|void test_glob) {
foreach (run_queue, object obj) {
if (is_describer(obj)) {
foreach (obj->tests, .Test t) {
if (!test_glob || glob(test_glob, t->description)) {
t->run();
}
}
} else {
if (!test_glob || glob(test_glob, obj->description)) {
obj->run();
}
}
}
}
public mapping report() {
array(.Test) tests = ({});
foreach (run_queue, object o) {
if (is_describer(o)) {
tests += ({ @o->tests });
} else {
tests += ({ o });
}
}
array successes = filter(tests, lambda (.Test t) { return t->is_success; });
array failures = filter(tests, lambda (.Test t) { return !!t->error; });
array skips = filter(tests, lambda (.Test t) { return t->skipped; });
return ([
"successes": successes,
"failures": failures,
"skips": skips,
]);
}
public bool is_describer(object o) {
return object_program(o) == .Describer;
}
public bool is_test(object o) {
return object_program(o) == .Test;
}
public RunQueue `queue() {
return run_queue;
}
public bool has_run_tests() {
function filter_fn = lambda(.Test|.Describer t) {
if (is_test(t)) {
return !t->skipped;
} else {
return t->number_of_tests_run() > 0;
}
};
int n = sizeof(filter(run_queue, filter_fn)) > 0;
return n > 0;
}
protected string _sprintf() {
return sprintf("%O(%q)", object_program(this), file);
}