|
| 1 | +import { Expect, Test } from "alsatian"; |
| 2 | +import * as util from "../../src/util"; |
| 3 | + |
| 4 | +export class WeakMapTests { |
| 5 | + private initRefsTs = `let ref = {}; |
| 6 | + let ref2 = () => {};`; |
| 7 | + |
| 8 | + @Test("weakMap constructor") |
| 9 | + public weakMapConstructor(): void |
| 10 | + { |
| 11 | + const result = util.transpileAndExecute(this.initRefsTs + ` |
| 12 | + let mymap = new WeakMap([[ref, 1]]); |
| 13 | + return mymap.get(ref); |
| 14 | + `); |
| 15 | + |
| 16 | + Expect(result).toBe(1); |
| 17 | + } |
| 18 | + |
| 19 | + @Test("weakMap iterable constructor") |
| 20 | + public weakMapIterableConstructor(): void |
| 21 | + { |
| 22 | + const result = util.transpileAndExecute(this.initRefsTs + ` |
| 23 | + let mymap = new WeakMap([[ref, 1], [ref2, 2]]); |
| 24 | + return mymap.has(ref) && mymap.has(ref2); |
| 25 | + `); |
| 26 | + |
| 27 | + Expect(result).toBe(true); |
| 28 | + } |
| 29 | + |
| 30 | + @Test("weakMap iterable constructor map") |
| 31 | + public weakMapIterableConstructor2(): void |
| 32 | + { |
| 33 | + const result = util.transpileAndExecute(this.initRefsTs + ` |
| 34 | + let mymap = new WeakMap(new Map([[ref, 1], [ref2, 2]])); |
| 35 | + return mymap.has(ref) && mymap.has(ref2); |
| 36 | + `); |
| 37 | + |
| 38 | + Expect(result).toBe(true); |
| 39 | + } |
| 40 | + |
| 41 | + @Test("weakMap delete") |
| 42 | + public weakMapDelete(): void |
| 43 | + { |
| 44 | + const contains = util.transpileAndExecute(this.initRefsTs + ` |
| 45 | + let mymap = new WeakMap([[ref, true], [ref2, true]]); |
| 46 | + mymap.delete(ref2); |
| 47 | + return mymap.has(ref) && !mymap.has(ref2); |
| 48 | + `); |
| 49 | + |
| 50 | + Expect(contains).toBe(true); |
| 51 | + } |
| 52 | + |
| 53 | + @Test("weakMap get") |
| 54 | + public weakMapGet(): void |
| 55 | + { |
| 56 | + const result = util.transpileAndExecute(this.initRefsTs + ` |
| 57 | + let mymap = new WeakMap([[ref, 1], [{}, 2]]); |
| 58 | + return mymap.get(ref); |
| 59 | + `); |
| 60 | + |
| 61 | + Expect(result).toBe(1); |
| 62 | + } |
| 63 | + |
| 64 | + @Test("weakMap get missing") |
| 65 | + public weakMapGetMissing(): void |
| 66 | + { |
| 67 | + const result = util.transpileAndExecute(this.initRefsTs + ` |
| 68 | + let mymap = new WeakMap([[{}, true]]); |
| 69 | + return mymap.get({}); |
| 70 | + `); |
| 71 | + |
| 72 | + Expect(result).toBe(undefined); |
| 73 | + } |
| 74 | + |
| 75 | + @Test("weakMap has") |
| 76 | + public weakMapHas(): void |
| 77 | + { |
| 78 | + const contains = util.transpileAndExecute(this.initRefsTs + ` |
| 79 | + let mymap = new WeakMap([[ref, true]]); |
| 80 | + return mymap.has(ref); |
| 81 | + `); |
| 82 | + |
| 83 | + Expect(contains).toBe(true); |
| 84 | + } |
| 85 | + |
| 86 | + @Test("weakMap has false") |
| 87 | + public weakMapHasFalse(): void |
| 88 | + { |
| 89 | + const contains = util.transpileAndExecute(this.initRefsTs + ` |
| 90 | + let mymap = new WeakMap([[ref, true]]); |
| 91 | + return mymap.has(ref2); |
| 92 | + `); |
| 93 | + |
| 94 | + Expect(contains).toBe(false); |
| 95 | + } |
| 96 | + |
| 97 | + @Test("weakMap has null") |
| 98 | + public weakMapHasNull(): void |
| 99 | + { |
| 100 | + const contains = util.transpileAndExecute(this.initRefsTs + ` |
| 101 | + let mymap = new WeakMap([[{}, true]]); |
| 102 | + return mymap.has(null); |
| 103 | + `); |
| 104 | + |
| 105 | + Expect(contains).toBe(false); |
| 106 | + } |
| 107 | + |
| 108 | + @Test("weakMap set") |
| 109 | + public weakMapSet(): void |
| 110 | + { |
| 111 | + const init = this.initRefsTs + ` |
| 112 | + let mymap = new WeakMap(); |
| 113 | + mymap.set(ref, 5); |
| 114 | + `; |
| 115 | + |
| 116 | + const has = util.transpileAndExecute(init + `return mymap.has(ref);`); |
| 117 | + Expect(has).toBe(true); |
| 118 | + |
| 119 | + const value = util.transpileAndExecute(init + `return mymap.get(ref)`); |
| 120 | + Expect(value).toBe(5); |
| 121 | + } |
| 122 | + |
| 123 | + @Test("weakMap has no map features") |
| 124 | + public weakMapHasNoMapFeatures(): void |
| 125 | + { |
| 126 | + const transpileAndExecute = (tsStr: string) => util.transpileAndExecute(tsStr, undefined, undefined, undefined, true); |
| 127 | + Expect(transpileAndExecute(`return new WeakMap().size`)).toBe(undefined); |
| 128 | + Expect(() => transpileAndExecute(`new WeakMap().clear()`)).toThrow(); |
| 129 | + Expect(() => transpileAndExecute(`new WeakMap().keys()`)).toThrow(); |
| 130 | + Expect(() => transpileAndExecute(`new WeakMap().values()`)).toThrow(); |
| 131 | + Expect(() => transpileAndExecute(`new WeakMap().entries()`)).toThrow(); |
| 132 | + Expect(() => transpileAndExecute(`new WeakMap().forEach(() => {})`)).toThrow(); |
| 133 | + } |
| 134 | +} |
0 commit comments