]>
Commit | Line | Data |
---|---|---|
1 | function shouldBe(actual, expected) { | |
2 | if (actual !== expected) | |
3 | throw new Error('bad value: ' + actual); | |
4 | } | |
5 | ||
6 | // Set is iterable. | |
7 | var set = new Set([0, 1, 2, 3, 4, 5]); | |
8 | var array = Array.from(set); | |
9 | ||
10 | shouldBe(array.length, set.size); | |
11 | for (var i = 0; i < array.length; ++i) { | |
12 | shouldBe(set.has(array[i]), true); | |
13 | } | |
14 | ||
15 | // Map is iterable. | |
16 | var map = new Map([ | |
17 | [0, 0], | |
18 | [1, 0], | |
19 | [2, 0], | |
20 | [3, 0], | |
21 | [4, 0], | |
22 | [5, 0] | |
23 | ]); | |
24 | var array = Array.from(map); | |
25 | ||
26 | shouldBe(array.length, map.size); | |
27 | for (var i = 0; i < array.length; ++i) { | |
28 | shouldBe(array[i][1], 0); | |
29 | shouldBe(map.has(array[i][0]), true); | |
30 | shouldBe(map.get(array[i][0]), 0); | |
31 | } | |
32 | ||
33 | // String is iterable | |
34 | var string = "Cocoa Cappuccino"; | |
35 | var array = Array.from(string); | |
36 | shouldBe(array.length, string.length); | |
37 | for (var i = 0; i < array.length; ++i) { | |
38 | shouldBe(array[i], string[i]); | |
39 | } | |
40 | ||
41 | // Arguments is iterable | |
42 | var argumentsGenerators = [ | |
43 | function () { | |
44 | return arguments; | |
45 | }, | |
46 | ||
47 | function () { | |
48 | 'use strict'; | |
49 | return arguments; | |
50 | }, | |
51 | ||
52 | function (a, b, c) { | |
53 | return arguments; | |
54 | }, | |
55 | ||
56 | function (a, b, c) { | |
57 | 'use strict'; | |
58 | return arguments; | |
59 | } | |
60 | ]; | |
61 | ||
62 | for (var gen of argumentsGenerators) { | |
63 | var args = gen(1, 2, 3, 4, 5); | |
64 | var array = Array.from(args); | |
65 | shouldBe(array.length, args.length); | |
66 | for (var i = 0; i < array.length; ++i) { | |
67 | shouldBe(array[i], args[i]); | |
68 | } | |
69 | } |