-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest.js
383 lines (379 loc) · 14.9 KB
/
test.js
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
window = {
printStackTrace: function () { }
};
var nodeunit = require("nodeunit");
require("./exceptions");
module.exports = {
setUp: function (callback) {
window.exceptionReported = undefined;
window.ex.handler
.scope(window.ex.handler.scopeOption.all)
.beforeReport(function (exception) {
exception.beforeReportCallbackExecuted = true;
})
.reportCallback(function (exception) {
exception.callbackExecuted = true;
window.exceptionReported = exception;
})
.guard(new window.ex.Guard());
callback();
},
//basic Exception functionality
testExecutedBeforeReport: function (test) {
var exception = new window.ex.Exception("foo");
exception.report();
test.equals(exception.beforeReportCallbackExecuted, true);
test.done();
},
testExecutedCallback: function (test) {
var exception = new window.ex.Exception("foo");
exception.report();
test.equals(exception.callbackExecuted, true);
test.done();
},
testCustomName: function (test) {
var exception = new window.ex.Exception("foo", {
name: "bar"
});
exception.report();
test.equals(exception.name(), "bar");
test.done();
},
testDefaultName: function (test) {
var exception = new window.ex.Exception("foo", {
name: "bar"
});
exception.report();
test.equals(exception.name(), "bar");
test.done();
},
testData: function (test) {
var exception = new window.ex.Exception("foo", {
data: {
foo: "bar"
}
});
exception.report();
test.equals(exception.data().foo, "bar");
test.done();
},
//Inner exception
testInnerException: function (test) {
var exception = new window.ex.Exception("foo", {
innerException: new window.ex.ArgumentException("bar")
});
exception.report();
test.equals(exception.message(), "foo");
test.equals(exception.innerException().message(), "bar");
test.done();
},
//Options and guards
testOptionCallback: function (test) {
var exception = new window.ex.Exception("foo", {
options: new window.ex.Options({ reportCallback: false })
});
test.equals(exception.options().reportCallback(), false);
test.done();
},
testOptionAllfalse: function (test) {
var exception = new window.ex.Exception("foo", {
options: new window.ex.Options().toggleAll(false)
});
exception.report();
test.equals(exception.options().stacktrace(), false);
test.equals(exception.options().screenshot(), false);
test.equals(exception.options().reportPost(), false);
test.equals(exception.options().reportToExceptionsJsPlatform(), false);
test.equals(exception.options().reportCallback(), false);
test.done();
},
testDefaultGuard: function (test) {
var exception = new window.ex.Exception("foo");
exception.report();
//we default to enable stacktrace and callback because we
//have an Error.stack and a specified callback.
//But we haven't specified a html2canvas url, post url, or client id for exceptionsjs platform.
test.equals(exception.options().stacktrace(), true);
test.equals(exception.options().screenshot(), false);
test.equals(exception.options().reportPost(), false);
test.equals(exception.options().reportToExceptionsJsPlatform(), false);
test.equals(exception.options().reportCallback(), true);
test.done();
},
testCustomGuard: function (test) {
var guard = new window.ex.Guard()
.protectAgainst(function (o, e) {
if (e.message() === "foo") {
return o.toggleAll(false);
}
return o;
});
window.ex.handler.guard(guard);
var exception = new window.ex.Exception("foo");
exception.report();
test.equals(exception.options().stacktrace(), false);
test.equals(exception.options().screenshot(), false);
test.equals(exception.options().reportPost(), false);
test.equals(exception.options().reportToExceptionsJsPlatform(), false);
test.equals(exception.options().reportCallback(), false);
test.done();
},
//Exception static functions
testReportIfTrueWithOutMessage: function (test) {
window.ex.Exception.reportIf(true);
test.equals(window.exceptionReported.callbackExecuted, true);
test.equals(window.exceptionReported.message(), "Condition evaluated to truthy");
test.done();
},
testReportIfTrueWithMessage: function (test) {
window.ex.Exception.reportIf(true, "foo");
test.equals(window.exceptionReported.callbackExecuted, true);
test.equals(window.exceptionReported.message(), "foo");
test.done();
},
testReportIfFalse: function (test) {
window.ex.Exception.reportIf(false);
test.equals(window.exceptionReported, undefined);
test.done();
},
testShortcutReportIfTrueWithOutMessage: function (test) {
window.ex.reportIf(true);
test.equals(window.exceptionReported.callbackExecuted, true);
test.equals(window.exceptionReported.message(), "Condition evaluated to truthy");
test.done();
},
testShortcutReportIfTrueWithMessage: function (test) {
window.ex.reportIf(true, "foo");
test.equals(window.exceptionReported.callbackExecuted, true);
test.equals(window.exceptionReported.message(), "foo");
test.done();
},
testShortcutReportIfFalse: function (test) {
window.ex.reportIf(false);
test.equals(window.exceptionReported, undefined);
test.done();
},
//serialization
testToSerializableObject: function (test) {
var data = { foo: "bar" }
exception = new window.ex.Exception("foo", {
data: data
}),
serializableObject = exception.toSerializableObject();
test.equals(serializableObject.name, "Exception");
test.equals(serializableObject.message, "foo");
test.equals(serializableObject.data, data);
test.done();
},
//Custom Exceptions
testArgumentException: function (test) {
var argumentException = new window.ex.ArgumentException("foo");
argumentException.report();
test.equals(argumentException.callbackExecuted, true);
test.equals(argumentException.name(), "ArgumentException");
test.done();
},
testInvalidOperationException: function (test) {
var invalidOperationException = new window.ex.InvalidOperationException("foo");
invalidOperationException.report();
test.equals(invalidOperationException.callbackExecuted, true);
test.equals(invalidOperationException.name(), "InvalidOperationException");
test.done();
},
testEvalException: function (test) {
var exception = new window.ex.EvalException("foo");
exception.report();
test.equals(exception.callbackExecuted, true);
test.equals(exception.name(), "EvalException");
test.ok(exception.error() instanceof EvalError);
test.done();
},
testRangeException: function (test) {
var exception = new window.ex.RangeException("foo");
exception.report();
test.equals(exception.callbackExecuted, true);
test.equals(exception.name(), "RangeException");
test.ok(exception.error() instanceof RangeError);
test.done();
},
testReferenceException: function (test) {
var exception = new window.ex.ReferenceException("foo");
exception.report();
test.equals(exception.callbackExecuted, true);
test.equals(exception.name(), "ReferenceException");
test.ok(exception.error() instanceof ReferenceError);
test.done();
},
testSyntaxException: function (test) {
var exception = new window.ex.SyntaxException("foo");
exception.report();
test.equals(exception.callbackExecuted, true);
test.equals(exception.name(), "SyntaxException");
test.ok(exception.error() instanceof SyntaxError);
test.done();
},
testTypeException: function (test) {
var exception = new window.ex.TypeException("foo");
exception.report();
test.equals(exception.callbackExecuted, true);
test.equals(exception.name(), "TypeException");
test.ok(exception.error() instanceof TypeError);
test.done();
},
testURIException: function (test) {
var exception = new window.ex.URIException("foo");
exception.report();
test.equals(exception.callbackExecuted, true);
test.equals(exception.name(), "URIException");
test.ok(exception.error() instanceof URIError);
test.done();
},
testCreateCustomException: function (test) {
var CustomException = window.ex.createCustomException({
exception: function CustomException(message, config) {
if (!(this instanceof CustomException)) {
return new CustomException(message, config);
}
window.ex.Exception.call(this, message, config);
},
baseException: window.ex.Exception
}),
instance = new CustomException("foo");
instance.report();
test.equals(instance.name(), "CustomException");
test.equals(instance.callbackExecuted, true);
test.done();
},
testTwoLayerCustomException: function (test) {
var CustomArgumentException = window.ex.createCustomException({
exception: function CustomArgumentException(message, config) {
if (!(this instanceof CustomArgumentException)) {
return new CustomArgumentException(message, config);
}
window.ex.ArgumentException.call(this, message, config);
},
baseException: window.ex.ArgumentException
}),
instance = new CustomArgumentException("foo");
instance.report();
test.equals(instance.name(), "CustomArgumentException");
test.equals(instance.callbackExecuted, true);
test.done();
},
//handler
testHandleException: function (test) {
window.ex.handler.handle({
errorMessage: "foo",
url: "url",
lineNumber: 1,
columnNumber: 1,
error: new window.ex.Exception("message") });
test.equals(window.exceptionReported.name(), "Exception");
test.ok(window.exceptionReported instanceof window.ex.Exception);
test.ok(window.exceptionReported.error() instanceof Error);
test.done();
},
testHandleError: function (test) {
window.ex.handler.handle({
errorMessage: "foo",
url: "url",
lineNumber: 1,
columnNumber: 1,
error: new Error() });
test.equals(window.exceptionReported.name(), "Exception");
test.ok(window.exceptionReported instanceof window.ex.Exception);
test.ok(window.exceptionReported.error() instanceof Error);
test.done();
},
testHandleEvalError: function (test) {
window.ex.handler.handle({
errorMessage: "foo",
url: "url",
lineNumber: 1,
columnNumber: 1,
error: new EvalError() });
test.equals(window.exceptionReported.name(), "EvalException");
test.ok(window.exceptionReported instanceof window.ex.Exception);
test.ok(window.exceptionReported.error() instanceof EvalError);
test.done();
},
testHandleRangeError: function (test) {
window.ex.handler.handle({
errorMessage: "foo",
url: "url",
lineNumber: 1,
columnNumber: 1,
error: new RangeError() });
test.equals(window.exceptionReported.name(), "RangeException");
test.ok(window.exceptionReported instanceof window.ex.Exception);
test.ok(window.exceptionReported.error() instanceof RangeError);
test.done();
},
testHandleSyntaxError: function (test) {
window.ex.handler.handle({
errorMessage: "foo",
url: "url",
lineNumber: 1,
columnNumber: 1,
error: new SyntaxError() });
test.equals(window.exceptionReported.name(), "SyntaxException");
test.ok(window.exceptionReported instanceof window.ex.Exception);
test.ok(window.exceptionReported.error() instanceof SyntaxError);
test.done();
},
testHandleTypeError: function (test) {
window.ex.handler.handle({
errorMessage: "foo",
url: "url",
lineNumber: 1,
columnNumber: 1,
error: new TypeError() });
test.equals(window.exceptionReported.name(), "TypeException");
test.ok(window.exceptionReported instanceof window.ex.Exception);
test.ok(window.exceptionReported.error() instanceof TypeError);
test.done();
},
testHandleURIError: function (test) {
window.ex.handler.handle({
errorMessage: "foo",
url: "url",
lineNumber: 1,
columnNumber: 1,
error: new URIError() });
test.equals(window.exceptionReported.name(), "URIException");
test.ok(window.exceptionReported instanceof window.ex.Exception);
test.ok(window.exceptionReported.error() instanceof URIError);
test.done();
},
testHandleURIError: function (test) {
window.ex.handler.handle({
errorMessage: "foo",
url: "url",
lineNumber: 1,
columnNumber: 1,
error: new URIError() });
test.equals(window.exceptionReported.name(), "URIException");
test.ok(window.exceptionReported instanceof window.ex.Exception);
test.ok(window.exceptionReported.error() instanceof URIError);
test.done();
},
testScope: function (test) {
var exception = new window.ex.Exception("foo");
window.ex.handler.scope(window.ex.handler.scopeOption.exceptions);
window.ex.handler.handle({
errorMessage: "foo",
url: "url",
lineNumber: 1,
columnNumber: 1,
error: new Error("foo") });
test.equals(window.exceptionReported, undefined);
window.ex.handler.handle({
errorMessage: "foo",
url: "url",
lineNumber: 1,
columnNumber: 1,
error: exception });
test.equals(window.exceptionReported, exception);
test.done();
}
};