def test_JavaRewrite_delete_comment(): """Comment(s) are deleted.""" assert java.JavaRewrite('/* This is a comment */') == '\n' assert java.JavaRewrite('//Line comment') == '\n' assert java.JavaRewrite(""" /** * Docstring format comment. */ // And a line comment. /* And a C syntax style comment. */ """) == '\n'
def test_JavaRewrite_github_testcase_1(): """Regression test found from scraping GitHub. This is an interesting file because it flexes the rewriter's ability to insert blocks around one-liner `for` and `if` constructs. Original source: github.com/0001077192/discord-spicybot src/main/java/com/nsa/spicybot/commands/SpicyPointsCommand.java """ assert (java.JavaRewrite(""" public class A { public static String format(int num){ String original="" + num; String dummy=original.length() % 3 != 0 ? original.substring(0,original.length() % 3) + "," : ""; for (int i=original.length() % 3; i < original.length(); i+=3) dummy+=original.substring(i,i + 3) + ","; if (dummy.endsWith(",")) dummy=dummy.substring(0,dummy.length() - 1); return dummy; } } """) == """\ public class A { \tpublic static String fn_A(int a) { \t\tString b = "" + a; \t\tString c = b.length() % 3 != 0 ? b.substring(0,b.length() % 3) + "," : ""; \t\tfor (int d = b.length() % 3; d < b.length(); d += 3) { \t\t\tc += b.substring(d, d + 3) + ","; \t\t} \t\tif (c.endsWith(",")) { \t\t\tc = c.substring(0, c.length() - 1); \t\t} \t\treturn c; \t} } """)
def test_JavaRewrite_formats_source(): """Test that source is formatted.""" assert java.JavaRewrite(""" public class A { public static void Foo(int a) { return a;} } """) == """\
def test_JavaRewrite_optional_if_else_braces_on_one_line(): """Test that if/else braces are inserted when declaration is on one line.""" assert java.JavaRewrite(""" public class A { \tpublic static int fn_A(int x) { \t\tif (x) return 1; else return 0; \t} } """) == """\
def test_JavaRewrite_comments_are_unchanged(): """Comment(s) are not deleted.""" assert java.JavaRewrite(""" /** * Docstring format comment. */ // And a line comment. /* And a C syntax style comment. */ """) == """\
def test_JavaRewrite_rewrite_class_name(): """Test that class is renamed.""" assert (java.JavaRewrite(""" public class MyJavaClass { } """) == """\ public class A { } """)
def test_JavaRewrite_rewrite_static_method_argument_names(): """Test that arguments are renamed.""" assert java.JavaRewrite(""" public class A { public static int myMethod(final int foo, int bar) { System.out.println("Hello world! " + bar); return foo + bar; } } """) == """\
def test_JavaRewrite_rewrite_anonymous_class_names(): """Test that anonymous classes are renamed.""" assert java.JavaRewrite(""" public class MyJavaClass { private class AnonymousClassA { } private class AnotherPrivateClass { } } """) == """\
def test_JavaRewrite_optional_if_else_braces(): """Test that if/else braces are inserted.""" assert java.JavaRewrite(""" public class A { \tpublic static int fn_A(int x) { \t\tif (x) \t\t\treturn 1; \t\telse \t\t\treturn 0; \t} } """) == """\
def test_JavaRewrite_conflicting_length_name(): """Test that rewriter gracefully handles 'length' used as variable.""" assert java.JavaRewrite(""" public class A { public static double[] createEntry(final double[] position){ int length=position.length; int sqrt=(int)Math.sqrt(9); return 0.0; } } """) == """\
def test_JavaRewrite_assertion_arg_rename_FAILS(): """This test highlights a failure of the JavaRewriter. Instead of correctly renaming 'x' -> 'a' in the assertion, the assertion is treated as the declaration of a new variable (of type 'assert'), and named 'b'. """ assert java.JavaRewrite(""" public class A { \tpublic static void fn_A(int x) { \t\tassert x; \t} } """) == """\
def test_JavaRewrite_whitespace(): """Multiple blank lines and whitespace is stripped.""" assert java.JavaRewrite('\n\n \n\t\n') == '\n'
def test_JavaRewrite_hello_world(): """Java rewriter returns unmodified input for bad code.""" assert java.JavaRewrite("hello, world") == "hello, world\n"