@@ -43,7 +43,9 @@ private void assertFunctionCall(String output, String functionName, String argum
4343 private void assertFunctionBodyContains (String output , String functionName , String search , boolean mustContain ) {
4444 Pattern pattern = Pattern .compile ("function\\ s*" + functionName + "\\ s*\\ (.*\\ ).*\\ n" + "((?:\\ n|.)*?)end" );
4545 Matcher matcher = pattern .matcher (output );
46+ boolean found = false ;
4647 while (matcher .find ()) {
48+ found = true ;
4749 String body = matcher .group (1 );
4850 if (!body .contains (search ) && mustContain ) {
4951 fail ("Function " + functionName + " must contain " + search + "." );
@@ -52,6 +54,7 @@ private void assertFunctionBodyContains(String output, String functionName, Stri
5254 fail ("Function " + functionName + " must not contain " + search + "." );
5355 }
5456 }
57+ assertTrue ("Function " + functionName + " was not found." , found );
5558 }
5659
5760 @ Test
@@ -191,6 +194,46 @@ public void stringConcatenation() throws IOException {
191194 assertFunctionBodyContains (compiled , "test" , "stringConcat" , true );
192195 }
193196
197+ @ Test
198+ public void methodFieldNameCollision () throws IOException {
199+ test ().testLua (true ).lines (
200+ "package Test" ,
201+ "class Foo" ,
202+ " int size = 3" ,
203+ " function size() returns int" ,
204+ " return size" ,
205+ "init" ,
206+ " let f = new Foo()" ,
207+ " f.size()"
208+ );
209+ String compiled = Files .toString (new File ("test-output/lua/LuaTranslationTests_methodFieldNameCollision.lua" ), Charsets .UTF_8 );
210+ assertFunctionBodyContains (compiled , "Foo_Foo_size" , "Foo_size_field" , true );
211+ assertFunctionBodyContains (compiled , "Foo_Foo_size" , "return this.Foo_size\n " , false );
212+ }
213+
214+ @ Test
215+ public void mainAndConfigNamesFixed () throws IOException {
216+ test ().testLua (true ).lines (
217+ "package Test" ,
218+ "native takesInt(int i)" ,
219+ "function helper()" ,
220+ " let main = 1" ,
221+ " let config = 2" ,
222+ " takesInt(main)" ,
223+ " takesInt(config)" ,
224+ "init" ,
225+ " helper()"
226+ );
227+ String compiled = Files .toString (new File ("test-output/lua/LuaTranslationTests_mainAndConfigNamesFixed.lua" ), Charsets .UTF_8 );
228+ assertFunctionBodyContains (compiled , "helper" , "local main1" , true );
229+ assertFunctionBodyContains (compiled , "helper" , "local config1" , true );
230+ assertTrue (compiled .contains ("function main(" ));
231+ assertTrue (compiled .contains ("function config(" ));
232+ assertFalse (compiled .contains ("function main2(" ));
233+ assertFalse (compiled .contains ("function config2(" ));
234+ }
235+
236+
194237 @ Test
195238 public void intCasting () throws IOException {
196239 // Use local variables to test if it works even when local types are eliminated.
@@ -234,4 +277,3 @@ public void intCasting() throws IOException {
234277 assertFunctionBodyContains (compiled , "testClass" , "cObj2 = cInt" , false );
235278 }
236279}
237-
0 commit comments