Skip to content

Commit 4a6902d

Browse files
authored
fix lua null string (#995)
* fix lua null string replaces null strings with empty strings in lua * add lua translation tests * ignore lua execution tests
1 parent e25c260 commit 4a6902d

File tree

4 files changed

+142
-2
lines changed

4 files changed

+142
-2
lines changed

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/lua/translation/ExprTranslation.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ public static LuaExpr translate(ImMethodCall e, LuaTranslator tr) {
102102
}
103103

104104
public static LuaExpr translate(ImNull e, LuaTranslator tr) {
105-
return LuaAst.LuaExprNull();
105+
if(isStringType(e.getType())) {
106+
return LuaAst.LuaExprStringVal("");
107+
} else {
108+
return LuaAst.LuaExprNull();
109+
}
106110
}
107111

108112
public static LuaExpr translate(ImOperatorCall e, LuaTranslator tr) {
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package tests.wurstscript.tests;
2+
3+
import com.google.common.base.Charsets;
4+
import com.google.common.io.Files;
5+
import org.testng.annotations.Ignore;
6+
import org.testng.annotations.Test;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
11+
import static org.testng.AssertJUnit.*;
12+
13+
14+
public class LuaTranslationTests extends WurstScriptTest {
15+
16+
17+
@Test
18+
public void testStdLib() throws IOException {
19+
test().testLua(true).withStdLib().lines(
20+
"package Test",
21+
"import MagicFunctions",
22+
"init",
23+
" print(compiletime)"
24+
);
25+
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_testStdLib.lua"), Charsets.UTF_8);
26+
assertTrue(compiled.contains("MagicFunctions_compiletime"));
27+
}
28+
29+
@Ignore
30+
@Test
31+
public void testExecution() {
32+
test().testLua(true).executeProg().lines(
33+
"package Test",
34+
"native testSuccess()",
35+
"init",
36+
" testSuccess()"
37+
);
38+
}
39+
40+
@Test
41+
public void nullString1() throws IOException {
42+
test().testLua(true).lines(
43+
"package Test",
44+
"function nullString() returns string",
45+
" return null",
46+
"init",
47+
" nullString()"
48+
);
49+
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_nullString1.lua"), Charsets.UTF_8);
50+
assertTrue(compiled.contains("return \"\"") && !compiled.contains("return nil"));
51+
}
52+
53+
@Test
54+
public void nullString2() throws IOException {
55+
test().testLua(true).lines(
56+
"package Test",
57+
"function takesString(string s)",
58+
"init",
59+
" takesString(null)"
60+
);
61+
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_nullString2.lua"), Charsets.UTF_8);
62+
assertTrue(compiled.contains("takesString(\"\")") && !compiled.contains("takesString(nil)"));
63+
}
64+
65+
@Ignore
66+
@Test
67+
public void nullString3() {
68+
test().testLua(true).executeProg().lines(
69+
"package Test",
70+
"native testSuccess()",
71+
"function nullString() returns string",
72+
" return null",
73+
"function returnsString(string s) returns string",
74+
" return s + nullString() + s",
75+
"init",
76+
" let s = \".\"",
77+
" let r = returnsString(s)",
78+
" if r == s + s and r == \"..\"",
79+
" testSuccess()"
80+
);
81+
}
82+
83+
@Test
84+
public void nullObject1() throws IOException {
85+
test().testLua(true).lines(
86+
"package Test",
87+
"class A",
88+
"function nullObject() returns A",
89+
" return null",
90+
"init",
91+
" nullObject()"
92+
);
93+
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_nullObject1.lua"), Charsets.UTF_8);
94+
assertTrue(compiled.contains("return nil") && !compiled.contains("return \"\""));
95+
}
96+
97+
@Test
98+
public void nullObject2() throws IOException {
99+
test().testLua(true).lines(
100+
"package Test",
101+
"class A",
102+
"function takesObject(A a)",
103+
"init",
104+
" takesObject(null)"
105+
);
106+
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_nullObject2.lua"), Charsets.UTF_8);
107+
assertTrue(compiled.contains("takesObject(nil)") && !compiled.contains("takesObject(\"\")"));
108+
}
109+
110+
@Test
111+
public void nullUnit1() throws IOException {
112+
test().testLua(true).withStdLib().lines(
113+
"package Test",
114+
"function nullUnit() returns unit",
115+
" return null",
116+
"init",
117+
" nullUnit()"
118+
);
119+
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_nullUnit1.lua"), Charsets.UTF_8);
120+
assertTrue(compiled.contains("return nil") && !compiled.contains("return \"\""));
121+
}
122+
123+
@Test
124+
public void nullUnit2() throws IOException {
125+
test().testLua(true).withStdLib().lines(
126+
"package Test",
127+
"function takesUnit(unit u)",
128+
"init",
129+
" takesUnit(null)"
130+
);
131+
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_nullUnit2.lua"), Charsets.UTF_8);
132+
assertTrue(compiled.contains("takesUnit(nil)") && !compiled.contains("takesUnit(\"\")"));
133+
}
134+
}
135+

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/WurstScriptTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private CompilationResult testScript() {
205205

206206
testWithInliningAndOptimizationsAndStacktraces(name, executeProg, executeTests, gui, compiler, model, executeProgOnlyAfterTransforms, runArgs);
207207

208-
if (testLua && executeProg && !withStdLib) {
208+
if (testLua) {
209209
// test lua translation
210210
runArgs = runArgs.with("-lua");
211211
compiler.setRunArgs(runArgs);

de.peeeq.wurstscript/src/test/resources/QuickTestsSuite.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<class name="tests.wurstscript.tests.InterfaceTests"/>
2424
<class name="tests.wurstscript.tests.InterfaceExtendedTests"/>
2525
<class name="tests.wurstscript.tests.LexerTests"/>
26+
<class name="tests.wurstscript.tests.LuaTranslationTests"/>
2627
<class name="tests.wurstscript.tests.ModuleTests"/>
2728
<class name="tests.wurstscript.tests.MpqTest"/>
2829
<class name="tests.wurstscript.tests.NewFeatureTests"/>

0 commit comments

Comments
 (0)