-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.test.ts
More file actions
91 lines (73 loc) · 2.43 KB
/
Copy pathindex.test.ts
File metadata and controls
91 lines (73 loc) · 2.43 KB
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
import { List, MyIterator } from './example_1';
import { List as List2 } from './example_2';
import { List as ListGenerator } from './example_3';
describe('Iterator Pattern', () => {
describe('Example 1 Tests', () => {
it('should iterate successfuly with elements', () => {
const list: List<number> = new List<number>(1, 2, 3);
const iterator: MyIterator = list.createIterator();
const output: number[] = [];
while (iterator.hasNext()) {
output.push(iterator.next() as number);
}
expect(output).toEqual([1, 2, 3]);
});
it('should iterate successfuly without elements', () => {
const list: List<number> = new List<number>();
const iterator: MyIterator = list.createIterator();
const output: number[] = [];
while (iterator.hasNext()) {
output.push(iterator.next() as number);
}
expect(output).toEqual([]);
});
});
describe('Example 2 Tests', () => {
it('should iterate successfuly with for of loop', () => {
const list: List2<number> = new List2<number>(1, 2, 3);
const output: number[] = [];
for (const item of list) {
output.push(item);
}
expect(output).toEqual([1, 2, 3]);
});
it('should iterate successfuly with iterator', () => {
const list: List2<number> = new List2<number>(1, 2, 3);
const iterator = list[Symbol.iterator]();
const output: number[] = [];
do {
const next = iterator.next();
if (next.done) {
break;
} else {
output.push(next.value as number);
}
} while (true);
expect(output).toEqual([1, 2, 3]);
});
});
describe('Example 3 Tests', () => {
it('should iterate successfuly with for of loop', () => {
const list: ListGenerator<number> = new ListGenerator<number>(1, 2, 3);
const output: number[] = [];
for (const item of list) {
output.push(item);
}
expect(output).toEqual([1, 2, 3]);
});
it('should iterate successfuly with iterator', () => {
const list: ListGenerator<number> = new ListGenerator<number>(1, 2, 3);
const iterator = list[Symbol.iterator]();
const output: number[] = [];
do {
const next = iterator.next();
if (next.done) {
break;
} else {
output.push(next.value as number);
}
} while (true);
expect(output).toEqual([1, 2, 3]);
});
});
});