]>
Commit | Line | Data |
---|---|---|
ed1e77d3 A |
1 | // This test checks the shape of builtin iterators. |
2 | ||
3 | function iteratorShape(iter) { | |
4 | if (iter.hasOwnProperty('next')) | |
5 | throw "Error: iterator should not have next method."; | |
6 | if (!iter.__proto__.hasOwnProperty('next')) | |
7 | throw "Error: iterator prototype should have next method."; | |
8 | if (typeof iter.__proto__.next !== "function") | |
9 | throw "Error: iterator prototype should have next method."; | |
10 | } | |
11 | ||
12 | function sameNextMethods(iterators) { | |
13 | var iterator = iterators[0]; | |
14 | for (var i = 1; i < iterators.length; ++i) { | |
15 | if (iterator.next !== iterators[i].next) | |
16 | throw "Error: next method is not the same."; | |
17 | } | |
18 | } | |
19 | ||
20 | var array = ['Cocoa', 'Cappuccino', 'The des Alizes', 'Matcha', 'Kilimanjaro']; | |
21 | var iterator = array[Symbol.iterator](); | |
22 | iteratorShape(iterator); | |
23 | ||
24 | var keyIterator = array.keys(); | |
25 | iteratorShape(keyIterator); | |
26 | ||
27 | var keyValueIterator = array.entries(); | |
28 | iteratorShape(keyValueIterator); | |
29 | ||
30 | sameNextMethods([array[Symbol.iterator](), array.keys(), array.entries()]); | |
31 | ||
32 | var set = new Set(['Cocoa', 'Cappuccino', 'The des Alizes', 'Matcha', 'Kilimanjaro']); | |
33 | var iterator = set[Symbol.iterator](); | |
34 | iteratorShape(iterator); | |
35 | ||
36 | var keyIterator = set.keys(); | |
37 | iteratorShape(keyIterator); | |
38 | ||
39 | var keyValueIterator = set.entries(); | |
40 | iteratorShape(keyValueIterator); | |
41 | ||
42 | sameNextMethods([set[Symbol.iterator](), set.keys(), set.entries()]); | |
43 | ||
44 | var map = new Map(); | |
45 | [ | |
46 | [ 'Cocoa', 2, ], | |
47 | [ 'Cappuccino', 0 ], | |
48 | [ 'The des Alizes', 3 ], | |
49 | [ 'Matcha', 2 ], | |
50 | [ 'Kilimanjaro', 1] | |
51 | ].forEach(function ([ key, value ]) { | |
52 | map.set(key, value); | |
53 | }); | |
54 | var iterator = map[Symbol.iterator](); | |
55 | iteratorShape(iterator); | |
56 | ||
57 | var keyIterator = map.keys(); | |
58 | iteratorShape(keyIterator); | |
59 | ||
60 | var keyValueIterator = map.entries(); | |
61 | iteratorShape(keyValueIterator); | |
62 | ||
63 | sameNextMethods([map[Symbol.iterator](), map.keys(), map.entries()]); |