/
NSUnit.newt
190 lines (186 loc) · 7.4 KB
/
NSUnit.newt
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
/**
* NSUnit (or NewtonScriptUnit) is a simple NewtonScript unit testing framework.
*
* Based on <http://www.xprogramming.com/testfram.htm>
* -and-
* <http://junit.sourceforge.net/>
* -and-
* <http://www.phpunit.de/manual/current/en/index.html>
*
* TO DO:
*
* - Implement AssertStringBeginsWith(), AssertStringEndsWith()
*/
// I'd prefer to use DeclareUnit(), like NHttpLib, but it's not yet implemented in NEWT/0
DefGlobalVar('|NSUnit:makkintosshu|, NSUnit := {
protoNSUnitTestCase: {
assertions: 0,
passes: 0,
fails: 0,
/**
* SetUp() is called prior to each test case being run, so can be overridden
* if you need to set up fixtures.
*/
SetUp: func () begin
end,
/**
* TearDown() is called after each test case is run, so should be overridden
* to destroy anything done in SetUp().
*/
TearDown: func () begin
end,
/*
TestNil: func () begin
self:AssertNil(nil);
end,
*/
/**
* Run() runs all tests (any function named Test*) in an NSUnitTestCase.
*
* @return bool Returns true if all tests passed, otherwise nil.
*/
Run: func () begin
local success := true;
foreach slot, value deeply in self do begin
local slotName := SPrintObject(slot); // is this needed for NewtonOS? It is for NEWT/0!
if IsFunction(value) and BeginsWith(slotName, "Test") then begin
self:SetUp();
Print(slotName & "()... ");
try
ProtoPerform(self, Intern(slotName), nil);
self.passes := self.passes + 1;
Print("PASS\n");
onexception |evt.ex.msg.assert| do begin
success := nil;
self.fails := self.fails + 1;
Print("FAIL:\n\t" & CurrentException().message & "\n");
end;
self:TearDown();
end;
end;
return success;
end,
/**
* AssertEquals()
*
* @param mixed expected The value you expect the actual value to equal.
* @param mixed actual The actual value.
*/
AssertEquals: func (expected, actual) begin
self.assertions := self.assertions + 1;
if IsString(expected) and IsString(actual) then begin
if StrExactCompare(expected, actual) = 0 then nil else Throw('|evt.ex.msg.assert|, "Failed asserting that '" & actual & "' equals '" & expected & "'.");
end
else begin
if expected = actual then nil else Throw('|evt.ex.msg.assert|, "Failed asserting that '" & actual & "' equals '" & expected & "'.");
end
end,
/**
* AssertNotEquals()
*
* @param mixed expected The value you expect the actual value not to equal.
* @param mixed actual The actual value.
*/
AssertNotEquals: func (expected, actual) begin
self.assertions := self.assertions + 1;
if IsString(expected) and IsString(actual) then begin
if StrExactCompare(expected, actual) <> 0 then nil else Throw('|evt.ex.msg.assert|, "Failed asserting that '" & actual & "' equals '" & expected & "'.");
end
else begin
if expected <> actual then nil else Throw('|evt.ex.msg.assert|, "Failed asserting that '" & actual & "' equals '" & expected & "'.");
end
end,
/**
* AssertTrue()
*
* @param mixed condition The value you expect to equal true.
*/
AssertTrue: func (condition) begin
self.assertions := self.assertions + 1;
if condition = true then nil else Throw('|evt.ex.msg.assert|, "Failed asserting that '" & condition & "' equals true.");
end,
/**
* AssertNil()
*
* @param mixed condition The value you expect to equal nil.
*/
AssertNil: func (condition) begin
self.assertions := self.assertions + 1;
if condition = nil then nil else Throw('|evt.ex.msg.assert|, "Failed asserting that '" & condition & "' equals nil.");
end,
/**
* AssertGreaterThan()
*
* @param int expected The value you expect the actual value to be greater than.
* @param int actual The actual value.
*/
AssertGreaterThan: func (expected, actual) begin
self.assertions := self.assertions + 1;
if actual > expected then nil else Throw('|evt.ex.msg.assert|, "Failed asserting that '" & actual & "' is greater than '" & expected & "'.");
end,
/**
* AssertGreaterThanOrEqual()
*
* @param int expected The value you expect the actual value to be greater than or equal to.
* @param int actual The actual value.
*/
AssertGreaterThanOrEqual: func (expected, actual) begin
self.assertions := self.assertions + 1;
if actual >= expected then nil else Throw('|evt.ex.msg.assert|, "Failed asserting that '" & actual & "' is greater than or equal to '" & expected & "'.");
end,
/**
* AssertLessThan()
*
* @param int expected The value you expect the actual value to be less than.
* @param int actual The actual value.
*/
AssertLessThan: func (expected, actual) begin
self.assertions := self.assertions + 1;
if actual < expected then nil else Throw('|evt.ex.msg.assert|, "Failed asserting that '" & actual & "' is less than '" & expected & "'.");
end,
/**
* AssertLessThanOrEqual()
*
* @param int expected The value you expect the actual value to be less than or equal to.
* @param int actual The actual value.
*/
AssertLessThanOrEqual: func (expected, actual) begin
self.assertions := self.assertions + 1;
if actual <= expected then nil else Throw('|evt.ex.msg.assert|, "Failed asserting that '" & actual & "' is less than or equal to '" & expected & "'.");
end,
/**
* AssertFrameContainsSlot()
*
* @param int frame The frame you expect to contain the slot.
* @param int slot The symbol naming the slot which you expect to exist in the frame.
*/
AssertFrameContainsSlot: func (frame, slot) begin
self.assertions := self.assertions + 1;
if HasVariable(frame, slot) then nil else Throw('|evt.ex.msg.assert|, "Failed asserting that '" & frame & "' contains the slot '" & slot & "'.");
end
},
protoNSUnitTestSuite: {
testCases: [],
assertions: 0,
passes: 0,
fails: 0,
/**
* Run()
*
* @return bool Returns true if all tests passed, otherwise nil.
*/
Run: func () begin
local success := true;
foreach testCase in testCases do begin
if not testCase:Run() then success := nil;
self.assertions := self.assertions + testCase.assertions;
self.passes := self.passes + testCase.passes;
self.fails := self.fails + testCase.fails;
end;
Print("Assertions:" && self.assertions & "\n");
Print("Passing Tests:" && self.passes & "\n");
Print("Failing Tests:" && self.fails & "\n");
return success;
end
}
});