-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow.test.ts
158 lines (133 loc) · 3.91 KB
/
flow.test.ts
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
import { describe, expect, test } from '@jest/globals';
import { Flow } from './flow';
describe('flow', () => {
test(
'accept-and-peep',
() => {
// cooking
var lastValue: string;
let f = Flow.start<string>()
f.peep((v: string) => { lastValue = v })
// running
f.accept("some-value")
// cheking
expect(lastValue!).toBe("some-value")
}
)
test(
'accept-many',
() => {
// cooking
let f = Flow.start<number>()
let passed = f.collect()
// running
f.acceptMany([1, 2, 3, 4, 5])
// checking
expect(passed).toStrictEqual([1, 2, 3, 4, 5])
}
)
test(
'collect',
() => {
let f = Flow.start<string>()
let strings = f.collect()
f.acceptMany(["1", "2", "3"])
expect(strings).toStrictEqual(["1", "2", "3"])
}
)
test(
'collectTo',
() => {
let f = Flow.start<string>()
let strings = new Array()
f.collectTo(strings)
f.acceptMany(["1", "2", "3"])
expect(strings).toStrictEqual(["1", "2", "3"])
}
)
test(
'filter',
() => {
// cooking
let f = Flow.start<number>()
let lessThan5 = f.filter((v) => v < 5).collect()
// running
f.acceptMany([1, 2, 3, 4, 5, 6, 7, 8, 9])
// checking
expect(lessThan5).toStrictEqual([1, 2, 3, 4])
}
)
test(
'map',
() => {
// cooking
let f = Flow.start<number>()
let strings = f.map((v) => "" + v).collect()
// running
f.acceptMany([1, 2, 3, 4, 5])
// checking
expect(strings).toStrictEqual(["1", "2", "3", "4", "5"])
}
)
test(
'select',
() => {
// cooking
let f = Flow.start<string>()
let filesystem = {
"folder1": ["file1", "file2"],
"folder2": ["file3", "file4"],
}
let files = f.select((folder) => filesystem[folder]).collect()
// running
f.accept("folder1")
f.accept("folder2")
// checking
expect(files).toStrictEqual(["file1", "file2", "file3", "file4"])
}
)
test(
'segregate-no-unclassified',
() => {
// cooking
let f = Flow.start<number>()
const classify = (n: number): string[] => {
return (n % 2) == 0 ? ["even"] : ["odd"]
}
let [even, odd] = f.segregate(classify, ["even", "odd"])
let evens = even.collect()
let odds = odd.collect()
// running
f.acceptMany([1, 2, 3, 4, 5, 6, 7, 8, 9])
// checking
expect(evens).toStrictEqual([2, 4, 6, 8])
expect(odds).toStrictEqual([1, 3, 5, 7, 9])
}
)
test(
'segregate-unclassified',
() => {
// cooking
let f = Flow.start<number>()
const classify = (n: number): string[] => {
let classes = new Array()
for (let i = 2; i < 10; i++) {
if (n % i == 0) {
classes.push("div" + i)
}
}
return classes
}
let [div7, other] = f.segregate(classify, ["div7"], true)
let div7s = div7.collect()
let others = other.collect()
// running
for (let n = 1; n <= 50; n++) {
f.accept(n)
}
// checking
expect(div7s).toStrictEqual([7, 14, 21, 28, 35, 42, 49])
expect(others).toHaveLength(43)
}
)
})