Skip to content

fix lua null string #995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ public static LuaExpr translate(ImMethodCall e, LuaTranslator tr) {
}

public static LuaExpr translate(ImNull e, LuaTranslator tr) {
return LuaAst.LuaExprNull();
if(isStringType(e.getType())) {
return LuaAst.LuaExprStringVal("");
} else {
return LuaAst.LuaExprNull();
}
}

public static LuaExpr translate(ImOperatorCall e, LuaTranslator tr) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package tests.wurstscript.tests;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.testng.annotations.Ignore;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;

import static org.testng.AssertJUnit.*;


public class LuaTranslationTests extends WurstScriptTest {


@Test
public void testStdLib() throws IOException {
test().testLua(true).withStdLib().lines(
"package Test",
"import MagicFunctions",
"init",
" print(compiletime)"
);
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_testStdLib.lua"), Charsets.UTF_8);
assertTrue(compiled.contains("MagicFunctions_compiletime"));
}

@Ignore
@Test
public void testExecution() {
test().testLua(true).executeProg().lines(
"package Test",
"native testSuccess()",
"init",
" testSuccess()"
);
}

@Test
public void nullString1() throws IOException {
test().testLua(true).lines(
"package Test",
"function nullString() returns string",
" return null",
"init",
" nullString()"
);
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_nullString1.lua"), Charsets.UTF_8);
assertTrue(compiled.contains("return \"\"") && !compiled.contains("return nil"));
}

@Test
public void nullString2() throws IOException {
test().testLua(true).lines(
"package Test",
"function takesString(string s)",
"init",
" takesString(null)"
);
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_nullString2.lua"), Charsets.UTF_8);
assertTrue(compiled.contains("takesString(\"\")") && !compiled.contains("takesString(nil)"));
}

@Ignore
@Test
public void nullString3() {
test().testLua(true).executeProg().lines(
"package Test",
"native testSuccess()",
"function nullString() returns string",
" return null",
"function returnsString(string s) returns string",
" return s + nullString() + s",
"init",
" let s = \".\"",
" let r = returnsString(s)",
" if r == s + s and r == \"..\"",
" testSuccess()"
);
}

@Test
public void nullObject1() throws IOException {
test().testLua(true).lines(
"package Test",
"class A",
"function nullObject() returns A",
" return null",
"init",
" nullObject()"
);
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_nullObject1.lua"), Charsets.UTF_8);
assertTrue(compiled.contains("return nil") && !compiled.contains("return \"\""));
}

@Test
public void nullObject2() throws IOException {
test().testLua(true).lines(
"package Test",
"class A",
"function takesObject(A a)",
"init",
" takesObject(null)"
);
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_nullObject2.lua"), Charsets.UTF_8);
assertTrue(compiled.contains("takesObject(nil)") && !compiled.contains("takesObject(\"\")"));
}

@Test
public void nullUnit1() throws IOException {
test().testLua(true).withStdLib().lines(
"package Test",
"function nullUnit() returns unit",
" return null",
"init",
" nullUnit()"
);
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_nullUnit1.lua"), Charsets.UTF_8);
assertTrue(compiled.contains("return nil") && !compiled.contains("return \"\""));
}

@Test
public void nullUnit2() throws IOException {
test().testLua(true).withStdLib().lines(
"package Test",
"function takesUnit(unit u)",
"init",
" takesUnit(null)"
);
String compiled = Files.toString(new File("test-output/lua/LuaTranslationTests_nullUnit2.lua"), Charsets.UTF_8);
assertTrue(compiled.contains("takesUnit(nil)") && !compiled.contains("takesUnit(\"\")"));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private CompilationResult testScript() {

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

if (testLua && executeProg && !withStdLib) {
if (testLua) {
// test lua translation
runArgs = runArgs.with("-lua");
compiler.setRunArgs(runArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<class name="tests.wurstscript.tests.InterfaceTests"/>
<class name="tests.wurstscript.tests.InterfaceExtendedTests"/>
<class name="tests.wurstscript.tests.LexerTests"/>
<class name="tests.wurstscript.tests.LuaTranslationTests"/>
<class name="tests.wurstscript.tests.ModuleTests"/>
<class name="tests.wurstscript.tests.MpqTest"/>
<class name="tests.wurstscript.tests.NewFeatureTests"/>
Expand Down