forked from titulus/test.it
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestit.js
More file actions
498 lines (461 loc) · 18 KB
/
testit.js
File metadata and controls
498 lines (461 loc) · 18 KB
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
(function(window) {
var testit = function() {
/**
* group class, which will contain tests
* In addition, it will be used for wrapping some wrong code from falling.
* @constructor
* @private
* @attribute {String} type type of object ('group' or 'test')
* @attribute {String} name name of group
* @attribute {String} status indicate results of all test in group ('pass','fail','error')
* @attribute {String} comment text specified by user
* @attribute {Error} error contain error object if some of tests throw it
* @attribute {Number} time time in ms spend on code in group
* @attribute {Object} result counters for tests and groups
* @attribute {array} stack array of tests and groups
*/
var group = function() {
this.type = 'group';
this.name = undefined;
this.status = undefined;
this.comment = undefined;
this.error = undefined;
this.time = new Date().getTime();
this.result = {
tests: {
passed: 0,
failed: 0,
error: 0,
total: 0
},
groups: {
passed: 0,
failed: 0,
error: 0,
total: 0
}
};
this.stack = [];
}
/**
* test class, which will contain result and some more info about one test
* @constructor
* @private
* @attribute {String} type type of object ('group' or 'test')
* @attribute {String} status indicate results of test ('pass','fail','error')
* @attribute {String} comment text specified by user
* @attribute {String} description text generated by script
* @attribute {Error} error contain error object if test can throw it without falling
* @attribute {Number} time time in ms spend on test
* @attribute {Array} entity all received arguments
*/
var test = function() {
this.type = 'test';
this.status = undefined;
this.comment = undefined;
this.description = undefined;
this.error = undefined;
this.time = new Date().getTime();
this.entity = [];
}
/**
* main group
* @public
* @type {group}
*/
var root = new group();
this.root = root;
root.name = 'root';
/**
* make new instace of group, fill it, add it to previous group.stack, fill some values in previous group
* @private
* @type {Function}
* @param {String} name name of new group
* @param {Function} fun function witch will be tryed to execute (commonly consist of tests and other groups)
*/
var _makeGroup = function(name,fun) {
/**
* making a new instance of group
* Most of code in this function will manipulate whis it.
*/
var newgroup = new group();
newgroup.name = name;
/** set to pass as default. it's may be changed in some next lines */
newgroup.status ='pass';
/**
* making a new root, to provide nesting
* Nested tests and groups will us it, like first one use root.
*/
var oldRoot = root;
root = newgroup;
/**
* try to execute code with tests and other groups in it
* This part provide nesting.
*/
try{fun();} catch(e) {
newgroup.status = 'error';
newgroup.error = e;
}
/**
* reverse inheritance of status
* If some of deep nested test will 'fail', root will be 'fail' too.
* More info in updateStatus() comments.
*/
oldRoot.status = updateStatus(oldRoot.status,root.status);
/**
* take back old root
* Next code do not need nesting.
*/
root = oldRoot;
/** update counters */
switch (newgroup.status) {
case 'pass' : {
root.result.groups.passed++;
} break;
case 'fail' : {
root.result.groups.failed++;
} break;
case 'error' : {
root.result.groups.error++;
} break;
}
root.result.groups.total++;
/** update time */
newgroup.time = new Date().getTime() - newgroup.time;
/** finally place this group into previous level stack */
root.stack.push(newgroup);
}
/**
* public interface for _makeGroup
* @public
* @type {Function}
* @example
* test.group('name of group',function(){
* test.it('nested test');
* test.group('nested group',function(){
* test.it('deep nested test');
* });
* });
*/
this.group = _makeGroup;
/**
* basic test. Make new instance of test, fill it, add it to previous group.stack, fill some values in previous group
* @private
* @type {Function}
* @param {Multiple} a @required first entity, which will check for truth it only transmitted
* @param {Multiple} b second entity which will compared with a if transmitted
* @return {Boolean} true if 'pass', fail otherwise
*/
var _it = function(a,b) {
/**
* making a new instance of test
* Most of code in this function will manipulate whis it.
*/
var newtest = new test();
/**
* fill newtest.entity with arguments
* (arguments is array-like object, but not array. So i can't just newtest.entity = newtest.entity.concat(arguments); or newtest.entity = arguments)
*/
for (i in arguments) {
newtest.entity.push(arguments[i]);
}
/** try to figure out what kind of test expected */
switch (arguments.length) {
/** in case of no arguments - throw Reference error */
case 0 : {
newtest.status = 'error';
newtest.error = new ReferenceError("at least one argument expected");
} break;
/** if there only one argument - test it for truth */
case 1 : {
newtest.description = 'argument exist and not false';
if (a) {
newtest.status = 'pass';
} else {
newtest.status = 'fail';
}
} break;
/** if there are two arguments - test equalence between them */
case 2 : {
newtest.description = 'arguments are equal';
if (_typeof(a) !== _typeof(b)) {
newtest.status = 'fail';
} else {
/*switch (_typeof(a)) {
case 'array' : {} break;
case 'object' : {} break;
case 'regexp' : {} break;
case 'dom' : {} break;
case 'nodelist' : {} break;
default : {
newtest.status = (a===b);
}
}*/
newtest.status = (deepCompare(a,b))? 'pass' : 'fail';
}
} break;
/** otherwise throw Range error */
default : {
newtest.status = 'error';
newtest.error = new RangeError("too much arguments");
}
}
/** update counters of contained object */
switch (newtest.status) {
case 'pass' : {
root.result.tests.passed++;
} break;
case 'fail' : {
root.result.tests.failed++;
} break;
case 'error' : {
root.result.tests.error++;
} break;
}
root.result.tests.total++;
/** reverse inheritance of status */
root.status = updateStatus(root.status,newtest.status);
/** update time */
newtest.time = new Date().getTime() - newtest.time;
/** finally place this test into container stack */
root.stack.push(newtest);
return (newtest.status==='pass')? true:false;
}
/**
* public interface for _it()
* @public
* @type {Function}
* @example
* test.it(myFunction());
* test.it(myVar>5);
* test.it(myVar,mySecondVar);
*/
this.it = _it;
/**
* add comment for the last test or group in current stack
* @private
* @type {Function}
* @param {String} text user defined text, which will be used as a comment
*/
var _comment = function(text) {
/** add comment, if there are something can be commented */
if (root.stack.length) root.stack[root.stack.length-1].comment = text;
}
/**
* public interface for _comment()
* @public
* @type {Function}
* @example
* test.group('group name',function(){
* test.it(myFunction());
* test.comment('comment to test');
* });
* test.comment('comment to group');
*/
this.comment = _comment;
/**
* apply last stuff and display results
* type {Function}
* @private
*/
var _done = function(obj) {
/** update time in root */
root.time = new Date().getTime() - root.time;
/** display root */
// console.dir(root);
_printConsole(root);
}
/**
* public interface for _done()
* @type {Function}
* @public
* @example
* test.it(1);
* test.it(2);
* test.it(3);
*
* test.done();
*/
this.done = _done;
/**
* pritty display group or test in browser dev console
* @private
* @param {Object} obj group or test to display
*/
var _printConsole = function(obj) {
/** colors for console.log %c */
var green = "color: green",
red = "color: red;",
orange = "color: orange",
blue = "color: blue",
normal = "color: normal";
/** Try to figure out what type of object display and open group */
switch (obj.type) {
case 'group' : {
/** some difference depends on status */
switch (obj.status) {
/** if object passed - make collapsed group*/
case 'pass' : {
console.groupCollapsed("%s - %cpass",obj.name,green);
} break;
case 'fail' : {
console.group("%s - %cfail",obj.name,red);
} break;
case 'error' : {
console.group("%s - %cerror",obj.name,orange);
} break;
/** if status is not defined - display error; finish displaying */
default : {
console.error("No status in object %s",obj.name);
return false;
}
}
/** display description if defined */
if (obj.description) {
console.log(obj.description);
}
/** display comment if defined */
if (obj.comment) {
console.log(obj.comment);
}
/** display result counters */
console.log("tests: %cpass%c %d, %cfail%c %d, %cerror%c %d\ngroups: %cpass%c %d, %cfail%c %d, %cerror%c %d"
,green,normal,obj.result.tests.passed
,red,normal,obj.result.tests.failed
,orange,normal,obj.result.tests.error
,green,normal,obj.result.groups.passed
,red,normal,obj.result.groups.failed
,orange,normal,obj.result.groups.error);
/** display time */
console.log("time: %c%d%c ms",blue,obj.time,normal);
/** display error if defined */
if (obj.error) {
console.error(obj.error);
}
/**
* display all tests and groups in stack
* It will make new levels of group, if there are groups in stack.
*/
for (i in obj.stack) {
_printConsole(obj.stack[i]);
}
/** close opened group (current level) */
console.groupEnd();
} break;
case 'test' : {
/** display different results, depend on status */
switch (obj.status) {
case 'pass' : {
/** if pass - collaps group*/
console.groupCollapsed("%cpass%c: %s",green,normal,(obj.comment)?obj.comment:'no comment');
console.log("%s\n%o"
,(obj.description)?obj.description:'no description'
,obj.entity);
console.groupEnd();
} break;
case 'fail' : {
console.group("%cfail%c: %s",red,normal,(obj.comment)?obj.comment:'no comment');
console.log("%s\n%o"
,(obj.description)?obj.description:'no description'
,obj.entity);
console.groupEnd();
} break;
case 'error' : {
console.group("%cerror%c: %s",orange,normal,(obj.comment)?obj.comment:'no comment');
console.log("%s\n%o"
,(obj.description)?obj.description:'no description'
,obj.error);
console.groupEnd();
} break;
}
} break;
}
}
/**
* public interface for _printConsole
* @type {Function}
* @public
* @example
* test.ptint(test.root);
*/
this.print = _printConsole;
/**
* determinate type of entity
* More powerfull then typeof().
* @private
* @return {String} type name of entity
* undefined, if type was not determinated
*/
var _typeof = function (entity) {
var type;
try {
switch (entity.constructor) {
case Array : type='array';break;
case Boolean : type='boolean';break;
case Date : type='date';break;
case Error : type='error';break;
case EvalError : type='evalerror';break;
case Function : type='function';break;
// case Math : type='math';break;
case Number : {type=(isNaN(entity))?'nan':'number';}break;
case Object : type='object';break;
case RangeError : type='rangeerror';break;
case ReferenceError : type='referenceerror';break;
case RegExp : type='regexp';break;
case String : type='string';break;
case SyntaxError : type='syntaxerror';break;
case TypeError : type='typeerror';break;
case URIError : type='urierror';break;
case Window : type='window';break;
case HTMLDocument : type='dom';break;
case NodeList : type='nodelist';break;
default : {
if (typeof entity === 'object'
&& entity.toString().indexOf('HTML') !== -1) {
type = 'dom';
} else {
type = undefined;
}
}
}
} catch (e) {
type = (entity === null)? 'null' : typeof entity;
}
return type;
}
/**
* public interface for _typeof
* @public
* @example
* test.typeof(myVar);
*/
this.typeof = _typeof;
}
/**
* figure out what status will be used
* Depends on significanse:
* More significant -> less significant.
* error -> fail -> pass -> undefined
* @param {String} oldstatus first compared status
* @param {String} newstatus second compared status
* @return {String} status which will be set
*/
var updateStatus = function(oldstatus,newstatus) {
if (oldstatus===undefined) return newstatus;
if (newstatus===undefined) return oldstatus;
if (oldstatus==='error' || newstatus==='error') return 'error';
if (oldstatus==='fail' || newstatus==='fail') return 'fail';
return 'pass';
}
/**
* Compare any type of variables
* @return {Boolean} result of comparison
* {@link http://stackoverflow.com/a/1144249/1771942}
*/
function deepCompare(){function c(d,e){var f;if(isNaN(d)&&isNaN(e)&&"number"==typeof d&&"number"==typeof e)return!0;if(d===e)return!0;if("function"==typeof d&&"function"==typeof e||d instanceof Date&&e instanceof Date||d instanceof RegExp&&e instanceof RegExp||d instanceof String&&e instanceof String||d instanceof Number&&e instanceof Number)return d.toString()===e.toString();if(!(d instanceof Object&&e instanceof Object))return!1;if(d.isPrototypeOf(e)||e.isPrototypeOf(d))return!1;if(d.constructor!==e.constructor)return!1;if(d.prototype!==e.prototype)return!1;if(a.indexOf(d)>-1||b.indexOf(e)>-1)return!1;for(f in e){if(e.hasOwnProperty(f)!==d.hasOwnProperty(f))return!1;if(typeof e[f]!=typeof d[f])return!1}for(f in d){if(e.hasOwnProperty(f)!==d.hasOwnProperty(f))return!1;if(typeof e[f]!=typeof d[f])return!1;switch(typeof d[f]){case"object":case"function":if(a.push(d),b.push(e),!c(d[f],e[f]))return!1;a.pop(),b.pop();break;default:if(d[f]!==e[f])return!1}}return!0}var a,b;if(arguments.length<1)return!0;for(var d=1,e=arguments.length;e>d;d++)if(a=[],b=[],!c(arguments[0],arguments[d]))return!1;return!0}
/**
* make new instance of testit
* Make it availible from outside.
*/
window.test = new testit();
})(window)