-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathtest-introduction.es6
253 lines (209 loc) · 6.41 KB
/
test-introduction.es6
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// I'm using Babel.js and Intellij IDEA File Watcher to automatically transpile es6 to js:
// --source-maps --out-file $FileNameWithoutExtension$-compiled.js $FilePath$
QUnit.test("sample 1", function (assert) {
let myList = ["a1", "a2", "b1", "c2", "c1"];
let result = Stream(myList)
.filter(s => s.indexOf("c") === 0)
.map(s => s.toUpperCase())
.sorted()
.toArray();
assert.equal(result.length, 2);
assert.equal(result[0], "C1");
assert.equal(result[1], "C2");
});
QUnit.test("sample 2", function (assert) {
Stream(["a1", "a2", "a3"])
.findFirst()
.ifPresent(first => assert.equal(first, "a1"));
Stream.of("a1", "a2", "a3")
.findFirst()
.ifPresent(first => assert.equal(first, "a1"));
let result = Stream
.range(1, 4)
.toArray();
assert.equal(result.length, 3);
assert.equal(result[0], 1);
assert.equal(result[1], 2);
assert.equal(result[2], 3);
});
QUnit.test("sample 3", function (assert) {
Stream.of(1, 2, 3)
.map(n => 2 * n + 1)
.average()
.ifPresent(avg => assert.equal(avg, 5.0));
});
QUnit.test("sample 4", function (assert) {
Stream.of("a1", "a2", "a3")
.map(s => s.slice(1))
.map(s => parseInt(s, 10))
.max()
.ifPresent(max => assert.equal(max, 3));
});
QUnit.test("sample 5", function (assert) {
Stream.of("a1", "b2", "c3")
.filter(s => {
console.log("filtering: %s", s);
assert.ok(false);
return true;
});
assert.ok(true);
});
QUnit.test("sample 6", function (assert) {
let ops = [];
Stream.of("a1", "b2", "c3")
.filter(s => {
ops.push("filter: " + s);
return true;
})
.forEach(s => ops.push("forEach: " + s));
assert.equal(ops.length, 6);
assert.equal(ops[0], "filter: a1");
assert.equal(ops[1], "forEach: a1");
assert.equal(ops[2], "filter: b2");
assert.equal(ops[3], "forEach: b2");
assert.equal(ops[4], "filter: c3");
assert.equal(ops[5], "forEach: c3");
});
QUnit.test("sample 6", function (assert) {
let ops = [];
Stream.of("d2", "a2", "b1", "b3", "c")
.map(s => {
ops.push("map: " + s);
return s.toUpperCase();
})
.anyMatch(s => {
ops.push("anyMatch: " + s);
return s.indexOf("A") === 0;
});
assert.equal(ops.length, 4);
assert.equal(ops[0], "map: d2");
assert.equal(ops[1], "anyMatch: D2");
assert.equal(ops[2], "map: a2");
assert.equal(ops[3], "anyMatch: A2");
});
QUnit.test("sample 7", function (assert) {
let ops = [];
Stream.of("d2", "a2", "b1", "b3", "c")
.filter(s => {
ops.push("filter: " + s);
return s.indexOf("a") === 0;
})
.map(s => {
ops.push("map: " + s);
return s.toUpperCase();
})
.forEach(s => ops.push("forEach: " + s));
assert.equal(ops.length, 7);
assert.equal(ops[0], "filter: d2");
assert.equal(ops[1], "filter: a2");
assert.equal(ops[2], "map: a2");
assert.equal(ops[3], "forEach: A2");
assert.equal(ops[4], "filter: b1");
assert.equal(ops[5], "filter: b3");
assert.equal(ops[6], "filter: c");
});
QUnit.test("sample 8", function (assert) {
assert.throws(function () {
let stream = Stream.of(1, 2, 3)
.filter(n => n % 2 === 1);
stream.anyMatch(n => true); // ok
stream.toArray(); // error
});
});
QUnit.test("sample 9", function (assert) {
let odd = array =>
Stream(array).filter(n => n % 2 === 1);
assert.equal(odd([1, 2, 3]).anyMatch(n => true), true);
assert.equal(odd([1, 2, 3]).toArray().length, 2);
});
var persons = [
{name: "Max", age: 18},
{name: "Peter", age: 23},
{name: "Pamela", age: 23},
{name: "David", age: 12}
];
QUnit.test("sample 10", function (assert) {
var groups = Stream(persons)
.groupBy(p => p.age);
assert.equal(groups[18].length, 1);
assert.equal(groups[23].length, 2);
assert.equal(groups[12].length, 1);
});
QUnit.test("sample 10", function (assert) {
var avg = Stream(persons)
.map(p => p.age)
.average()
.get();
assert.equal(avg, 19);
avg = Stream(persons)
.map("age")
.average()
.get();
assert.equal(avg, 19);
});
QUnit.test("sample 11", function (assert) {
var phrase = Stream(persons)
.filter(p => p.age >= 18)
.map(p => p.name)
.join({
prefix: 'In Germany ',
suffix: ' are of legal age.',
delimiter: ' and '
});
assert.equal(phrase, 'In Germany Max and Peter and Pamela are of legal age.');
phrase = Stream(persons)
.filter(p => p.age >= 18)
.map(p => p.name)
.join(" | ");
assert.equal(phrase, 'Max | Peter | Pamela');
});
QUnit.test("sample 12", function (assert) {
var result = Stream(persons)
.collect({
supplier: () => '[',
accumulator: (s, p) => s + ' ' + p.name.toUpperCase(),
finisher: (s) => s + ' ]'
});
assert.equal(result, "[ MAX PETER PAMELA DAVID ]");
});
QUnit.test("sample 13", function (assert) {
var oldest = Stream(persons)
.reduce((p1, p2) => p1.age > p2.age ? p1 : p2)
.get();
assert.equal(oldest.name, "Pamela");
});
QUnit.test("sample 13", function (assert) {
var result = Stream(persons)
.sort("age")
.reverse()
.reduce({names: [], sumOfAges: 0}, (res, p) => {
res.names.push(p.name);
res.sumOfAges += p.age;
return res;
});
assert.equal(result.names.length, 4);
assert.equal(result.names[0], "Pamela");
assert.equal(result.names[1], "Peter");
assert.equal(result.names[2], "Max");
assert.equal(result.names[3], "David");
assert.equal(result.sumOfAges, 76);
});
QUnit.test("sample 14", function (assert) {
function* fibonacci() {
let [prev, cur] = [0, 1];
while (true) {
[prev, cur] = [cur, prev + cur];
yield cur;
}
}
var fib = Stream(fibonacci())
.filter(n => n % 2)
.takeWhile(n => n < 50)
.toArray();
assert.equal(fib.length, 5);
assert.equal(fib[0], 1);
assert.equal(fib[1], 3);
assert.equal(fib[2], 5);
assert.equal(fib[3], 13);
assert.equal(fib[4], 21);
});