-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
138 lines (119 loc) · 3.5 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
import test from 'ava';
import 'babel-core/register';
import {push, pop, unshift, shift, splice, set, flatten, map, move, filter} from './index';
import arrayMethods from './index';
test('push()', t => {
t.deepEqual(push(['a', 'b', 'c'], 'd'), ['a', 'b', 'c', 'd']);
t.deepEqual(push(['a', 'b', 'c'], ['d']), ['a', 'b', 'c', ['d']]);
});
test('unshift()', t => {
t.deepEqual(unshift(['a', 'b', 'c'], 'd'), ['d', 'a', 'b', 'c']);
t.deepEqual(unshift(['a', 'b', 'c'], ['d']), [['d'], 'a', 'b', 'c']);
});
test('pop()', t => {
t.deepEqual(pop(['a', 'b', 'c']), ['a', 'b']);
});
test('shift()', t => {
t.deepEqual(shift(['a', 'b', 'c']), ['b', 'c']);
});
test('splice()', t => {
t.deepEqual(splice(['a', 'b', 'c', 'd'], 2, 1), ['a', 'b', 'd']);
t.deepEqual(splice(['a', 'b', 'c', 'd'], 2, 0, 'e'), ['a', 'b', 'e', 'c', 'd']);
});
test('splice() no change', t => {
const input = ['a', 'b', 'c', 'd'];
const actual = splice(input, 0, 0);
const expected = input;
t.is(actual, expected);
});
test('splice(), unchanged array', t => {
const input = ['a', 'b', 'c', 'd'];
const actual = splice(input, 2, 2, 'c', 'd');
const expected = input;
t.is(actual, expected);
});
test('set', t => {
t.deepEqual(set([0, 1, 2], 0, 666), [666, 1, 2]);
t.deepEqual(set([], 0, [666]), [[666]]);
const input = [0, 1, 2];
const actual = set(input, 1, 1);
const expected = input;
t.is(actual, expected);
});
test('flatten, no nested arrays', t => {
const input = [1, 2, 3];
const actual = flatten(input);
const expected = input;
t.is(actual, expected);
});
test('flatten, with nested arrays', t => {
const input = Object.freeze([1, [2, 3], [4, [5]]]);
const actual = flatten(input);
const expected = [1, 2, 3, 4, [5]];
t.deepEqual(actual, expected);
});
test('map', t => {
const input = [1, 2, 3, 4];
const actual = map(input, num => num % 2);
const expected = [1, 0, 1, 0];
t.deepEqual(actual, expected);
});
test('map not changed values', t => {
const input = [1, 2, 3];
const actual = map(input, num => num);
const expected = input;
t.is(actual, expected);
});
test('map w index', t => {
const input = ['', ''];
const actual = map(input, (val, index) => index);
const expected = [0, 1];
t.deepEqual(actual, expected);
});
test('move() forward', t => {
const input = [1, 2, 3];
const from = 0;
const to = 1;
const actual = move(input, from, to);
const expected = [2, 1, 3];
t.deepEqual(actual, expected);
});
test('move() backward', t => {
const input = [1, 2, 3];
const from = 1;
const to = 0;
const actual = move(input, from, to);
const expected = [2, 1, 3];
t.deepEqual(actual, expected);
});
test('move() multiple indexes length', t => {
const input = [1, 2, 3];
const from = 0;
const to = 2;
const actual = move(input, from, to);
const expected = [2, 3, 1];
t.deepEqual(actual, expected);
});
test('filter() unchanged', t => {
const input = [1, 2, 3];
const actual = filter(input, () => true);
const expected = input;
t.is(actual, expected);
});
test('filter()', t => {
const input = [1, 2, 3];
const actual = filter(input, (value) => value > 1);
const expected = [2, 3];
t.deepEqual(actual, expected);
});
test('default import', t => {
t.is(arrayMethods.push, push);
t.is(arrayMethods.unshift, unshift);
t.is(arrayMethods.pop, pop);
t.is(arrayMethods.shift, shift);
t.is(arrayMethods.splice, splice);
t.is(arrayMethods.set, set);
t.is(arrayMethods.flatten, flatten);
t.is(arrayMethods.map, map);
t.is(arrayMethods.filter, filter);
});