Skip to content

Commit 45a8c0d

Browse files
committed
Prepare node testing. Separate browser specific tests
1 parent 891a6a8 commit 45a8c0d

File tree

4 files changed

+152
-85
lines changed

4 files changed

+152
-85
lines changed

test/collections.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@
7979
doubled = _([1, 2, 3]).map(function(num){ return num * 2; });
8080
deepEqual(doubled, [2, 4, 6], 'OO-style doubled numbers');
8181

82-
if (document.querySelectorAll) {
83-
var ids = _.map(document.querySelectorAll('#map-test *'), function(n){ return n.id; });
82+
if (typeof document != 'undefined') {
83+
var nodes = _.filter(document.getElementById('map-test').childNodes, _.isElement);
84+
var ids = _.map(nodes, 'id');
85+
equal(nodes.length, 2);
8486
deepEqual(ids, ['id1', 'id2'], 'Can use collection methods on NodeLists.');
8587
}
8688

@@ -651,12 +653,14 @@
651653
var numbers = _.toArray({one : 1, two : 2, three : 3});
652654
deepEqual(numbers, [1, 2, 3], 'object flattened into array');
653655

654-
// test in IE < 9
655-
try {
656-
var actual = _.toArray(document.childNodes);
657-
} catch(ex) { }
658-
659-
ok(_.isArray(actual), 'should not throw converting a node list');
656+
if (typeof document != 'undefined') {
657+
// test in IE < 9
658+
var actual;
659+
try {
660+
actual = _.toArray(document.childNodes);
661+
} catch(ex) { }
662+
deepEqual(actual, _.map(document.childNodes, _.identity), 'works on NodeList');
663+
}
660664
});
661665

662666
test('size', function() {

test/cross-document.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
(function() {
2+
if (typeof document == 'undefined') return;
3+
4+
module('Cross Document');
5+
/* global iObject, iElement, iArguments, iFunction, iArray, iError, iString, iNumber, iBoolean, iDate, iRegExp, iNaN, iNull, iUndefined, ActiveXObject */
6+
7+
// Setup remote variables for iFrame tests.
8+
var iframe = document.createElement('iframe');
9+
iframe.frameBorder = iframe.height = iframe.width = 0;
10+
document.body.appendChild(iframe);
11+
var iDoc = (iDoc = iframe.contentDocument || iframe.contentWindow).document || iDoc;
12+
iDoc.write(
13+
[
14+
'<script>',
15+
'parent.iElement = document.createElement("div");',
16+
'parent.iArguments = (function(){ return arguments; })(1, 2, 3);',
17+
'parent.iArray = [1, 2, 3];',
18+
'parent.iString = new String("hello");',
19+
'parent.iNumber = new Number(100);',
20+
'parent.iFunction = (function(){});',
21+
'parent.iDate = new Date();',
22+
'parent.iRegExp = /hi/;',
23+
'parent.iNaN = NaN;',
24+
'parent.iNull = null;',
25+
'parent.iBoolean = new Boolean(false);',
26+
'parent.iUndefined = undefined;',
27+
'parent.iObject = {};',
28+
'parent.iError = new Error();',
29+
'</script>'
30+
].join('\n')
31+
);
32+
iDoc.close();
33+
34+
test('isEqual', function() {
35+
36+
ok(!_.isEqual(iNumber, 101));
37+
ok(_.isEqual(iNumber, 100));
38+
39+
// Objects from another frame.
40+
ok(_.isEqual({}, iObject), 'Objects with equivalent members created in different documents are equal');
41+
42+
// Array from another frame.
43+
ok(_.isEqual([1, 2, 3], iArray), 'Arrays with equivalent elements created in different documents are equal');
44+
});
45+
46+
test('isEmpty', function() {
47+
ok(!_([iNumber]).isEmpty(), '[1] is not empty');
48+
ok(!_.isEmpty(iArray), '[] is empty');
49+
ok(_.isEmpty(iObject), '{} is empty');
50+
});
51+
52+
test('isElement', function() {
53+
ok(!_.isElement('div'), 'strings are not dom elements');
54+
ok(_.isElement(document.body), 'the body tag is a DOM element');
55+
ok(_.isElement(iElement), 'even from another frame');
56+
});
57+
58+
test('isArguments', function() {
59+
ok(_.isArguments(iArguments), 'even from another frame');
60+
});
61+
62+
test('isObject', function() {
63+
ok(_.isObject(iElement), 'even from another frame');
64+
ok(_.isObject(iFunction), 'even from another frame');
65+
});
66+
67+
test('isArray', function() {
68+
ok(_.isArray(iArray), 'even from another frame');
69+
});
70+
71+
test('isString', function() {
72+
ok(_.isString(iString), 'even from another frame');
73+
});
74+
75+
test('isNumber', function() {
76+
ok(_.isNumber(iNumber), 'even from another frame');
77+
});
78+
79+
test('isBoolean', function() {
80+
ok(_.isBoolean(iBoolean), 'even from another frame');
81+
});
82+
83+
test('isFunction', function() {
84+
ok(_.isFunction(iFunction), 'even from another frame');
85+
});
86+
87+
test('isDate', function() {
88+
ok(_.isDate(iDate), 'even from another frame');
89+
});
90+
91+
test('isRegExp', function() {
92+
ok(_.isRegExp(iRegExp), 'even from another frame');
93+
});
94+
95+
test('isNaN', function() {
96+
ok(_.isNaN(iNaN), 'even from another frame');
97+
});
98+
99+
test('isNull', function() {
100+
ok(_.isNull(iNull), 'even from another frame');
101+
});
102+
103+
test('isUndefined', function() {
104+
ok(_.isUndefined(iUndefined), 'even from another frame');
105+
});
106+
107+
test('isError', function() {
108+
ok(_.isError(iError), 'even from another frame');
109+
});
110+
111+
if (typeof ActiveXObject != 'undefined') {
112+
test('IE host objects', function() {
113+
var xml = new ActiveXObject('Msxml2.DOMDocument.3.0');
114+
ok(!_.isNumber(xml));
115+
ok(!_.isBoolean(xml));
116+
ok(!_.isNaN(xml));
117+
ok(!_.isFunction(xml));
118+
ok(!_.isNull(xml));
119+
ok(!_.isUndefined(xml));
120+
});
121+
122+
test('#1621 IE 11 compat mode DOM elements are not functions', function() {
123+
var fn = function() {};
124+
var xml = new ActiveXObject('Msxml2.DOMDocument.3.0');
125+
var div = document.createElement('div');
126+
127+
// JIT the function
128+
var count = 200;
129+
while (count--) {
130+
_.isFunction(fn);
131+
}
132+
133+
equal(_.isFunction(xml), false);
134+
equal(_.isFunction(div), false);
135+
equal(_.isFunction(fn), true);
136+
});
137+
}
138+
139+
}());

test/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<script src="arrays.js"></script>
2121
<script src="functions.js"></script>
2222
<script src="objects.js"></script>
23+
<script src="cross-document.js"></script>
2324
<script src="utility.js"></script>
2425
<script src="chaining.js"></script>
2526
</body>

test/objects.js

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(function() {
22

33
module('Objects');
4-
/* global iObject, iElement, iArguments, iFunction, iArray, iString, iNumber, iBoolean, iDate, iError, iRegExp, iNaN, iNull, iUndefined, ActiveXObject */
54

65
test('keys', function() {
76
deepEqual(_.keys({one : 1, two : 2}), ['one', 'two'], 'can extract the keys from an object');
@@ -437,12 +436,6 @@
437436
b = _({x: 1, y: 2}).chain();
438437
equal(_.isEqual(a.isEqual(b), _(true)), true, '`isEqual` can be chained');
439438

440-
// Objects from another frame.
441-
ok(_.isEqual({}, iObject), 'Objects with equivalent members created in different documents are equal');
442-
443-
// Array from another frame.
444-
ok(_.isEqual([1, 2, 3], iArray), 'Arrays with equivalent elements created in different documents are equal');
445-
446439
// Objects without a `constructor` property
447440
if (Object.create) {
448441
a = Object.create(null, {x: {value: 1, enumerable: true}});
@@ -477,37 +470,9 @@
477470
ok(!_.isEmpty(args('')), 'non-empty arguments object is not empty');
478471
});
479472

480-
// Setup remote variables for iFrame tests.
481-
var iframe = document.createElement('iframe');
482-
iframe.frameBorder = iframe.height = iframe.width = 0;
483-
document.body.appendChild(iframe);
484-
var iDoc = (iDoc = iframe.contentDocument || iframe.contentWindow).document || iDoc;
485-
iDoc.write(
486-
[
487-
'<script>',
488-
'parent.iElement = document.createElement("div");',
489-
'parent.iArguments = (function(){ return arguments; })(1, 2, 3);',
490-
'parent.iArray = [1, 2, 3];',
491-
'parent.iString = new String("hello");',
492-
'parent.iNumber = new Number(100);',
493-
'parent.iFunction = (function(){});',
494-
'parent.iDate = new Date();',
495-
'parent.iRegExp = /hi/;',
496-
'parent.iNaN = NaN;',
497-
'parent.iNull = null;',
498-
'parent.iBoolean = new Boolean(false);',
499-
'parent.iUndefined = undefined;',
500-
'parent.iObject = {};',
501-
'parent.iError = new Error();',
502-
'</script>'
503-
].join('\n')
504-
);
505-
iDoc.close();
506-
507473
test('isElement', function() {
508474
ok(!_.isElement('div'), 'strings are not dom elements');
509475
ok(_.isElement(document.body), 'the body tag is a DOM element');
510-
ok(_.isElement(iElement), 'even from another frame');
511476
});
512477

513478
test('isArguments', function() {
@@ -517,16 +482,13 @@
517482
ok(_.isArguments(args), 'but the arguments object is an arguments object');
518483
ok(!_.isArguments(_.toArray(args)), 'but not when it\'s converted into an array');
519484
ok(!_.isArguments([1, 2, 3]), 'and not vanilla arrays.');
520-
ok(_.isArguments(iArguments), 'even from another frame');
521485
});
522486

523487
test('isObject', function() {
524488
ok(_.isObject(arguments), 'the arguments object is object');
525489
ok(_.isObject([1, 2, 3]), 'and arrays');
526490
ok(_.isObject(document.body), 'and DOM element');
527-
ok(_.isObject(iElement), 'even from another frame');
528491
ok(_.isObject(function () {}), 'and functions');
529-
ok(_.isObject(iFunction), 'even from another frame');
530492
ok(!_.isObject(null), 'but not null');
531493
ok(!_.isObject(undefined), 'and not undefined');
532494
ok(!_.isObject('string'), 'and not string');
@@ -539,14 +501,12 @@
539501
ok(!_.isArray(undefined), 'undefined vars are not arrays');
540502
ok(!_.isArray(arguments), 'the arguments object is not an array');
541503
ok(_.isArray([1, 2, 3]), 'but arrays are');
542-
ok(_.isArray(iArray), 'even from another frame');
543504
});
544505

545506
test('isString', function() {
546507
var obj = new String('I am a string object');
547508
ok(!_.isString(document.body), 'the document body is not a string');
548509
ok(_.isString([1, 2, 3].join(', ')), 'but strings are');
549-
ok(_.isString(iString), 'even from another frame');
550510
ok(_.isString('I am a string literal'), 'string literals are');
551511
ok(_.isString(obj), 'so are String objects');
552512
});
@@ -558,7 +518,6 @@
558518
ok(_.isNumber(3 * 4 - 7 / 10), 'but numbers are');
559519
ok(_.isNumber(NaN), 'NaN *is* a number');
560520
ok(_.isNumber(Infinity), 'Infinity is a number');
561-
ok(_.isNumber(iNumber), 'even from another frame');
562521
ok(!_.isNumber('1'), 'numeric strings are not numbers');
563522
});
564523

@@ -573,7 +532,6 @@
573532
ok(!_.isBoolean(null), 'null is not a boolean');
574533
ok(_.isBoolean(true), 'but true is');
575534
ok(_.isBoolean(false), 'and so is false');
576-
ok(_.isBoolean(iBoolean), 'even from another frame');
577535
});
578536

579537
test('isFunction', function() {
@@ -582,21 +540,18 @@
582540
ok(!_.isFunction('moe'), 'strings are not functions');
583541
ok(!_.isFunction(document.createElement('div')), 'elements are not functions');
584542
ok(_.isFunction(_.isFunction), 'but functions are');
585-
ok(_.isFunction(iFunction), 'even from another frame');
586543
ok(_.isFunction(function(){}), 'even anonymous ones');
587544
});
588545

589546
test('isDate', function() {
590547
ok(!_.isDate(100), 'numbers are not dates');
591548
ok(!_.isDate({}), 'objects are not dates');
592549
ok(_.isDate(new Date()), 'but dates are');
593-
ok(_.isDate(iDate), 'even from another frame');
594550
});
595551

596552
test('isRegExp', function() {
597553
ok(!_.isRegExp(_.identity), 'functions are not RegExps');
598554
ok(_.isRegExp(/identity/), 'but RegExps are');
599-
ok(_.isRegExp(iRegExp), 'even from another frame');
600555
});
601556

602557
test('isFinite', function() {
@@ -620,15 +575,13 @@
620575
ok(!_.isNaN(null), 'null is not NaN');
621576
ok(!_.isNaN(0), '0 is not NaN');
622577
ok(_.isNaN(NaN), 'but NaN is');
623-
ok(_.isNaN(iNaN), 'even from another frame');
624578
ok(_.isNaN(new Number(NaN)), 'wrapped NaN is still NaN');
625579
});
626580

627581
test('isNull', function() {
628582
ok(!_.isNull(undefined), 'undefined is not null');
629583
ok(!_.isNull(NaN), 'NaN is not null');
630584
ok(_.isNull(null), 'but null is');
631-
ok(_.isNull(iNull), 'even from another frame');
632585
});
633586

634587
test('isUndefined', function() {
@@ -638,15 +591,13 @@
638591
ok(!_.isUndefined(NaN), 'NaN is defined');
639592
ok(_.isUndefined(), 'nothing is undefined');
640593
ok(_.isUndefined(undefined), 'undefined is undefined');
641-
ok(_.isUndefined(iUndefined), 'even from another frame');
642594
});
643595

644596
test('isError', function() {
645597
ok(!_.isError(1), 'numbers are not Errors');
646598
ok(!_.isError(null), 'null is not an Error');
647599
ok(!_.isError(Error), 'functions are not Errors');
648600
ok(_.isError(new Error()), 'Errors are Errors');
649-
ok(_.isError(iError), 'even from another frame');
650601
ok(_.isError(new EvalError()), 'EvalErrors are Errors');
651602
ok(_.isError(new RangeError()), 'RangeErrors are Errors');
652603
ok(_.isError(new ReferenceError()), 'ReferenceErrors are Errors');
@@ -655,34 +606,6 @@
655606
ok(_.isError(new URIError()), 'URIErrors are Errors');
656607
});
657608

658-
if (window.ActiveXObject) {
659-
test('IE host objects', function() {
660-
var xml = new ActiveXObject('Msxml2.DOMDocument.3.0');
661-
ok(!_.isNumber(xml));
662-
ok(!_.isBoolean(xml));
663-
ok(!_.isNaN(xml));
664-
ok(!_.isFunction(xml));
665-
ok(!_.isNull(xml));
666-
ok(!_.isUndefined(xml));
667-
});
668-
669-
test('#1621 IE 11 compat mode DOM elements are not functions', function() {
670-
var fn = function() {};
671-
var xml = new ActiveXObject('Msxml2.DOMDocument.3.0');
672-
var div = document.createElement('div');
673-
674-
// JIT the function
675-
var count = 200;
676-
while (count--) {
677-
_.isFunction(fn);
678-
}
679-
680-
equal(_.isFunction(xml), false);
681-
equal(_.isFunction(div), false);
682-
equal(_.isFunction(fn), true);
683-
});
684-
}
685-
686609
test('tap', function() {
687610
var intercepted = null;
688611
var interceptor = function(obj) { intercepted = obj; };

0 commit comments

Comments
 (0)