-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
153 lines (132 loc) · 4.63 KB
/
test.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
var test = require('tape');
var forEach = require('./index.js');
test('second argument: iterator', function (t) {
var arr = [];
t.throws(function () { forEach(arr); }, TypeError, 'undefined is not a function');
t.throws(function () { forEach(arr, null); }, TypeError, 'null is not a function');
t.throws(function () { forEach(arr, ''); }, TypeError, 'string is not a function');
t.throws(function () { forEach(arr, /a/); }, TypeError, 'regex is not a function');
t.throws(function () { forEach(arr, true); }, TypeError, 'true is not a function');
t.throws(function () { forEach(arr, false); }, TypeError, 'false is not a function');
t.throws(function () { forEach(arr, NaN); }, TypeError, 'NaN is not a function');
t.throws(function () { forEach(arr, 42); }, TypeError, '42 is not a function');
t.doesNotThrow(function () { forEach(arr, function () {}); }, 'function is a function');
t.end();
});
test('array', function (t) {
var arr = [1, 2, 3];
t.test('iterates over every item', function (st) {
var index = 0;
forEach(arr, function () { index += 1; });
st.equal(index, arr.length, 'iterates ' + arr.length + ' times');
st.end();
});
t.test('first iterator argument', function (st) {
var index = 0;
st.plan(arr.length);
forEach(arr, function (item) {
st.equal(arr[index], item, 'item ' + index + ' is passed as first argument');
index += 1;
});
st.end();
});
t.test('second iterator argument', function (st) {
var counter = 0;
st.plan(arr.length);
forEach(arr, function (item, index) {
st.equal(counter, index, 'index ' + index + ' is passed as second argument');
counter += 1;
});
st.end();
});
t.test('third iterator argument', function (st) {
st.plan(arr.length);
forEach(arr, function (item, index, array) {
st.deepEqual(arr, array, 'array is passed as third argument');
});
st.end();
});
t.test('context argument', function (st) {
var context = {};
st.plan(1);
forEach([1], function () {
st.equal(this, context, '"this" is the passed context');
}, context);
st.end();
});
t.end();
});
test('object', function (t) {
var obj = {
a: 1,
b: 2,
c: 3
};
var keys = ['a', 'b', 'c'];
var F = function () {
this.a = 1;
this.b = 2;
};
F.prototype.c = 3;
var fKeys = ['a', 'b'];
t.test('iterates over every object literal key', function (st) {
var counter = 0;
forEach(obj, function () { counter += 1; });
st.equal(counter, keys.length, 'iterated ' + counter + ' times');
st.end();
});
t.test('iterates only over own keys', function (st) {
var counter = 0;
forEach(new F(), function () { counter += 1; });
st.equal(counter, fKeys.length, 'iterated ' + fKeys.length + ' times');
st.end();
});
t.test('first iterator argument', function (st) {
var index = 0;
st.plan(keys.length);
forEach(obj, function (item) {
st.equal(obj[keys[index]], item, 'item at key ' + keys[index] + ' is passed as first argument');
index += 1;
});
st.end();
});
t.test('second iterator argument', function (st) {
var counter = 0;
st.plan(keys.length);
forEach(obj, function (item, key) {
st.equal(keys[counter], key, 'key ' + key + ' is passed as second argument');
counter += 1;
});
st.end();
});
t.test('third iterator argument', function (st) {
st.plan(keys.length);
forEach(obj, function (item, key, object) {
st.deepEqual(obj, object, 'object is passed as third argument');
});
st.end();
});
t.test('context argument', function (st) {
var context = {};
st.plan(1);
forEach({ a: 1 }, function () {
st.equal(this, context, '"this" is the passed context');
}, context);
st.end();
});
t.end();
});
test('string', function (t) {
var str = 'str';
t.test('second iterator argument', function (st) {
var counter = 0;
st.plan(str.length * 2 + 1);
forEach(str, function (item, index) {
st.equal(counter, index, 'index ' + index + ' is passed as second argument');
st.equal(str.charAt(index), item);
counter += 1;
});
st.equal(counter, str.length, 'iterates ' + str.length + ' times');
});
t.end();
});