|
| 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 | + |
0 commit comments