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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var EXPORTED_SYMBOLS = ["assert", "assertTrue", "assertFalse", "assertEquals", "assertNotEquals",
"assertNull", "assertNotNull", "assertUndefined", "assertNotUndefined",
"assertNaN", "assertNotNaN", "assertArrayContains", "fail", "pass"];
// Array.isArray comes with JavaScript 1.8.5 (Firefox 4)
// cf. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
Array.isArray = Array.isArray || function(o) { return Object.prototype.toString.call(o) === '[object Array]'; };
var frame = {}; Components.utils.import("resource://mozmill/modules/frame.js", frame);
var ifJSONable = function (v) {
if (typeof(v) == 'function') {
return undefined;
} else {
return v;
}
}
var assert = function (booleanValue, comment) {
if (booleanValue) {
frame.events.pass({'function':'jum.assert', 'value':ifJSONable(booleanValue), 'comment':comment});
return true;
} else {
frame.events.fail({'function':'jum.assert', 'value':ifJSONable(booleanValue), 'comment':comment});
return false;
}
}
var assertTrue = function (booleanValue, comment) {
if (typeof(booleanValue) != 'boolean') {
frame.events.fail({'function':'jum.assertTrue', 'value':ifJSONable(booleanValue),
'message':'Bad argument, value type '+typeof(booleanValue)+' != "boolean"',
'comment':comment});
return false;
}
if (booleanValue) {
frame.events.pass({'function':'jum.assertTrue', 'value':ifJSONable(booleanValue),
'comment':comment});
return true;
} else {
frame.events.fail({'function':'jum.assertTrue', 'value':ifJSONable(booleanValue),
'comment':comment});
return false;
}
}
var assertFalse = function (booleanValue, comment) {
if (typeof(booleanValue) != 'boolean') {
frame.events.fail({'function':'jum.assertFalse', 'value':ifJSONable(booleanValue),
'message':'Bad argument, value type '+typeof(booleanValue)+' != "boolean"',
'comment':comment});
return false;
}
if (!booleanValue) {
frame.events.pass({'function':'jum.assertFalse', 'value':ifJSONable(booleanValue),
'comment':comment});
return true;
} else {
frame.events.fail({'function':'jum.assertFalse', 'value':ifJSONable(booleanValue),
'comment':comment});
return false;
}
}
var assertEquals = function (value1, value2, comment) {
// Case where value1 is an array
if (Array.isArray(value1)) {
if (!Array.isArray(value2)) {
frame.events.fail({'function':'jum.assertEquals', 'comment':comment,
'message':'Bad argument, value1 is an array and value2 type ' +
typeof(value2)+' != "array"',
'value2':ifJSONable(value2)});
return false;
}
if (value1.length != value2.length) {
frame.events.fail({'function':'jum.assertEquals', 'comment':comment,
'message':"The arrays do not have the same length",
'value1':ifJSONable(value1), 'value2':ifJSONable(value2)});
return false;
}
for (var i = 0; i < value1.length; i++) {
if (value1[i] !== value2[i]) {
frame.events.fail(
{'function':'jum.assertEquals', 'comment':comment,
'message':"The element of the arrays are different at index " + i,
'value1':ifJSONable(value1), 'value2':ifJSONable(value2)});
return false;
}
}
frame.events.pass({'function':'jum.assertEquals', 'comment':comment,
'value1':ifJSONable(value1), 'value2':ifJSONable(value2)});
return true;
}
// Case where value1 is not an array
if (value1 == value2) {
frame.events.pass({'function':'jum.assertEquals', 'comment':comment,
'value1':ifJSONable(value1), 'value2':ifJSONable(value2)});
return true;
} else {
frame.events.fail({'function':'jum.assertEquals', 'comment':comment,
'value1':ifJSONable(value1), 'value2':ifJSONable(value2)});
return false;
}
}
var assertNotEquals = function (value1, value2, comment) {
if (value1 != value2) {
frame.events.pass({'function':'jum.assertNotEquals', 'comment':comment,
'value1':ifJSONable(value1), 'value2':ifJSONable(value2)});
return true;
} else {
frame.events.fail({'function':'jum.assertNotEquals', 'comment':comment,
'value1':ifJSONable(value1), 'value2':ifJSONable(value2)});
return false;
}
}
var assertNull = function (value, comment) {
if (value == null) {
frame.events.pass({'function':'jum.assertNull', 'comment':comment,
'value':ifJSONable(value)});
return true;
} else {
frame.events.fail({'function':'jum.assertNull', 'comment':comment,
'value':ifJSONable(value)});
return false;
}
}
var assertNotNull = function (value, comment) {
if (value != null) {
frame.events.pass({'function':'jum.assertNotNull', 'comment':comment,
'value':ifJSONable(value)});
return true;
} else {
frame.events.fail({'function':'jum.assertNotNull', 'comment':comment,
'value':ifJSONable(value)});
return false;
}
}
var assertUndefined = function (value, comment) {
if (value == undefined) {
frame.events.pass({'function':'jum.assertUndefined', 'comment':comment,
'value':ifJSONable(value)});
return true;
} else {
frame.events.fail({'function':'jum.assertUndefined', 'comment':comment,
'value':ifJSONable(value)});
return false;
}
}
var assertNotUndefined = function (value, comment) {
if (value != undefined) {
frame.events.pass({'function':'jum.assertNotUndefined', 'comment':comment,
'value':ifJSONable(value)});
return true;
} else {
frame.events.fail({'function':'jum.assertNotUndefined', 'comment':comment,
'value':ifJSONable(value)});
return false;
}
}
var assertNaN = function (value, comment) {
if (isNaN(value)) {
frame.events.pass({'function':'jum.assertNaN', 'comment':comment,
'value':ifJSONable(value)});
return true;
} else {
frame.events.fail({'function':'jum.assertNaN', 'comment':comment,
'value':ifJSONable(value)});
return false;
}
}
var assertNotNaN = function (value, comment) {
if (!isNaN(value)) {
frame.events.pass({'function':'jum.assertNotNaN', 'comment':comment,
'value':ifJSONable(value)});
return true;
} else {
frame.events.fail({'function':'jum.assertNotNaN', 'comment':comment,
'value':ifJSONable(value)});
return false;
}
}
var assertArrayContains = function(array, value, comment) {
if (!Array.isArray(array)) {
frame.events.fail({'function':'jum.assertArrayContains', 'comment':comment,
'message':'Bad argument, value type '+typeof(array)+' != "array"',
'value':ifJSONable(array)});
return false;
}
for (var i = 0; i < array.length; i++) {
if (array[i] === value) {
frame.events.pass({'function':'jum.assertArrayContains', 'comment':comment,
'value1':ifJSONable(array), 'value2':ifJSONable(value)});
return true;
}
}
frame.events.fail({'function':'jum.assertArrayContains', 'comment':comment,
'value1':ifJSONable(array), 'value2':ifJSONable(value)});
return false;
}
var fail = function (comment) {
frame.events.fail({'function':'jum.fail', 'comment':comment});
return false;
}
var pass = function (comment) {
frame.events.pass({'function':'jum.pass', 'comment':comment});
return true;
}
|