forked from titulus/test.it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testit.js
930 lines (861 loc) · 35.2 KB
/
testit.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
(function(scope) {
var rootTimeDone = false;
var testit = function() {
/**
* group class which will contain tests
* In addition it will be used to prevent 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 tests 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 spent 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 = 0;
this.result = {
pass: 0,
fail: 0,
error: 0,
total: 0
};
this.stack = [];
}
/**
* test class which will contain result and other 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 throw it without falling
* @attribute {Number} time time in ms spent on test
* @attribute {Array} argument 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.argument = [];
}
/**
* main group
* @public
* @type {group}
*/
var root = new group();
this.root = root;
root.name = 'root';
root.time = new Date().getTime();
/**
* make new instace of group, fill it, push it into stack of current level group
* @private
* @chainable
* @param {String} name name of new group
* @param {Function} fun function which will be executed (mainly consist of tests and other groups)
* @param {Boolean} excluded if true - newgroup will not be added into currentlevel stack;
* @return {Object} testit with link to new group
*/
var _makeGroup = function(name,fun,excluded) {
/** get timestamp */
var time = new Date().getTime();
/** var for the new instance of group */
var newgroup;
/** identify new group */
var groupAlreadyExist = false;
/** find group in current-level stack */
for (var i in this.stack) {
if (this.stack[i].type !== 'group') continue;
if (this.stack[i].name === name) {
newgroup = this.stack[i];
groupAlreadyExist = true;
break;
}
}
if (!groupAlreadyExist) newgroup = new group();
newgroup.name = name;
/** add backlink to provide trace */
newgroup.linkBack = this;
/** set to pass as default. it's may be changed in some next lines */
var oldstatus;
if (groupAlreadyExist) oldstatus = newgroup.status;
newgroup.status ='pass';
/**
* try to execute fun() with tests and other groups in
* This part provides nesting.
* For this reason there are redefinition of root.
*/
try {
var oldRoot = root;
root = newgroup;
fun();
root = oldRoot;
} catch(e) {
newgroup.status = 'error';
newgroup.error = generateError(e);
}
/** update time */
newgroup.time += new Date().getTime() - time;
/** finally place this group into previous level stack (if it's a new group) */
if (!groupAlreadyExist && !excluded) this.stack.push(newgroup);
/** update counters */
updateCounters(newgroup);
/** return testit with link to this group */
return newgroup;
}
/**
* return group by it's name in stack of current level group
* @private
* @param {String} name name of group which will be searched for
* @return {Object} group
*/
var _getGroup = function (name) {
var stack = this.stack;
for (var i in stack) {
if (stack[i].type !== 'group') continue;
if (stack[i].name === name) {
return stack[i];
}
}
throw new ReferenceError('there is no group with name: '+name);
}
/**
* Interface for _makeGroup() and _getGroup() methods.
* Definition, which method will be executed, depends on arguments number.
* @private
* @chainable chain-opener
* @param {String} name name of group
* @param {Function} fun function contains tests and other groups
* @return {Object} testit with link to specified group
*/
var _group = function(name,fun) {
/**
* Here may be 3 situation:
* this.link is root && root is root - test.group() called in root scope
* this.link is root && root is some group - test.group() called in some other group scope
* this.link is some group && root is root - .group() called in chain in root scope
* this.link is some group && root is some group - .group() called in chain in some other group scope
* look at it with:
* console.log(name,'\nlink: ',this.link,'\nroot: ',root);
*/
var currentLevel = (this.link)?this.link:root;
var linkToGroup;
switch (arguments.length) {
case 0 : throw new RangeError("test.group expect at least 1 argument");
case 1 : {
linkToGroup = _getGroup.call(currentLevel,name);
} break;
case 2 : {
linkToGroup = _makeGroup.call(currentLevel,name,fun,this.excluded);
} break;
default : throw new RangeError("test.group expect no more than 2 arguments");
}
/** get trace for this group */
var trace = getTrace();
return Object.create(this,{link:{value:linkToGroup},trace:{value:trace}});
}
/**
* public interface for _group
* @public
* @example
* test.group('name of group',function(){
* test.it('nested test');
* test.group('nested group',function(){
* test.it('deep nested test');
* });
* });
* test.group('name of group')
* .group('nested group',function(){
* test.it('additional deep nested test');
* });
*/
this.group = _group;
/**
* Base for all tests. Make new instance of test, fill it using test-functions, push it into stack of current level group
* @private
* @chainable chain-opener
* @param {String} type determines test-function which will be used
* @param {Array} args array of arguments
* @return {Object} testit with link to this test
*/
var _doTest = function (type,args) {
/**
* new instance of test
* Most of code in this function will manipulate with it.
*/
var newtest = new test();
/** fill newtest.agrument from method arguments */
for (var i in args) {
newtest.argument.push(args[i]);
}
/** execute test-function specified by type */
switch (type) {
case 'it' : _testIt(newtest); break;
case 'them' : _testThem(newtest); break;
case 'type' : _testType(newtest); break;
case 'types' : _testTypes(newtest); break;
}
/** calculate time, if .time was called before this test */
if (this.timestamp) newtest.time = new Date().getTime() - this.timestamp;
/** finally place this test into container stack */
if (!this.excluded) root.stack.push(newtest);
/** update counters of contained group */
updateCounters(root);
/** get trace for this test */
var trace = getTrace();
/** return testit with
* link to this test
* trace for this test
*/
return Object.create(this,{link:{value:newtest},trace:{value:trace}});
}
this.it = function(){return _doTest.call(this,'it',arguments)};
this.them = function(){return _doTest.call(this,'them',arguments)};
this.type = function(){return _doTest.call(this,'type',arguments)};
this.types = function(){return _doTest.call(this,'types',arguments)};
/**
* checks the argument for the true-like value
* @private
* @param {Object} testobj test object, wich will be filled with result
*/
var _testIt = function(testobj){
switch (testobj.argument.length) {
/** in case of no arguments - throw Reference error */
case 0 : {
testobj.status = 'error';
testobj.error = generateError(new RangeError("at least one argument expected"));
} break;
/** if there is only one argument - check it for truth */
case 1 : {
if (testobj.argument[0]) {
testobj.description = 'argument is true-like';
testobj.status = 'pass';
} else {
testobj.description = 'argument is false-like';
testobj.status = 'fail';
}
} break;
/** if there are two arguments - check equalence between them */
case 2 : {
if (_typeof(testobj.argument[0]) !== _typeof(testobj.argument[1])) {
testobj.description = 'argument hase different types';
testobj.status = 'fail';
} else if (deepCompare(testobj.argument[0],testobj.argument[1])) {
testobj.description = 'arguments are equal';
testobj.status = 'pass';
} else {
testobj.description = 'argument are not equal';
testobj.status = 'fail';
}
} break;
/** otherwise throw Range error */
default : {
testobj.status = 'error';
testobj.error = generateError(new RangeError("maximum of 2 arguments expected"));
}
}
}
/**
* checks array of values to be true-like
* @private
* @param {Object} testobj test object, wich will be filled with result
*/
var _testThem = function(testobj){
switch (testobj.argument.length) {
/** in case of no arguments - throw Reference error */
case 0 : {
testobj.status = 'error';
testobj.error = generateError(new RangeError("at least one argument expected"));
} break;
/** if there is only one argument - continue */
case 1 : {
/** if first argument is not an Array - throw TypeError */
if (_typeof(testobj.argument[0]) !== 'Array') {
testobj.status = 'error';
testobj.error = generateError(new TypeError("argument should be an array"));
} else {
/** check elements of array to be true-like */
for (var i in testobj.argument[0]) {
if (!testobj.argument[0][i]) {
testobj.status = 'fail';
testobj.description = 'there are at least one false-like element';
}
}
/** test passed if there are no false-like elements found */
if (testobj.status !== 'fail') {
testobj.status = 'pass';
testobj.description = 'arguments are true-like';
}
}
} break;
/** otherwise throw Range error */
default : {
testobj.status = 'error';
testobj.error = generateError(new RangeError("maximum of 1 arguments expected"));
}
}
}
/**
* checks type of value to be equal to specified
* @private
* @param {Object} testobj test object, wich will be filled with result
*/
var _testType = function(testobj) {
if (testobj.argument.length!==2) {
testobj.status = 'error';
testobj.error = generateError(new RangeError("expect two arguments"));
} else if (_typeof(testobj.argument[1]) !== 'String') {
testobj.status = 'error';
testobj.error = generateError(new TypeError("second argument should be a String"));
} else if (!arrayConsist(identifiedTypes,testobj.argument[1].toLowerCase())) {
testobj.status = 'error';
testobj.error = generateError(new TypeError("second argument should be a standart type"));
} else {
testobj.description = 'type of argument is ';
if (_typeof(testobj.argument[0]).toLowerCase() !== testobj.argument[1].toLowerCase()) {
testobj.description += 'not '+testobj.argument[1];
testobj.status = 'fail';
} else {
testobj.description += _typeof(testobj.argument[0]);
testobj.status = 'pass';
}
}
}
/**
* checks types of elements in array to be equal to specified and between each other
* @private
* @param {Object} testobj test object, wich will be filled with result
*/
var _testTypes = function(testobj) {
if (testobj.argument.length==0) {
testobj.status = 'error';
testobj.error = generateError(new RangeError("at least one argument expected"));
} else if (testobj.argument.length>2) {
testobj.status = 'error';
testobj.error = generateError(new RangeError("maximum of two arguments expected"));
} else if (_typeof(testobj.argument[0]) !== 'Array') {
testobj.status = 'error';
testobj.error = generateError(new TypeError("first argument should be an array"));
} else {
var type, types;
if (_typeof(testobj.argument[1]) === 'undefined') {
type = _typeof(testobj.argument[0][0]);
types = 'same';
} else if (_typeof(testobj.argument[1]) !== 'String') {
testobj.status = 'error';
testobj.error = generateError(new TypeError("second argument should be a String"));
} else if (!arrayConsist(identifiedTypes,testobj.argument[1].toLowerCase())) {
testobj.status = 'error';
testobj.error = generateError(new TypeError("second argument should be a standart type"));
} else {
type = testobj.argument[1];
types = 'right';
}
if (testobj.status !== 'error') {
type = type.toLowerCase();
for (var i in testobj.argument[0]) {
if (_typeof(testobj.argument[0][i]).toLowerCase() !== type) {
testobj.status = 'fail';
testobj.description = 'There are at least one element with different type';
}
}
if (testobj.status !== 'fail') {
testobj.status = 'pass';
testobj.description = 'arguments are '+types+' type';
}
}
}
}
/**
* adds the time spent on test, into his result
* @private
* @chainable chain-preparatory
*/
var _time = Object.create(this,{timestamp:{value:new Date().getTime()}});
/**
* public interface for _time
* @public
* @example
* test.time.it(someThing());
*/
this.time = _time;
/**
* makes test/group unpushable into stack of current level group
* @private
* @chainable chain-preparatory
*/
var _exclude = Object.create(this,{excluded:{value:true}});
/**
* public interface for _exclude
* @public
* @example
* test.exclude.it(someThing).done();
* test.exclude.group('some group',function(){ ... }).done();
*/
this.exclude = _exclude;
/**
* add a comment for the linked test or group
* @private
* @chainable chain-link
* @type {Function}
* @param {String} text user defined text, which will be used as a comment
*/
var _comment = function(text) {
/** add a comment, if there is something can be commented */
if (!this.link) throw new ReferenceError('comment can only be used in testit chain');
this.link.comment = text;
return this;
}
/**
* public interface for _comment()
* @public
* @example
* test.group('group name',function(){
* test
* .it(someThing)
* .comment('comment to test');
* }).comment('comment to group');
*/
this.comment = _comment;
/**
* try to execute functions in arguments depending on test|group result
* @private
* @chainable chain-link
* @param {Function} pass function to execute if test|group pass
* @param {Function} fail function to execute if test|group fail
* @param {Function} error function to execute if test|group cause an error
*/
var _callback = function(pass,fail,error) {
if (!this.link) throw new ReferenceError('callback can only be used in testit chain');
if (this.link.status === 'pass' && _typeof(pass) === 'Function' ) try {pass();} catch(e) {throw e;}
if (this.link.status === 'fail' && _typeof(fail) === 'Function' ) try {fail();} catch(e) {throw e;}
if (this.link.status === 'error' && _typeof(error) === 'Function' ) try {error();} catch(e) {throw e;}
return this;
}
/**
* public interface for _callback()
* @public
* @example
* test.it(someThing).callback(
* function() {...} // - will be execute if test pass
* ,function() {...} // - will be execute if test fail
* ,function() {...} // - will be execute if test cause an error
* );
*/
this.callback = _callback;
/**
* add trace to test/group
* @private
* @chainable chain-link
* @param {Number} level Number of trace lines which will be added
*/
var _addTrace = function(level) {
if (!this.link) throw new ReferenceError('addTrace can only be used in testit chain');
if (this.trace) {
var trace = this.trace
if (_typeof(level) === 'Number') trace = trace.split('\n').slice(0,level+1).join('\n');
this.link.trace = trace;
}
return this;
}
/**
* public interface for _addTrace()
* @public
* @example
* test.it(someThing).addTrace(); // add full trace
* test.it(someThing).addTrace(0); // add only first line of trace
*/
this.addTrace = _addTrace;
/**
* Final chain-link: returns result of test or group
* @private
* @return {boolean} true - if test or group passed, false - otherwise.
*/
var _result = function() {
if (this.link) {
return (this.link.status == 'pass')? true : false;
}
return undefined;
}
/**
* public interface for _result()
* @public
* @example
* var testResult = test.it(undefined).comment('comment to test').result(); // testResult === false
*/
this.result = _result;
/**
* Final chain-link: returns arguments of test (not of group!)
* @private
* @return single argument or array of arguments
*/
var _arguments = function() {
if (this.link) {
if (this.link.type!=='test') return TypeError('groups don\'t return arguments');
switch (this.link.argument.length) {
case 0 : return undefined
case 1 : return this.link.argument[0];
default : return this.link.argument;
}
}
return undefined;
}
/**
* public interface for _arguments()
* @public
* @example
* var testArguments = test.it('single').comment('comment to test').arguments(); // testArguments === 'single'
* testArguments = test.it('first','second').comment('comment to test').arguments(); // testArguments === ['first','second']
*/
this.arguments = _arguments;
/**
* apply last stuff and display result
* type {Function}
* @private
*/
var _done = function() {
/** update time in root */
if (!rootTimeDone && root.name==='root') {
root.time = new Date().getTime() - root.time;
rootTimeDone = true;
}
curentLevel = (this.link)?this.link:root;
/** display result */
_printConsole(curentLevel);
}
/**
* public interface for _done()
* @type {Function}
* @public
* @example
* test.it(1);
* test.it(2);
* test.it(3);
*
* test.done();
*/
this.done = _done;
/** update counters of contained object */
var updateCounters = function(link) {
link.result = {
pass: 0,
fail: 0,
error: 0,
total: 0
};
for (var i in link.stack) {
link.result.total++;
switch (link.stack[i].status) {
case 'pass' : {
link.result.pass++;
} break;
case 'fail' : {
link.result.fail++;
} break;
case 'error' : {
link.result.error++;
} break;
};
};
if (link.result.error || link.error) {link.status='error'}
else if (link.result.fail) {link.status='fail'}
else {link.status='pass'}
if (link.linkBack) {
updateCounters(link.linkBack);
}
}
/**
* prittify display of 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; font-weight:normal;";
/** Try to figure out what type of object display and open group */
switch (obj.type) {
case 'group' : {
/** different behavior depending on status */
switch (obj.status) {
/** if object have passed - make collapsed group*/
case 'pass' : {
console.groupCollapsed("%s - %c%s%c - %c%d%c/%c%d%c/%c%d%c (%c%d%c ms) %s"
,obj.name,green,obj.status,normal
,green,obj.result.pass,normal
,red,obj.result.fail,normal
,orange,obj.result.error,normal
,blue,obj.time,normal,((obj.comment)?obj.comment:''));
} break;
case 'fail' : {
console.group("%s - %c%s%c - %c%d%c/%c%d%c/%c%d%c (%c%d%c ms) %s"
,obj.name,red,obj.status,normal
,green,obj.result.pass,normal
,red,obj.result.fail,normal
,orange,obj.result.error,normal
,blue,obj.time,normal,((obj.comment)?obj.comment:''));
} break;
case 'error' : {
console.group("%s - %c%s%c - %c%d%c/%c%d%c/%c%d%c (%c%d%c ms) %s"
,obj.name,orange,obj.status,normal
,green,obj.result.pass,normal
,red,obj.result.fail,normal
,orange,obj.result.error,normal
,blue,obj.time,normal,((obj.comment)?obj.comment:''));
} 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 trace if defined */
if (obj.trace) {
console.log(obj.trace);
}
/**
* display all tests and groups in stack
* It will make new levels of group, if there are groups in stack.
*/
for (var i in obj.stack) {
_printConsole(obj.stack[i]);
}
/** display error if defined */
if (obj.error) {
// console.error(obj.error);
console.group('%c%s%c: %s',orange,obj.error.type,normal,obj.error.message);
if (obj.error.stack) console.log(obj.error.stack);
console.dir(obj.error.error);
console.groupEnd();
}
/** close opened group (current level) */
console.groupEnd();
} break;
case 'test' : {
/** display different results depending on status */
switch (obj.status) {
case 'pass' : {
/** if pass - collapse group*/
console.groupCollapsed("%cpass%c: %s%s%c%s%c%s",green,normal
,(obj.comment)?obj.comment:''
,(obj.time)?' (':''
,(obj.time)?blue:''
,(obj.time)?obj.time:''
,(obj.time)?normal:''
,(obj.time)?' ms)':'');
} break;
case 'fail' : {
console.group("%cfail%c: %s",red,normal
,(obj.comment)?obj.comment:''
,(obj.time)?' (':''
,(obj.time)?blue:''
,(obj.time)?obj.time:''
,(obj.time)?normal:''
,(obj.time)?' ms)':'');
} break;
case 'error' : {
console.group("%cerror%c: %s",orange,normal
,(obj.comment)?obj.comment:''
,(obj.time)?' (':''
,(obj.time)?blue:''
,(obj.time)?obj.time:''
,(obj.time)?normal:''
,(obj.time)?' ms)':'');
} break;
}
/** display description if defined */
if (obj.description) console.log(obj.description);
/** display trace if defined */
if (obj.trace) {
console.log(obj.trace);
}
/** display error if defined */
if (obj.error) {
// console.error(obj.error);
console.group('%c%s%c: %s',orange,obj.error.type,normal,obj.error.message);
if (obj.error.stack) console.log(obj.error.stack);
console.dir(obj.error.error);
console.groupEnd();
}
console.log(obj.argument);
console.groupEnd();
} break;
}
}
/**
* public interface for _printConsole
* @type {Function}
* @public
* @example
* test.ptint(test.root);
*/
this.print = _printConsole;
/**
* determinates type of argument
* More powerfull then typeof().
* @private
* @return {String} type name of the argument
* undefined, if type was not determinated
*/
var _typeof = function (argument) {
var type;
try {
switch (argument.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(argument))?'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='HTML';break;
case NodeList : type='NodeList';break;
default : {
if (typeof argument === 'object'
&& argument.toString().indexOf('HTML') !== -1) {
type = 'HTML';
} else {
type = undefined;
}
}
}
} catch (e) {
type = (argument === null)? 'null' : typeof argument;
}
return type;
}
/**
* public interface for _typeof
* @public
* @example
* test.typeof(myVar);
*/
this.typeof = _typeof;
/** list of types, which can be identified by _typeof */
var identifiedTypes = ['array', 'boolean', 'date', 'error', 'evalerror', 'function', 'html', 'nan', 'nodelist', 'null', 'number', 'object', 'rangeerror', 'referenceerror', 'regexp', 'string', 'syntaxerror', 'typeerror', 'urierror', 'window'];
/**
* public interface for getTrace(error)
* @public
* @example
* test.trace();
*/
this.trace = getTrace;
// return this;
return this;
}
/**
* figure out what status will be used
* Depends on significance:
* 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
*/
function updateStatus(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';
}
/**
* make the error object more clear and returns it
* @param {Error} error basic error
* @return {Object} clear error object
*/
function generateError(error) {
/**
* understandable error object
* @property {Error} error consist basic error
* @property {String} type type of error
* @property {String} message message from basic property
* @property {String} stack result of trace()
*/
var object = {
error: error,
type: test.typeof(error),
message: error.message,
}
if (getTrace(error)) object.stack = getTrace(error);
return object;
}
/**
* returns a list of functions that have been performed to call the current line
* @param {Error} error if set, trace will be based on its stack
* @return {String} list of functions joined by "\n";
* undefined if error.stack is not supported.
*/
function getTrace(error) {
if (!error) error = new Error();
if (!error.stack) return;
var stack = '';
error.stack.split(/[\n]/).forEach(function(i,n){
var addToStack = true;
/** take off empty strings (FireBug) */
if (i==='') addToStack = false;
/** take off Errors (Chrome) */
if (i.indexOf(test.typeof(error))!==-1) addToStack = false;
/** take of reference to this function */
if (i.indexOf('getTrace')!==-1) addToStack = false;
/** take off any references to testit lines */
if (i.indexOf('/testit.')!==-1) addToStack = false;
/** fill the stack */
if (addToStack) {
stack += (stack)?'\n':'';
stack += i.replace(/((\s+at\s+)|(^@))/,'');
}
})
return stack;
}
/**
* Compares 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}
/**
* finds val in array
* @param {Array} array where to search
* @param val what to search for
* @return {Boolean} true if found, false otherwise
*/
arrayConsist = function(array, val) {
for (var i in array) if (array[i] === val) return true;
return false;
}
/**
* new instance of testit, availible from outside.
*/
scope.test = new testit();
})(this);