This repository has been archived by the owner on Mar 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
arraytests.js
806 lines (654 loc) · 24.4 KB
/
arraytests.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
// ==========================================================================
// Project: SproutCore Unit Testing Library
// Copyright: ©2006-2010 Sprout Systems, Inc. and contributors.
// Portions ©2008-2010 Apple Inc. All rights reserved.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
/*globals global require CoreTest */
var fs = require('fs');
/** @namespace
CoreTest is the unit testing library for SproutCore. It includes a test
runner based on QUnit with some useful extensions for testing SproutCore-
based applications.
You can use CoreTest just like you would use QUnit in your tests directory.
*/
CoreTest = {
/**
Empty function. Useful for some operations.
*/
K: function() { return this; },
/**
Copied from SproutCore Runtime Core. Included here to avoid dependencies.
@param obj {Object} the object to beget
@returns {Object} the new object.
*/
beget: function(obj) {
if (!obj) return null ;
var K = CoreTest.K; K.prototype = obj ;
var ret = new K();
K.prototype = null ; // avoid leaks
return ret ;
},
/**
Copied from SproutCore Runtime Core. Included here to avoid dependencies.
@param target {Object} the target object to extend
@param properties {Object} one or more objects with properties to copy.
@returns {Object} the target object.
@static
*/
mixin: function() {
// copy reference to target object
var target = arguments[0] || {};
var idx = 1;
var length = arguments.length ;
var options ;
// Handle case where we have only one item...extend CoreTest
if (length === 1) {
target = this || {};
idx=0;
}
for ( ; idx < length; idx++ ) {
if (!(options = arguments[idx])) continue ;
for(var key in options) {
if (!options.hasOwnProperty(key)) continue ;
var src = target[key];
var copy = options[key] ;
if (target===copy) continue ; // prevent never-ending loop
if (copy !== undefined) target[key] = copy ;
}
}
return target;
},
/** Borrowed from SproutCore Runtime Core */
fmt: function(str) {
// first, replace any ORDERED replacements.
var args = arguments;
var idx = 1; // the current index for non-numerical replacements
return str.replace(/%@([0-9]+)?/g, function(s, argIndex) {
argIndex = (argIndex) ? parseInt(argIndex,0) : idx++ ;
s =args[argIndex];
return ((s===null) ? '(null)' : (s===undefined) ? '' : s).toString();
}) ;
},
/**
Returns a stub function that records any passed arguments and a call
count. You can pass no parameters, a single function or a hash.
If you pass no parameters, then this simply returns a function that does
nothing but record being called.
If you pass a function, then the function will execute when the method is
called, allowing you to stub in some fake behavior.
If you pass a hash, you can supply any properties you want attached to the
stub function. The two most useful are "action", which is the function
that will execute when the stub runs (as if you just passed a function),
and "expect" which should evaluate the stub results.
In your unit test you can verify the stub by calling stub.expect(X),
where X is the number of times you expect the function to be called. If
you implement your own test function, you can actually pass whatever you
want.
Calling stub.reset() will reset the record on the stub for further
testing.
@param {String} name the name of the stub to use for logging
@param {Function|Hash} func the function or hash
@returns {Function} stub function
*/
stub: function(name, func) {
// normalize param
var attrs = {};
if (typeof func === "function") {
attrs.action = func;
} else if (typeof func === "object") {
attrs = func ;
}
// create basic stub
var ret = function() {
ret.callCount++;
// get arguments into independent array and save in history
var args = [], loc = arguments.length;
while(--loc >= 0) args[loc] = arguments[loc];
args.unshift(this); // save context
ret.history.push(args);
return ret.action.apply(this, arguments);
};
ret.callCount = 0 ;
ret.history = [];
ret.stubName = name ;
// copy attrs
var key;
for(key in attrs) {
if (!attrs.hasOwnProperty(key)) continue ;
ret[key] = attrs[key];
}
// add on defaults
if (!ret.reset) {
ret.reset = function() {
this.callCount = 0;
this.history = [];
};
}
if (!ret.action) {
ret.action = function() { return this; };
}
if (!ret.expect) {
ret.expect = function(callCount) {
if (callCount === true) {
ok(this.callCount > 0, CoreTest.fmt("%@ should be called at least once", this.stubName));
} else {
if (callCount === false) callCount = 0;
equals(this.callCount, callCount, CoreTest.fmt("%@ should be called X times", this.stubName));
}
};
}
return ret ;
},
/** Test is OK */
OK: 'passed',
/** Test failed */
FAIL: 'failed',
/** Test raised exception */
ERROR: 'errors',
/** Test raised warning */
WARN: 'warnings',
showUI : false
};
/**
* jsDump
* Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
* Licensed under BSD (http://www.opensource.org/licenses/bsd-license.php)
* Date: 5/15/2008
* @projectDescription Advanced and extensible data dumping for Javascript.
* @version 1.0.0
* @author Ariel Flesler
* @link {http://flesler.blogspot.com/2008/05/jsdump-pretty-dump-of-any-javascript.html}
*/
(function(){
var reName, jsDump;
function quote( str ){
return '"' + str.toString().replace(/"/g, '\\"') + '"';
}
function literal( o ){
return o + '';
}
function join( pre, arr, post ){
var s = jsDump.separator(),
base = jsDump.indent(),
inner = jsDump.indent(1);
if( arr.join ) arr = arr.join( ',' + s + inner );
if( !arr ) return pre + post;
return [ pre, inner + arr, base + post ].join(s);
}
function array( arr ){
var i = arr.length, ret = new Array(i);
this.up();
while( i-- ) ret[i] = this._parse( arr[i] );
this.down();
return join( '[', ret, ']' );
}
reName = /^function (\w+)/;
jsDump = CoreTest.jsDump = {
parse: function(obj, type) {
if (obj && obj.toString) {
var toString = obj.toString;
if ((toString !== Object.prototype.toString) && (toString !== Array.toString)) return obj.toString();
}
if (obj && obj.inspect) return obj.inspect();
this.seen = [];
var ret = this._parse(obj, type);
this.seen = null;
return ret ;
},
//type is used mostly internally, you can fix a (custom)type in advance
_parse: function( obj, type ) {
var parser = this.parsers[ type || this.typeOf(obj) ];
type = typeof parser;
// avoid recursive loops
if ((parser === this.parsers.object) && (this.seen.indexOf(obj)>=0)) {
return '(recursive)';
}
this.seen.push(obj);
return type == 'function' ? parser.call( this, obj ) :
type == 'string' ? parser :
this.parsers.error;
},
typeOf:function( obj ){
var type = typeof obj,
f = 'function';//we'll use it 3 times, save it
if (obj && (obj.isObject || obj.isClass)) return 'scobj';
return type != 'object' && type != f ? type :
!obj ? 'null' :
obj.exec ? 'regexp' :// some browsers (FF) consider regexps functions
obj.getHours ? 'date' :
obj.scrollBy ? 'window' :
obj.nodeName == '#document' ? 'document' :
obj.nodeName ? 'node' :
obj.item ? 'nodelist' : // Safari reports nodelists as functions
obj.callee ? 'arguments' :
obj.call || obj.constructor != Array && //an array would also fall on this hack
(obj+'').indexOf(f) != -1 ? f : //IE reports functions like alert, as objects
'length' in obj ? 'array' :
type;
},
separator:function(){
return this.multiline ? this.HTML ? '<br />' : '\n' : this.HTML ? ' ' : ' ';
},
indent:function( extra ){// extra can be a number, shortcut for increasing-calling-decreasing
if( !this.multiline ) return '';
var chr = this.indentChar;
if( this.HTML ) chr = chr.replace(/\t/g,' ').replace(/ /g,' ');
return (new Array( this._depth_ + (extra||0) )).join(chr);
},
up:function( a ){
this._depth_ += a || 1;
},
down:function( a ){
this._depth_ -= a || 1;
},
setParser:function( name, parser ){
this.parsers[name] = parser;
},
// The next 3 are exposed so you can use them
quote:quote,
literal:literal,
join:join,
//
_depth_: 1,
// This is the list of parsers, to modify them, use jsDump.setParser
parsers:{
window: '[Window]',
document: '[Document]',
error:'[ERROR]', //when no parser is found, shouldn't happen
unknown: '[Unknown]',
'null':'null',
'undefined':'undefined',
'function':function( fn ){
var ret = 'function',
name = 'name' in fn ? fn.name : (reName.exec(fn)||[])[1];//functions never have name in IE
if( name ) ret += ' ' + name;
ret += '(';
ret = [ ret, this._parse( fn, 'functionArgs' ), '){'].join('');
return join( ret, this._parse(fn,'functionCode'), '}' );
},
array: array,
nodelist: array,
'arguments': array,
scobj: function(obj) { return obj.toString(); },
object:function( map ){
var ret = [ ];
this.up();
for( var key in map ) {
ret.push( this._parse(key,'key') + ': ' + this._parse(map[key]) );
}
this.down();
return join( '{', ret, '}' );
},
node:function( node ){
var open = this.HTML ? '<' : '<',
close = this.HTML ? '>' : '>';
var tag = node.nodeName.toLowerCase(),
ret = open + tag;
for( var a in this.DOMAttrs ){
var val = node[this.DOMAttrs[a]];
if( val ) {
ret += ' ' + a + '=' + this._parse( val, 'attribute' );
}
}
return ret + close + open + '/' + tag + close;
},
functionArgs:function( fn ){//function calls it internally, it's the arguments part of the function
var l = fn.length;
if( !l ) return '';
var args = new Array(l);
while( l-- ) args[l] = String.fromCharCode(97+l);//97 is 'a'
return ' ' + args.join(', ') + ' ';
},
key:quote, //object calls it internally, the key part of an item in a map
functionCode:'[code]', //function calls it internally, it's the content of the function
attribute:quote, //node calls it internally, it's an html attribute value
string:quote,
date:quote,
regexp:literal, //regex
number:literal,
'boolean':literal
},
DOMAttrs:{//attributes to dump from nodes, name=>realName
id:'id',
name:'name',
'class':'className'
},
HTML:true,//if true, entities are escaped ( <, >, \t, space and \n )
indentChar:' ',//indentation unit
multiline:true //if true, items in a collection, are separated by a \n, else just a space.
};
CoreTest.dump = function dump(obj,type) {
return CoreTest.jsDump.parse(obj, type);
};
})();
/**
Tests for equality any JavaScript type and structure without unexpected
results.
Discussions and reference: http://philrathe.com/articles/equiv
Test suites: http://philrathe.com/tests/equiv
Author: Philippe Rathé <[email protected]>
*/
CoreTest.equiv = function () {
var innerEquiv; // the real equiv function
var callers = []; // stack to decide between skip/abort functions
// Determine what is o.
function hoozit(o) {
if (typeof o === "string") {
return "string";
} else if (typeof o === "boolean") {
return "boolean";
} else if (typeof o === "number") {
if (isNaN(o)) {
return "nan";
} else {
return "number";
}
} else if (typeof o === "undefined") {
return "undefined";
// consider: typeof null === object
} else if (o === null) {
return "null";
// consider: typeof [] === object
} else if (o instanceof Array) {
return "array";
// consider: typeof new Date() === object
} else if (o instanceof Date) {
return "date";
// consider: /./ instanceof Object;
// /./ instanceof RegExp;
// typeof /./ === "function"; // => false in IE and Opera,
// true in FF and Safari
} else if (o instanceof RegExp) {
return "regexp";
} else if (typeof o === "object") {
return "object";
} else if (o instanceof Function) {
return "function";
}
}
// Call the o related callback with the given arguments.
function bindCallbacks(o, callbacks, args) {
var prop = hoozit(o);
if (prop) {
if (hoozit(callbacks[prop]) === "function") {
return callbacks[prop].apply(callbacks, args);
} else {
return callbacks[prop]; // or undefined
}
}
}
var callbacks = function () {
// for string, boolean, number and null
function useStrictEquality(b, a) {
return a === b;
}
return {
"string": useStrictEquality,
"boolean": useStrictEquality,
"number": useStrictEquality,
"null": useStrictEquality,
"undefined": useStrictEquality,
"nan": function (b) {
return isNaN(b);
},
"date": function (b, a) {
return hoozit(b) === "date" && a.valueOf() === b.valueOf();
},
"regexp": function (b, a) {
return hoozit(b) === "regexp" &&
a.source === b.source && // the regex itself
a.global === b.global && // and its modifers (gmi) ...
a.ignoreCase === b.ignoreCase &&
a.multiline === b.multiline;
},
// - skip when the property is a method of an instance (OOP)
// - abort otherwise,
// initial === would have catch identical references anyway
"function": function () {
var caller = callers[callers.length - 1];
return caller !== Object &&
typeof caller !== "undefined";
},
"array": function (b, a) {
var i;
var len;
// b could be an object literal here
if ( ! (hoozit(b) === "array")) {
return false;
}
len = a.length;
if (len !== b.length) { // safe and faster
return false;
}
for (i = 0; i < len; i++) {
if( ! innerEquiv(a[i], b[i])) {
return false;
}
}
return true;
},
"object": function (b, a) {
var i;
var eq = true; // unless we can proove it
var aProperties = [], bProperties = []; // collection of strings
if (b===a) return true;
// comparing constructors is more strict than using instanceof
if ( a.constructor !== b.constructor) {
return false;
}
// stack constructor before traversing properties
callers.push(a.constructor);
for (i in a) { // be strict: don't ensures hasOwnProperty and go deep
aProperties.push(i); // collect a's properties
if ( ! innerEquiv(a[i], b[i])) {
eq = false;
}
}
callers.pop(); // unstack, we are done
for (i in b) {
bProperties.push(i); // collect b's properties
}
// Ensures identical properties name
return eq && innerEquiv(aProperties.sort(), bProperties.sort());
}
};
}();
innerEquiv = function () { // can take multiple arguments
var args = Array.prototype.slice.apply(arguments);
if (args.length < 2) {
return true; // end transition
}
return (function (a, b) {
if (a === b) {
return true; // catch the most you can
} else if (typeof a !== typeof b || a === null || b === null || typeof a === "undefined" || typeof b === "undefined") {
return false; // don't lose time with error prone cases
} else if (b && b.isEqual && b.isEqual instanceof Function) {
return b.isEqual(a);
} else {
return bindCallbacks(a, callbacks, [b, a]);
}
// apply transition with (1..n) arguments
})(args[0], args[1]) && arguments.callee.apply(this, args.splice(1, args.length -1));
};
return innerEquiv;
}(); // equiv
/** @class
A test Suite defines a group of reusable unit tests that can be added to a
test plan at any time by calling the generate() method. Suites are most
useful for defining groups of tests that validate compliance with a mixin.
You can then generate customized versions of the test suite for different
types of objects to ensure that both the mixin and the object implementing
the mixin use the API properly.
h1. Using a Suite
To use a Suite, call the generate() method on the suite inside on of your
unit test files. This will generate new modules and tests in the suite
and add them to your test plan.
Usually you will need to customize the suite to apply to a specific object.
You can supply these customizations through an attribute hash passed to the
generate() method. See the documentation on the specific test suite for
information on the kind of customizations you may need to provide.
h2. Example
{{{
// generates the SC.ArrayTestSuite tests for a built-in array.
SC.ArrayTests.generate('Array', {
newObject: function() { return []; }
});
}}}
h1. Defining a Suite
To define a test suite, simply call the extend() method, passing any
attributs you want to define on the stuie along with this method. You can
then add functions that will define the test suite with the define() method.
Functions you pass to define will have an instance of the test suite passed
as their first parameter when invoked.
h2. Example
{{{
SC.ArrayTests = CoreTest.Suite.create("Verify SC.Array compliance", {
// override to generate a new object that implements SC.Array
newObject: function() { return null; }
});
SC.ArrayTests.define(function(T) {
T.module("length tests");
test("new length", function() {
equals(T.object.get('length'), 0, 'array length');
});
});
}}}
@since SproutCore 1.0
*/
CoreTest.Suite = /** @scope CoreTest.Suite.prototype */ {
/**
Call this method to define a new test suite. Pass one or more hashes of
properties you want added to the new suite.
@param {Hash} attrs one or more attribute hashes
@returns {CoreTest.Suite} subclass of suite.
*/
create: function(desc, attrs) {
var len = arguments.length,
ret = CoreTest.beget(this),
idx;
// copy any attributes
for(idx=1;idx<len;idx++) CoreTest.mixin(ret, arguments[idx]);
if (desc) ret.basedesc = desc;
// clone so that new definitions will be kept separate
ret.definitions = ret.definitions.slice();
return ret ;
},
/**
Generate a new test suite instance, adding the suite definitions to the
current test plan. Pass a description of the test suite as well as one or
more attribute hashes to apply to the test plan.
The description you add will be prefixed in front of the 'desc' property
on the test plan itself.
@param {String} desc suite description
@param {Hash} attrs one or more attribute hashes
@returns {CoreTest.Suite} suite instance
*/
generate: function(desc, attrs) {
var len = arguments.length,
ret = CoreTest.beget(this),
idx, defs;
// apply attributes - skip first argument b/c it is a string
for(idx=1;idx<len;idx++) CoreTest.mixin(ret, arguments[idx]);
ret.subdesc = desc ;
// invoke definitions
defs = ret.definitions ;
len = defs.length;
for(idx=0;idx<len;idx++) defs[idx].call(ret, ret);
return ret ;
},
/**
Adds the passed function to the array of definitions that will be invoked
when the suite is generated.
The passed function should expect to have the TestSuite instance passed
as the first and only parameter. The function should actually define
a module and tests, which will be added to the test suite.
@param {Function} func definition function
@returns {CoreTest.Suite} receiver
*/
define: function(func) {
this.definitions.push(func);
return this ;
},
/**
Definition functions. These are invoked in order when you generate a
suite to add unit tests and modules to the test plan.
*/
definitions: [],
/**
Generates a module description by merging the based description, sub
description and the passed description. This is usually used inside of
a suite definition function.
@param {String} str detailed description for this module
@returns {String} generated description
*/
desc: function(str) {
return this.basedesc.fmt(this.subdesc, str);
},
/**
The base description string. This should accept two formatting options,
a sub description and a detailed description. This is the description
set when you call extend()
*/
basedesc: "%@ > %@",
/**
Default setup method for use with modules. This method will call the
newObject() method and set its return value on the object property of
the receiver.
*/
setup: function() {
this.object = this.newObject();
},
/**
Default teardown method for use with modules. This method will call the
destroyObejct() method, passing the current object property on the
receiver. It will also clear the object property.
*/
teardown: function() {
if (this.object) this.destroyObject(this.object);
this.object = null;
},
/**
Default method to create a new object instance. You will probably want
to override this method when you generate() a suite with a function that
can generate the type of object you want to test.
@returns {Object} generated object
*/
newObject: function() { return null; },
/**
Default method to destroy a generated object instance after a test has
completed. If you override newObject() you can also overried this method
to cleanup the object you just created.
Default method does nothing.
*/
destroyObject: function(obj) {
// do nothing.
},
/**
Generates a default module with the description you provide. This is
a convenience function for use inside of a definition function. You could
do the same thing by calling:
{{{
var T = this ;
module(T.desc(description), {
setup: function() { T.setup(); },
teardown: function() { T.teardown(); }
}
}}}
@param {String} desc detailed descrition
@returns {CoreTest.Suite} receiver
*/
suite: function(desc) {
var T = this ;
suite(T.desc(desc), {
setup: function() { T.setup(); },
teardown: function() { T.teardown(); }
});
}
};
global.CoreTest = CoreTest;
// Bring in the actual array tests.
fs.readdirSync('./tests/test_suites/array/').forEach(function(filename) {
require('./tests/test_suites/array/'+filename);
});