-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
541 lines (445 loc) · 14 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/* globals require, KixxAssert, console */
(function (KixxAssert) {
'use strict';
var helpers = KixxAssert.helpers;
var assert = KixxAssert.assert;
var AssertionError = KixxAssert.AssertionError;
function test(title, block) {
try {
block();
} catch (err) {
console.error('Fail: '+ title); // eslint-disable-line no-console
console.error(err.stack); // eslint-disable-line no-console
return false;
}
console.log('Pass: '+ title); // eslint-disable-line no-console
return true;
}
test('AssertionError', function () {
var err = new AssertionError();
assert.isEqual('Unspecified AssertionError', err.message, '.message');
assert.isEqual('AssertionError', err.name, '.name');
// Slices unwanted items off the stack when capturing the stack trace.
// The first line of the stack trace is .name and .message, the second
// line is a reference to the file location, which should be right here:
var secondLine = err.stack.split('\n')[1];
assert.isMatch(/\/test\.js:[\d]{2}:[\d]{2}$/, secondLine, '.stack');
});
test('assertion() factory', function () {
var assertTruthy = helpers.assertion1(helpers.identity, function (actual) {
return helpers.printf('expected %x to be truthy', actual);
});
var assertEqual = helpers.assertion2(helpers.equal, function (expected, actual) {
return helpers.printf('expected %x to equal %x', actual, expected);
});
var err1;
var err2;
try {
assertTruthy(false, 'supposed to be false');
} catch (err) {
err1 = err;
}
try {
assertEqual(1, 2, 'supposed to be equal');
} catch (err) {
err2 = err;
}
assert.isDefined(err1, 'error 1');
assert.isDefined(err2, 'error 2');
var stack1 = err1.stack.split('\n');
var stack2 = err2.stack.split('\n');
assert.isEqual('AssertionError: supposed to be false :: expected Boolean(false) to be truthy', stack1[0]);
assert.isMatch(/\/test\.js:[\d]{2}:[\d]{1}$/, stack1[1]);
assert.isEqual('AssertionError: supposed to be equal :: expected Number(2) to equal Number(1)', stack2[0]);
assert.isMatch(/\/test\.js:[\d]{2}:[\d]{1}$/, stack2[1]);
});
test('helpers.identity()', function () {
var x = {rnd: Math.random()};
var y = helpers.identity(x);
assert.isEqual(x, y, 'returns exact given argument');
});
test('helpers.complement()', function () {
var flipflop = helpers.complement(helpers.identity);
var x = true;
var y = flipflop(x);
assert.isEqual(false, y, 'creates a function which returns the boolean opposite');
});
test('helpers.toString()', function () {
var s = '';
var i = 0;
var b = false;
var f = 1.1;
var u = (function (_a) { return _a; }());
var n = null;
var nn = 1 / 'foo'; // NaN
var a = [null, null];
var o = {};
var d = new Date();
var r = /foo$/;
var fn = function boilWater() {};
var json = {
toJSON: function () {
return {foo: 'bar'};
}
};
function Cat() {}
Cat.prototype.toString = function () { return 'Garfield'; };
var cat = new Cat();
assert.isEqual('String("")', helpers.toString(s), 'String');
assert.isEqual('Number(0)', helpers.toString(i), 'Number');
assert.isEqual('Boolean(false)', helpers.toString(b), 'Boolean');
assert.isEqual('Number(1.1)', helpers.toString(f), 'Float');
assert.isEqual('undefined', helpers.toString(u), 'Undefined');
assert.isEqual('null', helpers.toString(n), 'Null');
assert.isEqual('Number(NaN)', helpers.toString(nn), 'NaN');
assert.isEqual('Array([0..1])', helpers.toString(a), 'Array');
assert.isEqual('Object({})', helpers.toString(o), 'Object');
assert.isEqual('Date('+ d.toString() +')', helpers.toString(d), 'Date');
assert.isEqual('RegExp(/foo$/)', helpers.toString(r), 'RegExp');
assert.isEqual('Function(boilWater() {})', helpers.toString(fn), 'function');
assert.isEqual('{"foo":"bar"}', helpers.toString(json), 'with toJSON');
assert.isEqual('Cat(Garfield)', helpers.toString(cat), 'custom constructor');
});
test('assert.isOk()', function () {
try {
assert.isOk(false, 'should fail');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('should fail :: expected Boolean(false) to be truthy', err.message);
}
assert.isOk(true, 'should pass');
});
test('assert.isNotOk()', function () {
try {
assert.isNotOk(true, 'should fail');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('should fail :: expected Boolean(true) not to be truthy', err.message);
}
assert.isNotOk(false, 'should pass');
});
test('assert.isEqual()', function () {
try {
assert.isEqual({}, {}, 'should fail');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('should fail :: expected Object({}) to equal Object({})', err.message);
}
});
test('assert.isNotEqual()', function () {
var x = {};
var y = x;
try {
assert.isNotEqual(x, y, 'should fail');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('should fail :: expected Object({}) not to equal Object({})', err.message);
}
assert.isNotEqual({}, {}, 'should pass');
});
test('assert.isMatch()', function () {
assert.isMatch('foo', 'foo', 'using string match');
assert.isMatch(/^[fo]{3}$/, 'foo', 'using RegExp match');
});
test('assert.isDefined', function () {
var obj = {
x: null
};
try {
assert.isDefined(obj.y, 'fails');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('fails :: expected undefined to be defined', err.message);
}
assert.isDefined(obj.x, 'passes');
});
test('assert.isNotEmpty', function () {
var s = '';
var i = 0;
var b = false;
var u = (function (_a) { return _a; }());
var n = null;
var nn = 1 / 'foo'; // NaN
var a = [0, 0];
var o = {};
var d = new Date();
var fn = function boilWater() {};
function Cat() {
this.color = 'black';
}
var cat = new Cat();
try {
assert.isNotEmpty(s, 'String');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('String :: expected String("") not to be empty', err.message);
}
assert.isNotEmpty('foo', 'full String');
try {
assert.isNotEmpty(i, 'Integer');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('Integer :: expected Number(0) not to be empty', err.message);
}
assert.isNotEmpty(-1, 'truthy Number');
try {
assert.isNotEmpty(b, 'Boolean');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('Boolean :: expected Boolean(false) not to be empty', err.message);
}
assert.isNotEmpty(true, 'true');
try {
assert.isNotEmpty(u, 'undefined');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('undefined :: expected undefined not to be empty', err.message);
}
try {
assert.isNotEmpty(n, 'null');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('null :: expected null not to be empty', err.message);
}
try {
assert.isNotEmpty(nn, 'NaN');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('NaN :: expected Number(NaN) not to be empty', err.message);
}
assert.isNotEmpty(a, 'Array');
try {
assert.isNotEmpty(o, 'Object');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('Object :: expected Object({}) not to be empty', err.message);
}
assert.isNotEmpty(d, 'Date');
assert.isNotEmpty(fn, 'Function');
assert.isNotEmpty(cat, 'Cat');
});
test('assert.includes()', function () {
var s = '';
var i = 1;
var b = true;
var u = (function (_a) { return _a; }());
var n = null;
var nn = 1 / 'foo'; // NaN
var d = new Date();
var fn = function boilWater() {};
var x = {};
var a = [null, x, null];
var o = {a: null, b: x, c: null};
var str = 'The quick brown fox jumped over the lazy dog.';
try {
assert.includes('', s, 'String');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.includes(1, i, 'Integer');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.includes(true, b, 'Integer');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.includes(u, u, 'undefined');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.includes(null, n, 'null');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.includes(NaN, nn, 'NaN');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.includes(d.getMonth(), d, 'Date');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.includes(0, fn, 'Function');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
assert.includes(x, a, 'Array');
assert.includes(x, o, 'Object');
assert.includes('fox', str, 'non empty String');
});
test('assert.has()', function () {
var u = (function (_a) { return _a; }());
var o = {x: u};
var a = [u];
assert.has('x', o, 'Object key');
assert.has('0', a, 'Arry string key');
assert.has(0, a, 'Arry integer index');
try {
assert.has('k', u, 'undefined list');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('TypeError', err.name);
}
try {
assert.has('k', null, 'null list');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('TypeError', err.name);
}
try {
assert.has('k', 'k', 'string');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.has(1, 1, 'number');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
// Auto assigned members.
assert.has('length', a, '[].length');
assert.has('length', 'foo', '"foo".length');
});
test('assert.doesNotHave()', function () {
var u = (function (_a) { return _a; }());
var o = {x: u};
var a = [u];
var d = new Date();
try {
assert.doesNotHave('x', o, 'Object key fail');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
assert.doesNotHave('k', 0, 'Object key pass');
try {
assert.doesNotHave('0', a, 'Array key string fail');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.doesNotHave(0, a, 'Array index integer fail');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
assert.doesNotHave('1', a, 'Array key string pass');
assert.doesNotHave(1, a, 'Array index integer pass');
// Prototype members
assert.doesNotHave('toString', d, 'toString');
assert.doesNotHave('getMonth', d, 'getMonth');
});
test('assert.isGreaterThan()', function () {
var u = (function (_a) { return _a; }());
assert.isGreaterThan(0, 1, 'integer');
assert.isGreaterThan(0.1, 0.2, 'float');
assert.isGreaterThan('a', 'b', 'letter');
assert.isGreaterThan('A', 'a', 'case sensitive letter');
try {
assert.isGreaterThan(1, 1, 'same integer');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.isGreaterThan('a', 'a', 'same letter');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.isGreaterThan(null, u, 'null undefined');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
});
test('assert.isLessThan()', function () {
var u = (function (_a) { return _a; }());
assert.isLessThan(1, 0, 'integer');
assert.isLessThan(0.2, 0.1, 'float');
assert.isLessThan('b', 'a', 'letter');
assert.isLessThan('a', 'A', 'case sensitive letter');
try {
assert.isLessThan(1, 1, 'same integer');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.isLessThan('a', 'a', 'same letter');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.isLessThan(null, u, 'null undefined');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
});
test('assert.isNonEmptyString', function () {
var a = [0, 1];
var x = 'xxx';
var y = '';
try {
assert.isNonEmptyString(a, 'Array');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.isNonEmptyString(y, 'empty string');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
assert.isNonEmptyString(x, 'passing');
});
test('assert.isNumberNotNaN', function () {
var a = [0, 1];
var s = '1';
var nn = 1 / 'foo'; // NaN
try {
assert.isNumberNotNaN(a, 'Array');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.isNumberNotNaN(s, 'String');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
try {
assert.isNumberNotNaN(nn, 'NaN');
throw new AssertionError('Should have thrown!');
} catch (err) {
assert.isEqual('AssertionError', err.name);
}
assert.isNumberNotNaN(0, 'zero');
assert.isNumberNotNaN(1.1, 'float');
assert.isNumberNotNaN(Infinity, 'Infinity');
});
}(typeof require === 'undefined' ? KixxAssert : require('./kixx-assert')));