/
for_of_test.js
129 lines (117 loc) · 3.1 KB
/
for_of_test.js
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
/*
* Copyright 2016 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview
* Tests ES6 for...of iteration.
*
* @author mattloring@google.com (Matthew Loring)
*/
goog.require('goog.testing.jsunit');
// https://github.com/google/closure-compiler/issues/717
function testIterationVarShadows() {
let x = 'outer';
let result = [];
for (let x of [5, 6, 7]) { // let: This is a new 'x' variable.
result.push(x);
}
assertEquals(5, result[0]);
assertEquals(6, result[1]);
assertEquals(7, result[2]);
assertEquals('outer', x);
}
function testIterationVarIsDeclaredInOuterScope() {
let x = 'outer';
let result = [];
for (x of [5, 6, 7]) { // no declaration: This is the same 'x' as above.
result.push(x);
}
assertEquals(5, result[0]);
assertEquals(6, result[1]);
assertEquals(7, result[2]);
assertEquals(7, x);
}
function testForOfString() {
let result = [];
for (let c of 'a string') {
result.push(c);
}
assertArrayEquals(['a', ' ', 's', 't', 'r', 'i', 'n', 'g'], result);
}
function testArguments() {
function f(var_args) {
let result = [];
for (let a of arguments) {
result.push(a);
}
return result;
}
assertArrayEquals([5, 6, 7, 8], f(5, 6, 7, 8));
}
function testForOfArray() {
var arr = [5, 6, 7];
var result = [];
for (var val of arr) {
result.push(val);
}
assertEquals(5, result[0]);
assertEquals(6, result[1]);
assertEquals(7, result[2]);
}
function testForOfArrayLet() {
var arr = [5, 6, 7];
var result = [];
for (let val of arr) {
result.push(val);
}
assertEquals(5, result[0]);
assertEquals(6, result[1]);
assertEquals(7, result[2]);
}
function testForOfArrayNested() {
var arr = [5, 6, 7];
var result = [];
for (var val of arr) {
for (var valN of arr) {
result.push(val + valN);
}
}
assertEquals(10, result[0]);
assertEquals(11, result[1]);
assertEquals(12, result[2]);
assertEquals(11, result[3]);
assertEquals(12, result[4]);
assertEquals(13, result[5]);
assertEquals(12, result[6]);
assertEquals(13, result[7]);
assertEquals(14, result[8]);
}
function testForOfWithGetPropInitializer() {
const /** {a: number} */ obj = {a: 3};
for (obj.a of [4]) {}
assertEquals(4, obj.a);
}
function testForOfWithGetElemInitializer() {
const /** !Object<number, number> */ obj = {};
const /** !Array<number> */ arr = [5, 6, 7];
const /** !Array<number> */ newArr = [];
var index = 0;
for (obj[index] of arr) {
newArr.push(obj[index]);
index++;
}
assertArrayEquals(arr, newArr);
assertObjectEquals({0: 5, 1: 6, 2: 7}, obj);
}