1+ import { Expect , Test , TestCase } from "alsatian" ;
2+
3+ import * as ts from "typescript" ;
4+ import { LuaTranspiler , TranspileError } from "../../dist/Transpiler" ;
5+
6+ const dummyChecker = { getTypeAtLocation : function ( ) { return { } ; } }
7+ function transpileString ( str : string ) : string {
8+ const file = ts . createSourceFile ( "" , str , ts . ScriptTarget . Latest ) ;
9+ const result = LuaTranspiler . transpileSourceFile ( file , dummyChecker , false ) ;
10+ return result . trim ( ) ;
11+ }
12+
13+ export class ExpressionTests {
14+
15+ @TestCase ( "i++" , "i=i+1" )
16+ @TestCase ( "++i" , "i=i+1" )
17+ @TestCase ( "i--" , "i=i-1" )
18+ @TestCase ( "--i" , "i=i-1" )
19+ @TestCase ( "!a" , "not a" )
20+ @TestCase ( "-a" , "-a" )
21+ @Test ( "Unary expressions basic" )
22+ public unaryBasic ( input : string , lua : string ) {
23+ Expect ( transpileString ( input ) ) . toBe ( lua ) ;
24+ }
25+
26+ @TestCase ( "1+1" , "1+1" )
27+ @TestCase ( "1-1" , "1-1" )
28+ @TestCase ( "1*1" , "1*1" )
29+ @TestCase ( "1/1" , "1/1" )
30+ @TestCase ( "1%1" , "1%1" )
31+ @TestCase ( "1==1" , "1==1" )
32+ @Test ( "Binary expressions basic" )
33+ public binary ( input : string , lua : string ) {
34+ Expect ( transpileString ( input ) ) . toBe ( lua ) ;
35+ }
36+
37+ @TestCase ( "a+=b" , "a=a+b" )
38+ @TestCase ( "a-=b" , "a=a-b" )
39+ @TestCase ( "a*=b" , "a=a*b" )
40+ @TestCase ( "a/=b" , "a=a/b" )
41+ @TestCase ( "a&b" , "bit.band(a,b)" )
42+ @TestCase ( "a|b" , "bit.bor(a,b)" )
43+ @Test ( "Binary expressions overridden operators" )
44+ public binaryOperatorOverride ( input : string , lua : string ) {
45+ Expect ( transpileString ( input ) ) . toBe ( lua ) ;
46+ }
47+
48+ @TestCase ( "1+1" , "1+1" )
49+ @TestCase ( "-1+1" , "-1+1" )
50+ @TestCase ( "1*30+4" , "(1*30)+4" )
51+ @TestCase ( "1*(3+4)" , "1*(3+4)" )
52+ @TestCase ( "1*(3+4*2)" , "1*(3+(4*2))" )
53+ @Test ( "Binary expressions ordering parentheses" )
54+ public binaryParentheses ( input : string , lua : string ) {
55+ Expect ( transpileString ( input ) ) . toBe ( lua ) ;
56+ }
57+
58+ @TestCase ( "1 + a ? 3*a : c" , "TS_ITE(1+a,function() return 3*a end,function() return c end)" )
59+ @TestCase ( "a ? b : c" , "TS_ITE(a,function() return b end,function() return c end)" )
60+ @Test ( "Ternary operator" )
61+ public conditional ( input : string , lua : string ) {
62+ Expect ( transpileString ( input ) ) . toBe ( lua ) ;
63+ }
64+ }
0 commit comments