forked from crabmusket/tasman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cs
239 lines (202 loc) · 6.59 KB
/
main.cs
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//-----------------------------------------------------------------------------
// Tasman: A TorqueScript unit-testing library
function Tasman::create() {
new ScriptObject(Tasman) {
suites = new SimSet();
reporter = new ScriptObject(TasmanConsoleReporter) {
verbose = false;
};
};
// Enable globals by default.
Tasman.globals(true);
}
function Tasman::destroy() {
Tasman.cleanUp();
Tasman.delete();
}
function Tasman::cleanUp(%this) {
%this.suites.deleteAllObjects();
}
function Tasman::globals(%this, %on) {
if(%on) {
activatePackage(TasmanGlobals);
} else {
deactivatePackage(TasmanGlobals);
}
}
package TasmanGlobals {
function test(%name) { return Tasman.test(%name); }
function expect(%value) { return Tasman.expect(%value); }
};
function Tasman::runAll(%this) {
%this.reporter.begin("_all");
%dummy = new ScriptObject() { class = SomethingShould; };
foreach(%suite in Tasman.suites) {
%this.reporter.begin(%suite.subject);
%this._currentSuite = %suite;
%suite.passes = 0;
%suite.fails = 0;
%suite.count = 0;
%tester = %suite.tester;
%methods = %tester.dumpMethods();
for(%i = 0; %i < %methods.count(); %i++) {
%method = %methods.getKey(%i);
%suite.expectationNumber = 0;
// We only want methods unique to this tester, so we compare against an
// instance of the SomethingShould base class.
if(!%dummy.isMethod(%method)) {
// Used by the reporter, usually.
%suite._currentMethod = %method;
if(%suite.isMethod(before)) {
%suite.before();
}
%tester.call(%method);
if(%suite.isMethod(after)) {
%suite.after();
}
}
}
%this.reporter.reportSuite(%suite);
%this.reporter.end();
%methods.delete();
%this._currentSuite = "";
}
%dummy.delete();
%this.reporter.end();
}
function Tasman::test(%this, %name) {
new ScriptObject(%name @ Tests) {
subject = %name;
class = Suite;
tester = new ScriptObject(%name @ Should) {
class = SomethingShould;
};
};
}
function Suite::onAdd(%this) {
Tasman.suites.add(%this);
}
function Suite::onRemove(%this) {
%this.tester.delete();
}
function Tasman::expect(%this, %value) {
return new ScriptObject() {
class = Expectation;
value = %value;
inverted = false;
suite = Tasman._currentSuite;
};
}
function Expectation::not(%this) {
%this.inverted = !%this.inverted;
return %this;
}
function Expectation::toExist(%this) {
%this._test(isObject(%this.value), "expected \"" @ %this.value @ "\"to be an object");
return %this;
}
function Expectation::toBeDefined(%this) {
%message = %this.inverted ? "expected an empty string" : "expected a non-empty string";
%this._test(%this.value !$= "", %message);
return %this;
}
function Expectation::toBe(%this, %target) {
%messagePart = %this.inverted ? "not to be" : "to be";
%this._test(%this.value $= %target, "expected" SPC %this.value SPC %messagePart SPC %target);
return %this;
}
function Expectation::toEqual(%this, %target) {
%messagePart = %this.inverted ? "not to equal" : "to equal";
%this._test(%this.value == %target, "expected" SPC %this.value SPC %messagePart SPC %target);
return %this;
}
function Expectation::toHold(%this) {
%messagePart = %this.inverted ? "false" : "true";
%this._test(%this.value == true, "expected" SPC %this.value SPC "to be" SPC %messagePart);
}
function Expectation::toBeAVector(%this, %dim) {
%passing = getWordCount(%this.value) == %dim;
if(%passing) {
for(%i = 0; %i < %dim; %i++) {
%word = getWord(%this.value, %i);
// Check for numeric values, which are unaltered by addition with 0.
%pasing = %passing && (%word + 0 $= %word);
}
}
%message = (%this.inverted ? "\" not" : "\"") SPC "to be a numeric" SPC %dim @ "D vector";
%this._test(%passing, "expected \"" @ %this.value @ %message);
return %this;
}
function Expectation::toHave(%this, %num) {
%this.target = %num;
return %this;
}
function Expectation::words(%this) {
%message = %this.inverted ? "not to have" : "to have";
%this._test(getWordCount(%this.value) == %this.target,
"expected \"" @ %this.value @ "\"" SPC %message SPC %this.target SPC "words");
return %this;
}
function Expectation::word(%this) {
return %this.words();
}
function Expectation::fields(%this) {
%message = %this.inverted ? "not to have" : "to have";
%this._test(getFieldCount(%this.value) == %this.target,
"expected \"" @ %this.value @ "\"" SPC %message SPC %this.target SPC "tab-separated fields");
return %this;
}
function Expectation::field(%this) { return %this.fields(); }
function Expectation::lines(%this) {
%message = %this.inverted ? "not to have" : "to have";
%this._test(getRecordCount(%this.value) == %this.target,
"expected \"" @ %this.value @ "\"" SPC %message SPC %this.target SPC "lines");
return %this;
}
function Expectation::line(%this) { return %this.lines(); }
function Expectation::records(%this) { return %this.lines(); }
function Expectation::record(%this) { return %this.lines(); }
function Expectation::_test(%this, %pred, %failMessage) {
%pass = %this.inverted ? !%pred : %pred;
%this.suite.count++;
%this.suite.expectationNumber++;
if(%pass) {
%this.suite.passes++;
} else {
%this.suite.fails++;
%niceMethod = strreplace(%this.suite._currentMethod, "_", " ");
error(%this.suite.subject SPC "should" SPC %niceMethod SPC "[" @ %this.suite.expectationNumber @ "]:" SPC %failMessage);
}
return %this;
}
function TasmanConsoleReporter::begin(%this, %group) {
if(%group $= "_all") {
%this.depth = 0;
echo("\c8==================================================");
echo("\c8Running Tasman test suite!");
echo("");
} else if(%this.verbose) {
echo("Testing" SPC %group @ "...");
}
%this.depth++;
}
function TasmanConsoleReporter::end(%this) {
%this.depth--;
if(0 == %this.depth) {
if(!%this.verbose) {
echo("");
}
echo("\c8That concludes this Tasman test session");
echo("\c8==================================================");
}
}
function TasmanConsoleReporter::reportSuite(%this, %suite) {
if(%suite.fails > 0) {
error(%suite.subject SPC "failed" SPC %suite.fails SPC "of" SPC %suite.count);
} else {
echo("\c9" @ %suite.subject SPC "passed" SPC %suite.passes SPC "of" SPC %suite.count);
}
if(%this.verbose) {
echo("");
}
}