-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathFirst.ts
66 lines (49 loc) · 2.38 KB
/
First.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
import { InvalidOperationException } from "linq-to-typescript"
import { asAsync, expectAsync, itAsync, itEnumerable, itParallel } from "../TestHelpers"
describe("first", () => {
it("String", () => {
expect("abc".first()).toBe("a")
})
itEnumerable("Basic", (asEnumerable) =>
expect(asEnumerable([1]).first()).toBe(1))
itAsync("Basic", async () =>
expect(await asAsync([1]).first()).toBe(1))
itParallel("Basic", async (asParallel) =>
expect(await asParallel([1]).first()).toBe(1))
itEnumerable("FirstEmptyException", (asEnumerable) => {
expect(() => asEnumerable([]).first()).toThrowError(InvalidOperationException)
})
itAsync("FirstEmptyException", async () => {
const expect = await expectAsync(asAsync([]).first())
expect.toThrowError(InvalidOperationException)
})
itParallel("FirstEmptyException", async (asParallel) => {
const expect = await expectAsync(asParallel([]).first())
expect.toThrowError(InvalidOperationException)
})
itEnumerable("FirstPredicate", (asEnumerable) => {
expect(asEnumerable([1, 2]).first((x) => x === 2)).toBe(2)
})
itAsync("FirstPredicate", async () => {
expect(await asAsync([1, 2]).first((x) => x === 2)).toBe(2)
})
itParallel("FirstPredicate", async (asParallel) => {
expect(await asParallel([1, 2]).first((x) => x === 2)).toBe(2)
})
itEnumerable("Predicate", (asEnumerable) =>
expect(asEnumerable([1, 2, 3]).first((x) => x === 2)).toBe(2))
itAsync("Predicate", async () =>
expect(await asAsync([1, 2, 3]).first((x) => x === 2)).toBe(2))
itParallel("Predicate", async (asParallel) =>
expect(await asParallel([1, 2, 3]).first((x) => x === 2)).toBe(2))
itEnumerable("Empty array with predicate causes exception", (asEnumerable) =>
expect(() => asEnumerable([1, 2, 3]).first((x) => x === 4)).toThrowError(InvalidOperationException))
itAsync("Empty array with predicate causes exception", async () => {
const value = await expectAsync(asAsync([1, 2, 3]).first((x) => x === 4))
value.toThrowError(InvalidOperationException)
})
itParallel("Empty array with predicate causes exception", async (asParallel) => {
const value = await expectAsync(asParallel([1, 2, 3]).first((x) => x === 4))
value.toThrowError(InvalidOperationException)
})
})