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