-
Notifications
You must be signed in to change notification settings - Fork 18
/
Aggregate.ts
183 lines (144 loc) · 7.03 KB
/
Aggregate.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
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
import { InvalidOperationException } from "linq-to-typescript"
import { asAsync, expectAsync, itAsync, itEnumerable, itParallel } from "./../TestHelpers"
describe("aggregate", () => {
it("String", () => {
expect("thisisthemostboringpartofcoding".aggregate((x, y) => x + y)).toBe("thisisthemostboringpartofcoding")
})
itEnumerable<string>("Basic", (asEnumerable) => {
const array = asEnumerable(["f", "o", "o"])
expect(array.aggregate((x, y) => x + y)).toBe("foo")
const sentence = "the quick brown fox jumps over the lazy dog"
// Split the string into individual words.
const words = asEnumerable(sentence.split(" "))
// Prepend each word to the beginning of the
// new sentence to reverse the word order.
const reversed = words.aggregate((workingSentence, next) =>
next + " " + workingSentence)
expect(reversed).toBe("dog lazy the over jumps fox brown quick the")
})
itAsync("Basic", async () => {
const asyncArray = asAsync(["f", "o", "o"])
expect(await asyncArray.aggregate((x, y) => x + y)).toBe("foo")
const sentence = "the quick brown fox jumps over the lazy dog"
// Split the string into individual words.
const words = asAsync(sentence.split(" "))
// Prepend each word to the beginning of the
// new sentence to reverse the word order.
const reversed = await words.aggregate((workingSentence, next) =>
next + " " + workingSentence)
expect(reversed).toBe("dog lazy the over jumps fox brown quick the")
})
itParallel<string>("Basic", async (asParallel) => {
const asyncArray = asParallel(["f", "o", "o"])
expect(await asyncArray.aggregate((x, y) => x + y)).toBe("foo")
const sentence = "the quick brown fox jumps over the lazy dog"
// Split the string into individual words.
const words = asParallel(sentence.split(" "))
// Prepend each word to the beginning of the
// new sentence to reverse the word order.
const reversed = await words.aggregate((workingSentence, next) =>
next + " " + workingSentence)
expect(reversed).toBe("dog lazy the over jumps fox brown quick the")
})
itEnumerable<string>("ResultSelector", (asEnumerable) => {
const fruits = asEnumerable([ "apple", "mango", "orange", "passionfruit", "grape" ])
// Determine whether any string in the array is longer than "banana".
const longestName = fruits.aggregate(
"banana",
(longest, next) => next.length > longest.length ? next : longest,
// Return the final result as an upper case string.
(fruit) => fruit.toUpperCase())
expect(longestName).toBe("PASSIONFRUIT")
})
itAsync("ResultSelector", async () => {
const fruits = asAsync([ "apple", "mango", "orange", "passionfruit", "grape" ])
// Determine whether any string in the array is longer than "banana".
const longestName = await fruits.aggregate(
"banana",
(longest, next) => next.length > longest.length ? next : longest,
// Return the final result as an upper case string.
(fruit) => fruit.toUpperCase())
expect(longestName).toBe("PASSIONFRUIT")
})
itParallel<string>("ResultSelector", async (asParallel) => {
const fruits = asParallel([ "apple", "mango", "orange", "passionfruit", "grape" ])
// Determine whether any string in the array is longer than "banana".
const longestName = await fruits.aggregate(
"banana",
(longest, next) => next.length > longest.length ? next : longest,
// Return the final result as an upper case string.
(fruit) => fruit.toUpperCase())
expect(longestName).toBe("PASSIONFRUIT")
})
itEnumerable("SingleValue", (asEnumerable) => {
const val2 = asEnumerable([1]).aggregate((x, y) => x + y)
expect(val2).toBe(1)
})
itAsync("SingleValue", async () => {
const val2 = await asAsync([1]).aggregate((x, y) => x + y)
expect(val2).toBe(1)
})
itParallel<number>("SingleValue", async (asParallel) => {
const val2 = await asParallel([1]).aggregate((x, y) => x + y)
expect(val2).toBe(1)
})
itEnumerable("MultipleValues", (asEnumerable) => {
const val = asEnumerable([1, 2, 3]).aggregate((x, y) => x + y)
expect(val).toBe(6)
})
itAsync("MultipleValues", async () => {
const val = await asAsync([1, 2, 3]).aggregate((x, y) => x + y)
expect(val).toBe(6)
})
itParallel<number>("MultipleValues", async (asParallel) => {
const val = await asParallel([1, 2, 3]).aggregate((x, y) => x + y)
expect(val).toBe(6)
})
itEnumerable<any>("Exception", (asEnumerable) => {
expect(() => asEnumerable([]).aggregate((x, y) => x + y)).toThrowError(InvalidOperationException)
})
itAsync("Exception", async () => {
const expect = await expectAsync(asAsync([]).aggregate((x, y) => x + y))
expect.toThrowError(InvalidOperationException)
})
itParallel<number>("Exception", async (asParallel) => {
const expect = await expectAsync(asParallel([]).aggregate((x, y) => x + y))
expect.toThrowError(InvalidOperationException)
})
itEnumerable<number>("Aggregate2", (asEnumerable) => {
const val = asEnumerable([1, 2, 3]).aggregate(4, (x, y) => x + y)
expect(val).toBe(10)
const val2 = asEnumerable([1]).aggregate(9, (x, y) => x + y)
expect(val2).toBe(10)
const val3 = asEnumerable([] as number[]).aggregate(10, (x, y) => x + y)
expect(val3).toBe(10)
})
itAsync("Aggregate2", async () => {
const val = await asAsync([1, 2, 3]).aggregate(4, (x, y) => x + y)
expect(val).toBe(10)
const val2 = await asAsync([1]).aggregate(9, (x, y) => x + y)
expect(val2).toBe(10)
const val3 = await asAsync([] as number[]).aggregate(10, (x, y) => x + y)
expect(val3).toBe(10)
})
itParallel("Aggregate2", async (asParallel) => {
const val = await asParallel([1, 2, 3]).aggregate(4, (x, y) => x + y)
expect(val).toBe(10)
const val2 = await asParallel([1]).aggregate(9, (x, y) => x + y)
expect(val2).toBe(10)
const val3 = await asParallel([] as number[]).aggregate(10, (x, y) => x + y)
expect(val3).toBe(10)
})
itEnumerable("Aggregate3", (asEnumerable) => {
const val = asEnumerable([1, 2, 3]).aggregate(4, (x, y) => x + y, (acc) => acc * 10)
expect(val).toBe(100)
})
itAsync("Aggregate3", async () => {
const val = await asAsync([1, 2, 3]).aggregate(4, (x, y) => x + y, (acc) => acc * 10)
expect(val).toBe(100)
})
itParallel("Aggregate3", async (asParallel) => {
const val = await asParallel([1, 2, 3]).aggregate(4, (x, y) => x + y, (acc) => acc * 10)
expect(val).toBe(100)
})
})