-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.spec.js
154 lines (144 loc) · 4.75 KB
/
index.spec.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
const iterateur = require('./index.js')
const { expect } = require('chai')
describe('Number', () => {
iterateur.registerNumberIterator()
it('should iterate from 0 to 10', () => {
const numbers = [...10]
expect(numbers).to.eql([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
})
it('should iterate from 0 to -10', () => {
const numbers = [...-10]
expect(numbers).to.eql([0, -1, -2, -3, -4, -5, -6, -7, -8, -9])
})
it('should iterate from 2 to 12', () => {
const numbers = [...2.12]
expect(numbers).to.eql([2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
})
it('should iterate from 10 to 2', () => {
const numbers = [...10.2]
expect(numbers).to.eql([ 10, 9, 8, 7, 6, 5, 4, 3 ])
})
it('should iterate -2.5', () => {
const numbers = [...-2.5]
expect(numbers).to.eql([ 5, 4, 3 ])
})
it('should iterate -10.2', () => {
const numbers = [...-10.2]
expect(numbers).to.eql([2, 3, 4, 5, 6, 7, 8, 9])
})
it('should iterate over the number', () => {
const numbers = [...-3]
expect(numbers)
.to.be.an('array')
.that.has.lengthOf(3)
.and.that.deep.equals([0, -1, -2])
})
it('should iterate over Infinity', () => {
try {
iterateur.INFINITY_LIMIT = 3
;[...Infinity]
} catch (e) {
expect(e).to.be.an.instanceOf(iterateur.InfinityError)
}
iterateur.INFINITY_LIMIT = -1
let count = 0
for (const i of Infinity[Symbol.iterator]()) {
expect(i).to.equal(Infinity)
if (count++ > 5) {
break
}
}
})
it('should throw TypeError when NaN is iterated', () => {
expect(() => [...NaN]).to.throw(TypeError)
})
it('should iterate with step', () => {
const numbers = [...10..step(2)]
expect(numbers).to.eql([ 0, 2, 4, 6, 8 ])
})
})
describe('RegExp', () => {
iterateur.registerRegExpIterator()
it('should iterate from 0 to 10', () => {
const numbers = [.../10/]
expect(numbers).to.eql([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
})
it('should iterate from 0 to -10', () => {
const numbers = [.../-10/]
expect(numbers).to.eql([0, -1, -2, -3, -4, -5, -6, -7, -8, -9])
})
it('should iterate from 2 to 12', () => {
const numbers = [.../2~12/]
expect(numbers).to.eql([2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
})
it('should iterate from 10 to 2', () => {
const numbers = [.../10~2/]
expect(numbers).to.eql([ 10, 9, 8, 7, 6, 5, 4, 3 ])
})
it('should iterate from 0 to 10 with step', () => {
const numbers = [.../0~10:2/]
expect(numbers).to.eql([ 0, 2, 4, 6, 8 ])
})
it('should resolve the RegExpName', () => {
expect(iterateur.regexpNameResolver(/10/))
.to.eql([0, 10, 1])
expect(iterateur.regexpNameResolver(/0~10/))
.to.eql([0, 10, 1])
expect(iterateur.regexpNameResolver(/0~13:2/))
.to.eql([0, 13, 2])
expect(iterateur.regexpNameResolver(/5~13:2/))
.to.eql([5, 13, 2])
})
it('should custom the function name resolver', () => {
const oldResolver = iterateur.regexpNameResolver
iterateur.regexpNameResolver = regexp => regexp.source === 'any' ? [0, 10, 1] : oldResolver(regexp)
expect([.../any/]).to.eql([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
expect([.../5/]).to.eql([0, 1, 2, 3, 4])
iterateur.regexpNameResolver = oldResolver
})
})
describe('Function', () => {
iterateur.registerFunctionIterator([true, true])
describe('Anonyme', () => {
it('should iterate from 0 to 10', () => {
expect(
[...() => [/10/, i => i + 1]]
).to.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
expect(
[...() => [10, i => i + 1]]
).to.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
expect(
[...() => ['i0_10$plusOne', i => i + 1]]
).to.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
})
})
describe('Named', () => {
it('should iterate from 1 to 10', () => {
const numbers = [...function i0_10$plusOne(i) {
return i + 1
}]
expect(numbers).to.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
})
it('should resolve the function name', () => {
expect(iterateur.functionNameResolver('i0_10'))
.to.eql([0, 10, 1])
expect(iterateur.functionNameResolver('i0_10_plusOne'))
.to.eql([0, 10, 1])
expect(iterateur.functionNameResolver('i0_13$2_plusOne'))
.to.eql([0, 13, 2])
expect(iterateur.functionNameResolver('i5_13$2_plusOne'))
.to.eql([5, 13, 2])
})
it('should custom the function name resolver', () => {
const oldResolver = iterateur.functionNameResolver
iterateur.functionNameResolver = name => name === 'any' ? [0, 10, 1] : oldResolver(name)
expect([...function any(i) {
return i + 1
}]).to.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
expect([...function i0_5(i) {
return i + 1
}]).to.eql([1, 2, 3, 4, 5])
iterateur.functionNameResolver = oldResolver
})
})
})