-
Notifications
You must be signed in to change notification settings - Fork 1
/
underscore-tests.js
58 lines (48 loc) · 1.54 KB
/
underscore-tests.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
var _ = require("./underscore");
var prepareTest = function (test) {
test.isTrue = test.ok;
test.isFalse = function (v, m) {
return test.ok(!v, m);
};
};
exports['test underscore - each'] = function (test) {
// arrays
_.each([42], function (val, index) {
test.equal(index, 0);
test.equal(val, 42);
});
// objects with 'length' field aren't treated as arrays
_.each({length: 42}, function (val, key) {
test.equal(key, 'length');
test.equal(val, 42);
});
// The special 'arguments' variable is treated as an
// array
(function () {
_.each(arguments, function (val, index) {
test.equal(index, 0);
test.equal(val, 42);
});
})(42);
// An object with a 'callee' field isn't treated as arguments
_.each({callee: 42}, function (val, key) {
test.equal(key, 'callee');
test.equal(val, 42);
});
// An object with a 'callee' field isn't treated as arguments
_.each({length: 4, callee: 42}, function (val, key) {
if (key === 'callee')
test.equal(val, 42);
else if (key === 'length')
test.equal(val, 4);
else
test.fail({message: 'unexpected key: ' + key});
});
// NOTE: An object with a numberic 'length' field *and* a function
// 'callee' field will be treated as an array in IE. This may or may
// not be fixable, but isn't a big deal since: (1) 'callee' is a
// pretty rare key, and (2) JSON objects can't have functions
// anyways, which is the main use-case for _.each.
};
console.log(exports);
if (module == require.main) require('test').run(exports);