|
1 | | -import { Expect, Test, TestCase, FocusTests } from "alsatian"; |
| 1 | +import { Expect, Test, TestCase } from "alsatian"; |
2 | 2 | import { TranspileError } from "../../src/TranspileError"; |
3 | 3 | import { LuaTarget } from "../../src/CompilerOptions"; |
4 | 4 |
|
@@ -184,6 +184,44 @@ export class ExpressionTests { |
184 | 184 | Expect(util.transpileString("undefined")).toBe("nil;"); |
185 | 185 | } |
186 | 186 |
|
| 187 | + @TestCase("true ? 'a' : 'b'", "a") |
| 188 | + @TestCase("false ? 'a' : 'b'", "b") |
| 189 | + @TestCase("true ? false : true", false) |
| 190 | + @TestCase("false ? false : true", true) |
| 191 | + @TestCase("true ? literalValue : true", "literal") |
| 192 | + @TestCase("true ? variableValue : true", undefined) |
| 193 | + @TestCase("true ? maybeUndefinedValue : true", undefined) |
| 194 | + @TestCase("true ? maybeBooleanValue : true", false) |
| 195 | + @TestCase("true ? maybeUndefinedValue : true", undefined, { strictNullChecks: true }) |
| 196 | + @TestCase("true ? maybeBooleanValue : true", false, { strictNullChecks: true }) |
| 197 | + @TestCase("true ? undefined : true", undefined, { strictNullChecks: true }) |
| 198 | + @TestCase("true ? null : true", undefined, { strictNullChecks: true }) |
| 199 | + @TestCase("true ? false : true", false, { luaTarget: LuaTarget.Lua51 }) |
| 200 | + @TestCase("false ? false : true", true, { luaTarget: LuaTarget.Lua51 }) |
| 201 | + @TestCase("true ? undefined : true", undefined, { luaTarget: LuaTarget.Lua51 }) |
| 202 | + @TestCase("true ? false : true", false, { luaTarget: LuaTarget.LuaJIT }) |
| 203 | + @TestCase("false ? false : true", true, { luaTarget: LuaTarget.LuaJIT }) |
| 204 | + @TestCase("true ? undefined : true", undefined, { luaTarget: LuaTarget.LuaJIT }) |
| 205 | + @Test("Ternary operator") |
| 206 | + public ternaryOperator(input: string, expected: any, options?: ts.CompilerOptions): void { |
| 207 | + console.log(util.transpileString( |
| 208 | + `const literalValue = 'literal'; |
| 209 | + let variableValue:string; |
| 210 | + let maybeBooleanValue:string|boolean = false; |
| 211 | + let maybeUndefinedValue:string|undefined; |
| 212 | + return ${input};`, options |
| 213 | + |
| 214 | + )); |
| 215 | + const result = util.transpileAndExecute( |
| 216 | + `const literalValue = 'literal'; |
| 217 | + let variableValue:string; |
| 218 | + let maybeBooleanValue:string|boolean = false; |
| 219 | + let maybeUndefinedValue:string|undefined; |
| 220 | + return ${input};`, options); |
| 221 | + |
| 222 | + Expect(result).toBe(expected); |
| 223 | + } |
| 224 | + |
187 | 225 | @TestCase("inst.field", 8) |
188 | 226 | @TestCase("inst.field + 3", 8 + 3) |
189 | 227 | @TestCase("inst.field * 3", 8 * 3) |
@@ -246,7 +284,7 @@ export class ExpressionTests { |
246 | 284 | @TestCase("inst.superBaseField", 4) |
247 | 285 | @Test("Inherited accessors") |
248 | 286 | public inheritedAccessors(expression: string, expected: any): void { |
249 | | - const source = `class MyBaseClass {` |
| 287 | + const source = `class MyBaseClass {` |
250 | 288 | + ` public _baseField: number;` |
251 | 289 | + ` public get baseField(): number { return this._baseField + 6; }` |
252 | 290 | + ` public set baseField(v: number) { this._baseField = v; }` |
|
0 commit comments