| 1 |
import * as evaluator from "../../_namespaces/evaluator"; |
| 2 |
import * as ts from "../../_namespaces/ts"; |
| 3 |
|
| 4 |
describe("unittests:: evaluation:: superInStaticInitializer", () => { |
| 5 |
it("super-property-get in es2015", () => { |
| 6 |
const result = evaluator.evaluateTypeScript(` |
| 7 |
export function main() { |
| 8 |
class Base { |
| 9 |
static x = 1; |
| 10 |
} |
| 11 |
class Derived extends Base { |
| 12 |
static y = super.x; |
| 13 |
} |
| 14 |
return [ |
| 15 |
Base, |
| 16 |
Derived |
| 17 |
]; |
| 18 |
} |
| 19 |
`, { target: ts.ScriptTarget.ES2015 }); |
| 20 |
const [Base, Derived] = result.main(); |
| 21 |
assert.strictEqual(Base.x, 1); |
| 22 |
assert.strictEqual(Derived.y, 1); |
| 23 |
}); |
| 24 |
it("super-property-set in es2015", () => { |
| 25 |
const result = evaluator.evaluateTypeScript(` |
| 26 |
export function main() { |
| 27 |
class Base { |
| 28 |
static x = 1; |
| 29 |
} |
| 30 |
class Derived extends Base { |
| 31 |
static y = super.x++; |
| 32 |
} |
| 33 |
return [ |
| 34 |
Base, |
| 35 |
Derived |
| 36 |
]; |
| 37 |
} |
| 38 |
`, { target: ts.ScriptTarget.ES2015 }); |
| 39 |
const [Base, Derived] = result.main(); |
| 40 |
assert.strictEqual(Base.x, 1); |
| 41 |
assert.strictEqual(Derived.x, 2); |
| 42 |
assert.strictEqual(Derived.y, 1); |
| 43 |
}); |
| 44 |
it("super-accessor-get in es2015", () => { |
| 45 |
const result = evaluator.evaluateTypeScript(` |
| 46 |
export function main() { |
| 47 |
let thisInBase; |
| 48 |
class Base { |
| 49 |
static _x = 1; |
| 50 |
static get x() { |
| 51 |
thisInBase = this; |
| 52 |
return this._x; |
| 53 |
} |
| 54 |
} |
| 55 |
class Derived extends Base { |
| 56 |
static y = super.x; |
| 57 |
} |
| 58 |
return [ |
| 59 |
Base, |
| 60 |
Derived, |
| 61 |
thisInBase |
| 62 |
]; |
| 63 |
} |
| 64 |
`, { target: ts.ScriptTarget.ES2015 }); |
| 65 |
const [Base, Derived, thisInBase] = result.main(); |
| 66 |
assert.strictEqual(Base._x, 1); |
| 67 |
assert.strictEqual(Derived.y, 1); |
| 68 |
assert.strictEqual(thisInBase, Derived); |
| 69 |
}); |
| 70 |
it("super-accessor-set in es2015", () => { |
| 71 |
const result = evaluator.evaluateTypeScript(` |
| 72 |
export function main() { |
| 73 |
let thisInBaseGet; |
| 74 |
let thisInBaseSet; |
| 75 |
class Base { |
| 76 |
static _x = 1; |
| 77 |
static get x() { |
| 78 |
thisInBaseGet = this; |
| 79 |
return this._x; |
| 80 |
} |
| 81 |
static set x(value) { |
| 82 |
thisInBaseSet = this; |
| 83 |
this._x = value; |
| 84 |
} |
| 85 |
} |
| 86 |
class Derived extends Base { |
| 87 |
static y = super.x++; |
| 88 |
} |
| 89 |
return [ |
| 90 |
Base, |
| 91 |
Derived, |
| 92 |
thisInBaseGet, |
| 93 |
thisInBaseSet |
| 94 |
]; |
| 95 |
} |
| 96 |
`, { target: ts.ScriptTarget.ES2015 }); |
| 97 |
const [Base, Derived, thisInBaseGet, thisInBaseSet] = result.main(); |
| 98 |
assert.strictEqual(Base._x, 1); |
| 99 |
assert.strictEqual(Derived._x, 2); |
| 100 |
assert.strictEqual(Derived.y, 1); |
| 101 |
assert.strictEqual(thisInBaseGet, Derived); |
| 102 |
assert.strictEqual(thisInBaseSet, Derived); |
| 103 |
}); |
| 104 |
it("super-call in es2015", () => { |
| 105 |
const result = evaluator.evaluateTypeScript(` |
| 106 |
export function main() { |
| 107 |
let thisInBase; |
| 108 |
class Base { |
| 109 |
static x() { |
| 110 |
thisInBase = this; |
| 111 |
return 1; |
| 112 |
} |
| 113 |
} |
| 114 |
class Derived extends Base { |
| 115 |
static y = super.x(); |
| 116 |
} |
| 117 |
return [ |
| 118 |
Derived, |
| 119 |
thisInBase, |
| 120 |
]; |
| 121 |
} |
| 122 |
`, { target: ts.ScriptTarget.ES2015 }); |
| 123 |
const [Derived, thisInBase] = result.main(); |
| 124 |
assert.strictEqual(Derived.y, 1); |
| 125 |
assert.strictEqual(thisInBase, Derived); |
| 126 |
}); |
| 127 |
it("super-call in es5", () => { |
| 128 |
const result = evaluator.evaluateTypeScript(` |
| 129 |
export function main() { |
| 130 |
let thisInBase; |
| 131 |
class Base { |
| 132 |
static x() { |
| 133 |
thisInBase = this; |
| 134 |
return 1; |
| 135 |
} |
| 136 |
} |
| 137 |
class Derived extends Base { |
| 138 |
static y = super.x(); |
| 139 |
} |
| 140 |
return [ |
| 141 |
Derived, |
| 142 |
thisInBase, |
| 143 |
]; |
| 144 |
} |
| 145 |
`, { target: ts.ScriptTarget.ES5 }); |
| 146 |
const [Derived, thisInBase] = result.main(); |
| 147 |
assert.strictEqual(Derived.y, 1); |
| 148 |
assert.strictEqual(thisInBase, Derived); |
| 149 |
}); |
| 150 |
it("super- and this-call in es2015", () => { |
| 151 |
const result = evaluator.evaluateTypeScript(` |
| 152 |
export function main() { |
| 153 |
class Base { |
| 154 |
static x() { |
| 155 |
return 1; |
| 156 |
} |
| 157 |
} |
| 158 |
class Derived extends Base { |
| 159 |
static x() { |
| 160 |
return super.x() + 1; |
| 161 |
} |
| 162 |
static y = this.x(); |
| 163 |
} |
| 164 |
return [ |
| 165 |
Derived, |
| 166 |
]; |
| 167 |
} |
| 168 |
`, { target: ts.ScriptTarget.ES2015 }); |
| 169 |
const [Derived] = result.main(); |
| 170 |
assert.strictEqual(Derived.y, 2); |
| 171 |
}); |
| 172 |
it("super- and this-call in es5", () => { |
| 173 |
const result = evaluator.evaluateTypeScript(` |
| 174 |
export function main() { |
| 175 |
class Base { |
| 176 |
static x() { |
| 177 |
return 1; |
| 178 |
} |
| 179 |
} |
| 180 |
class Derived extends Base { |
| 181 |
static x() { |
| 182 |
return super.x() + 1; |
| 183 |
} |
| 184 |
static y = this.x(); |
| 185 |
} |
| 186 |
return [ |
| 187 |
Derived, |
| 188 |
]; |
| 189 |
} |
| 190 |
`, { target: ts.ScriptTarget.ES2015 }); |
| 191 |
const [Derived] = result.main(); |
| 192 |
assert.strictEqual(Derived.y, 2); |
| 193 |
}); |
| 194 |
}); |