|
| 1 | +import * as util from "../../util"; |
| 2 | +import { invalidPairsIterableWithoutDestructuring } from "../../../src/transformation/utils/diagnostics"; |
| 3 | +import { LuaTarget } from "../../../src"; |
| 4 | + |
| 5 | +const testIterable = ` |
| 6 | +const testIterable = {a1: "a2", b1: "b2", c1: "c2"} as unknown as LuaPairsIterable<string, string>; |
| 7 | +`; |
| 8 | + |
| 9 | +const testResults = { |
| 10 | + a1: "a2", |
| 11 | + b1: "b2", |
| 12 | + c1: "c2", |
| 13 | +}; |
| 14 | + |
| 15 | +test("pairs iterable", () => { |
| 16 | + util.testFunction` |
| 17 | + ${testIterable} |
| 18 | + const results: Record<string, string> = {}; |
| 19 | + for (const [k, v] of testIterable) { |
| 20 | + results[k] = v; |
| 21 | + } |
| 22 | + return results; |
| 23 | + ` |
| 24 | + .withLanguageExtensions() |
| 25 | + .expectToEqual(testResults); |
| 26 | +}); |
| 27 | + |
| 28 | +test("pairs iterable with external control variable", () => { |
| 29 | + util.testFunction` |
| 30 | + ${testIterable} |
| 31 | + const results: Record<string, string> = {}; |
| 32 | + let k: string, v: string; |
| 33 | + for ([k, v] of testIterable) { |
| 34 | + results[k] = v; |
| 35 | + } |
| 36 | + return results; |
| 37 | + ` |
| 38 | + .withLanguageExtensions() |
| 39 | + .expectToEqual(testResults); |
| 40 | +}); |
| 41 | + |
| 42 | +test("pairs iterable function forward", () => { |
| 43 | + util.testFunction` |
| 44 | + ${testIterable} |
| 45 | + function forward() { return testIterable; } |
| 46 | + const results: Record<string, string> = {}; |
| 47 | + for (const [k, v] of forward()) { |
| 48 | + results[k] = v; |
| 49 | + } |
| 50 | + return results; |
| 51 | + ` |
| 52 | + .withLanguageExtensions() |
| 53 | + .expectToEqual(testResults); |
| 54 | +}); |
| 55 | + |
| 56 | +test("pairs iterable function indirect forward", () => { |
| 57 | + util.testFunction` |
| 58 | + ${testIterable} |
| 59 | + function forward() { const iter = testIterable; return iter; } |
| 60 | + const results: Record<string, string> = {}; |
| 61 | + for (const [k, v] of forward()) { |
| 62 | + results[k] = v; |
| 63 | + } |
| 64 | + return results; |
| 65 | + ` |
| 66 | + .withLanguageExtensions() |
| 67 | + .expectToEqual(testResults); |
| 68 | +}); |
| 69 | + |
| 70 | +test("pairs iterable arrow function forward", () => { |
| 71 | + util.testFunction` |
| 72 | + ${testIterable} |
| 73 | + const forward = () => testIterable; |
| 74 | + const results: Record<string, string> = {}; |
| 75 | + for (const [k, v] of forward()) { |
| 76 | + results[k] = v; |
| 77 | + } |
| 78 | + return results; |
| 79 | + ` |
| 80 | + .withLanguageExtensions() |
| 81 | + .expectToEqual(testResults); |
| 82 | +}); |
| 83 | + |
| 84 | +test("pairs iterable with __pairs metamethod", () => { |
| 85 | + util.testFunction` |
| 86 | + class PairsTest { |
| 87 | + __pairs() { |
| 88 | + const kvp = [ ["a1", "a2"], ["b1", "b2"], ["c1", "c2"] ]; |
| 89 | + let i = 0; |
| 90 | + return () => { |
| 91 | + if (i < kvp.length) { |
| 92 | + const [k, v] = kvp[i++]; |
| 93 | + return $multi(k, v); |
| 94 | + } |
| 95 | + }; |
| 96 | + } |
| 97 | + } |
| 98 | + const tester = new PairsTest() as PairsTest & LuaPairsIterable<string, string>; |
| 99 | + const results: Record<string, string> = {}; |
| 100 | + for (const [k, v] of tester) { |
| 101 | + results[k] = v; |
| 102 | + } |
| 103 | + return results; |
| 104 | + ` |
| 105 | + .withLanguageExtensions() |
| 106 | + .setOptions({ luaTarget: LuaTarget.Lua53 }) |
| 107 | + .expectToEqual(testResults); |
| 108 | +}); |
| 109 | + |
| 110 | +test.each(["for (const s of testIterable) {}", "let s; for (s of testIterable) {}"])( |
| 111 | + "invalid LuaPairsIterable without destructuring (%p)", |
| 112 | + statement => { |
| 113 | + util.testFunction` |
| 114 | + ${testIterable} |
| 115 | + ${statement} |
| 116 | + ` |
| 117 | + .withLanguageExtensions() |
| 118 | + .expectDiagnosticsToMatchSnapshot([invalidPairsIterableWithoutDestructuring.code]); |
| 119 | + } |
| 120 | +); |
0 commit comments