| 1 |
import * as evaluator from "../../_namespaces/evaluator"; |
| 2 |
import * as ts from "../../_namespaces/ts"; |
| 3 |
|
| 4 |
describe("unittests:: evaluation:: forOfEvaluation", () => { |
| 5 |
it("es5 over a array with no Symbol", () => { |
| 6 |
const result = evaluator.evaluateTypeScript(` |
| 7 |
Symbol = undefined; |
| 8 |
export var output = []; |
| 9 |
export function main() { |
| 10 |
let x = [1,2,3]; |
| 11 |
|
| 12 |
for (let value of x) { |
| 13 |
output.push(value); |
| 14 |
} |
| 15 |
} |
| 16 |
`, { downlevelIteration: true, target: ts.ScriptTarget.ES5 }); |
| 17 |
|
| 18 |
result.main(); |
| 19 |
|
| 20 |
assert.strictEqual(result.output[0], 1); |
| 21 |
assert.strictEqual(result.output[1], 2); |
| 22 |
assert.strictEqual(result.output[2], 3); |
| 23 |
|
| 24 |
}); |
| 25 |
|
| 26 |
it("es5 over a string with no Symbol", () => { |
| 27 |
const result = evaluator.evaluateTypeScript(` |
| 28 |
Symbol = undefined; |
| 29 |
export var output = []; |
| 30 |
export function main() { |
| 31 |
let x = "hello"; |
| 32 |
|
| 33 |
for (let value of x) { |
| 34 |
output.push(value); |
| 35 |
} |
| 36 |
} |
| 37 |
`, { downlevelIteration: true, target: ts.ScriptTarget.ES5 }); |
| 38 |
|
| 39 |
result.main(); |
| 40 |
|
| 41 |
assert.strictEqual(result.output[0], "h"); |
| 42 |
assert.strictEqual(result.output[1], "e"); |
| 43 |
assert.strictEqual(result.output[2], "l"); |
| 44 |
assert.strictEqual(result.output[3], "l"); |
| 45 |
assert.strictEqual(result.output[4], "o"); |
| 46 |
}); |
| 47 |
|
| 48 |
it("es5 over undefined with no Symbol", () => { |
| 49 |
const result = evaluator.evaluateTypeScript(` |
| 50 |
Symbol = undefined; |
| 51 |
export function main() { |
| 52 |
let x = undefined; |
| 53 |
|
| 54 |
for (let value of x) { |
| 55 |
} |
| 56 |
} |
| 57 |
`, { downlevelIteration: true, target: ts.ScriptTarget.ES5 }); |
| 58 |
|
| 59 |
assert.throws(() => result.main(), "Symbol.iterator is not defined"); |
| 60 |
}); |
| 61 |
|
| 62 |
it("es5 over undefined with Symbol", () => { |
| 63 |
const result = evaluator.evaluateTypeScript(` |
| 64 |
export function main() { |
| 65 |
let x = undefined; |
| 66 |
|
| 67 |
for (let value of x) { |
| 68 |
} |
| 69 |
} |
| 70 |
`, { downlevelIteration: true, target: ts.ScriptTarget.ES5 }); |
| 71 |
|
| 72 |
assert.throws(() => result.main(), /cannot read property.*Symbol\(Symbol\.iterator\).*/i); |
| 73 |
}); |
| 74 |
|
| 75 |
it("es5 over object with no Symbol.iterator with no Symbol", () => { |
| 76 |
const result = evaluator.evaluateTypeScript(` |
| 77 |
Symbol = undefined; |
| 78 |
export function main() { |
| 79 |
let x = {} as any; |
| 80 |
|
| 81 |
for (let value of x) { |
| 82 |
} |
| 83 |
} |
| 84 |
`, { downlevelIteration: true, target: ts.ScriptTarget.ES5 }); |
| 85 |
|
| 86 |
assert.throws(() => result.main(), "Symbol.iterator is not defined"); |
| 87 |
}); |
| 88 |
|
| 89 |
it("es5 over object with no Symbol.iterator with Symbol", () => { |
| 90 |
const result = evaluator.evaluateTypeScript(` |
| 91 |
export function main() { |
| 92 |
let x = {} as any; |
| 93 |
|
| 94 |
for (let value of x) { |
| 95 |
} |
| 96 |
} |
| 97 |
`, { downlevelIteration: true, target: ts.ScriptTarget.ES5 }); |
| 98 |
|
| 99 |
assert.throws(() => result.main(), "Object is not iterable"); |
| 100 |
}); |
| 101 |
|
| 102 |
it("es5 over object with Symbol.iterator", () => { |
| 103 |
const result = evaluator.evaluateTypeScript(` |
| 104 |
export var output = []; |
| 105 |
export function main() { |
| 106 |
let thing : any = {}; |
| 107 |
thing[Symbol.iterator] = () => { |
| 108 |
let i = 0; |
| 109 |
return { next() { i++; return this; }, value: i, done: i < 10 }; |
| 110 |
}; |
| 111 |
|
| 112 |
for (let value of thing) |
| 113 |
{ |
| 114 |
output.push(value) |
| 115 |
} |
| 116 |
|
| 117 |
}`, { downlevelIteration: true, target: ts.ScriptTarget.ES5 }); |
| 118 |
|
| 119 |
result.main(); |
| 120 |
}); |
| 121 |
|
| 122 |
}); |