Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
var test = require('tape');
2
var forEach = require('./index.js');
3
4
5
test('second argument: iterator', function (t) {
6
var arr = [];
7
t.throws(function () { forEach(arr); }, TypeError, 'undefined is not a function');
8
t.throws(function () { forEach(arr, null); }, TypeError, 'null is not a function');
9
t.throws(function () { forEach(arr, ''); }, TypeError, 'string is not a function');
10
t.throws(function () { forEach(arr, /a/); }, TypeError, 'regex is not a function');
11
t.throws(function () { forEach(arr, true); }, TypeError, 'true is not a function');
12
t.throws(function () { forEach(arr, false); }, TypeError, 'false is not a function');
13
t.throws(function () { forEach(arr, NaN); }, TypeError, 'NaN is not a function');
14
t.throws(function () { forEach(arr, 42); }, TypeError, '42 is not a function');
15
t.doesNotThrow(function () { forEach(arr, function () {}); }, 'function is a function');
16
t.end();
17
});
18
19
test('array', function (t) {
20
var arr = [1, 2, 3];
21
22
t.test('iterates over every item', function (st) {
23
var index = 0;
24
forEach(arr, function () { index += 1; });
25
st.equal(index, arr.length, 'iterates ' + arr.length + ' times');
26
st.end();
27
});
28
29
t.test('first iterator argument', function (st) {
30
var index = 0;
31
st.plan(arr.length);
32
forEach(arr, function (item) {
33
st.equal(arr[index], item, 'item ' + index + ' is passed as first argument');
34
index += 1;
35
});
36
st.end();
37
});
38
39
t.test('second iterator argument', function (st) {
40
var counter = 0;
41
st.plan(arr.length);
42
forEach(arr, function (item, index) {
43
st.equal(counter, index, 'index ' + index + ' is passed as second argument');
44
counter += 1;
45
});
46
st.end();
47
});
48
49
t.test('third iterator argument', function (st) {
50
st.plan(arr.length);
51
forEach(arr, function (item, index, array) {
52
st.deepEqual(arr, array, 'array is passed as third argument');
53
});
54
st.end();
55
});
56
57
t.test('context argument', function (st) {
58
var context = {};
59
st.plan(1);
60
forEach([1], function () {
61
st.equal(this, context, '"this" is the passed context');
62
}, context);
63
st.end();
64
});
65
66
t.end();
67
});
68
69
test('object', function (t) {
70
var obj = {
71
a: 1,
72
b: 2,
73
c: 3
74
};
75
var keys = ['a', 'b', 'c'];
76
77
var F = function () {
78
this.a = 1;
79
this.b = 2;
80
};
81
F.prototype.c = 3;
82
var fKeys = ['a', 'b'];
83
84
t.test('iterates over every object literal key', function (st) {
85
var counter = 0;
86
forEach(obj, function () { counter += 1; });
87
st.equal(counter, keys.length, 'iterated ' + counter + ' times');
88
st.end();
89
});
90
91
t.test('iterates only over own keys', function (st) {
92
var counter = 0;
93
forEach(new F(), function () { counter += 1; });
94
st.equal(counter, fKeys.length, 'iterated ' + fKeys.length + ' times');
95
st.end();
96
});
97
98
t.test('first iterator argument', function (st) {
99
var index = 0;
100
st.plan(keys.length);
101
forEach(obj, function (item) {
102
st.equal(obj[keys[index]], item, 'item at key ' + keys[index] + ' is passed as first argument');
103
index += 1;
104
});
105
st.end();
106
});
107
108
t.test('second iterator argument', function (st) {
109
var counter = 0;
110
st.plan(keys.length);
111
forEach(obj, function (item, key) {
112
st.equal(keys[counter], key, 'key ' + key + ' is passed as second argument');
113
counter += 1;
114
});
115
st.end();
116
});
117
118
t.test('third iterator argument', function (st) {
119
st.plan(keys.length);
120
forEach(obj, function (item, key, object) {
121
st.deepEqual(obj, object, 'object is passed as third argument');
122
});
123
st.end();
124
});
125
126
t.test('context argument', function (st) {
127
var context = {};
128
st.plan(1);
129
forEach({ a: 1 }, function () {
130
st.equal(this, context, '"this" is the passed context');
131
}, context);
132
st.end();
133
});
134
135
t.end();
136
});
137
138
139
test('string', function (t) {
140
var str = 'str';
141
t.test('second iterator argument', function (st) {
142
var counter = 0;
143
st.plan(str.length * 2 + 1);
144
forEach(str, function (item, index) {
145
st.equal(counter, index, 'index ' + index + ' is passed as second argument');
146
st.equal(str.charAt(index), item);
147
counter += 1;
148
});
149
st.equal(counter, str.length, 'iterates ' + str.length + ' times');
150
});
151
t.end();
152
});
153
154
155