-
-
Notifications
You must be signed in to change notification settings - Fork 812
/
Copy pathtest.js
218 lines (165 loc) · 4.88 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
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
'use strict';
// MODULES //
var tape = require( 'tape' );
var indexOf = require( './../lib' );
// TESTS //
tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.equal( typeof indexOf, 'function', 'main export is a function' );
t.end();
});
tape( 'the function throws a type error if not provided an array-like object', function test( t ) {
var values;
var i;
values = [
5,
NaN,
true,
null,
undefined,
{}, // not array-like, as no `length` property
function noop() {} // has `length` property, but is a function
];
for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( values[i] ), TypeError, 'throws type error when provided ' + values[ i ] );
}
t.end();
function badValue( value ) {
return function badValue() {
indexOf( value, 3 );
};
}
});
tape( 'if provided a `fromIndex`, which is not an integer, the function throws a type error', function test( t ) {
var values;
var i;
values = [
'5',
5.234,
NaN,
true,
null,
undefined,
{},
[],
function noop() {}
];
for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( values[i] ), TypeError, 'throws type error when provided ' + values[ i ] );
}
t.end();
function badValue( value ) {
return function badValue() {
indexOf( [ 1, 2, 3 ], 3, value );
};
}
});
tape( 'if provided an array-like object having length `0`, the function always returns `-1`', function test( t ) {
var idx = indexOf( [], 5 );
t.equal( idx, -1, 'returns -1' );
t.end();
});
tape( 'if provided a `fromIndex` which exceeds the input array length, the function always returns `-1`', function test( t ) {
var idx = indexOf( [ 1, 2, 3 ], 2, 999999999999 );
t.equal( idx, -1, 'returns -1' );
t.end();
});
tape( 'the function returns the first index at which a given element can be found', function test( t ) {
var idx = indexOf( [ 1, 1, 1, 2, 2, 3, 3 ], 2 );
t.equal( idx, 3, 'returns first occurrence index' );
t.end();
});
tape( 'the function supports finding `NaN` elements', function test( t ) {
var idx = indexOf( [ 1, 1, 1, 2, NaN, 2, 3, NaN, 3 ], NaN );
t.equal( idx, 4, 'returns first occurrence index' );
t.end();
});
tape( 'if a search element is not present, the function returns `-1`', function test( t ) {
var idx = indexOf( [ 1, 2, 3 ], 4 );
t.equal( idx, -1, 'returns -1' );
t.end();
});
tape( 'the function supports specifying a `fromIndex`', function test( t ) {
var arr;
var idx;
arr = [ 1, 1, 2, 1, 2, 2, 3, 2, 3 ];
idx = indexOf( arr, 2 );
t.equal( idx, 2, 'returns first occurrence index' );
idx = indexOf( arr, 2, idx+1 );
t.equal( idx, 4, 'returns second occurrence index' );
idx = indexOf( arr, 2, idx+1 );
t.equal( idx, 5, 'returns third occurrence index' );
idx = indexOf( arr, 2, idx+1 );
t.equal( idx, 7, 'returns fourth occurrence index' );
idx = indexOf( arr, 2, idx+1 );
t.equal( idx, -1, 'returns -1' );
t.end();
});
tape( 'if provided a `fromIndex` which is less than `0`, the function determines a start index relative to the last array element', function test( t ) {
var arr;
var idx;
arr = [ 1, 1, 2, 1, 2, 2, 3, 2, 3 ];
idx = indexOf( arr, 3, -1 );
t.equal( idx, 8, 'returns last element index' );
idx = indexOf( arr, 2, -1 );
t.equal( idx, -1, 'returns -1' );
idx = indexOf( arr, 2, -2 );
t.equal( idx, 7, 'returns second to last element index' );
idx = indexOf( arr, 2, -3 );
t.equal( idx, 7, 'returns second to last element index' );
idx = indexOf( arr, 2, -4 );
t.equal( idx, 5, 'returns 5' );
t.end();
});
tape( 'if provided a `fromIndex` which is less than `0` and whose absolute value exceeds the array length, the function starts searching from the first array element', function test( t ) {
var arr;
var idx;
arr = [ 1, 1, 2, 1, 2, 2, 3, 2, 3 ];
idx = indexOf( arr, 2, -99999999999999 );
t.equal( idx, 2, 'returns first occurrence index' );
idx = indexOf( arr, 1, -99999999999999 );
t.equal( idx, 0, 'returns first occurrence index' );
t.end();
});
tape( 'the function supports searching `strings`', function test( t ) {
var str;
var idx;
str = 'bebop';
idx = indexOf( str, 'o' );
t.equal( idx, 3, 'returns first occurrence index' );
t.end();
});
tape( 'the function supports array-like `objects`', function test( t ) {
var obj;
var idx;
obj = {
'0': 'beep',
'1': 'boop',
'2': 'bap',
'3': 'bop',
'length': 4
};
idx = indexOf( obj, 'bap' );
t.equal( idx, 2, 'returns first occurrence index' );
t.end();
});
tape( 'the function does not guarantee that only "own" properties are searched', function test( t ) {
var foo;
var idx;
function Foo() {
this[ 0 ] = 'beep';
this[ 1 ] = 'boop';
this[ 2 ] = 'woot';
this[ 3 ] = 'bop';
this.length = 4;
return this;
}
Foo.prototype[ 2 ] = 'bap';
foo = new Foo();
idx = indexOf( foo, 'bap' );
t.equal( idx, -1, 'returns -1' );
delete foo[ 2 ];
idx = indexOf( foo, 'bap' );
t.equal( idx, 2, 'returns property on prototype' );
t.end();
});