From 95497c4c60b30f9c132a41dcc3041334c4b9e5bd Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Thu, 6 Jan 2022 16:35:05 +0100 Subject: [PATCH 1/5] - add test case with escape sequences - fix assertEquals arguments --- .../spoon/test/textBlocks/TextBlockTest.java | 31 +++++++++++++++---- .../textBlock/TextBlockTestClass.java | 8 +++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/test/java/spoon/test/textBlocks/TextBlockTest.java b/src/test/java/spoon/test/textBlocks/TextBlockTest.java index 4d855d3a455..7673c8707ba 100644 --- a/src/test/java/spoon/test/textBlocks/TextBlockTest.java +++ b/src/test/java/spoon/test/textBlocks/TextBlockTest.java @@ -44,7 +44,7 @@ public void testTextBlock1(){ CtStatement stmt1 = m1.getBody().getStatement(0); assertTrue(stmt1.getValueByRole(CtRole.ASSIGNMENT) instanceof CtTextBlock); CtTextBlock l1 = (CtTextBlock) stmt1.getValueByRole(CtRole.ASSIGNMENT); - assertEquals(l1.getValue(), "\n \n

Hello, कसौटी 🥲

\n \n\n"); + assertEquals("\n \n

Hello, कसौटी 🥲

\n \n\n", l1.getValue()); } @Test @@ -57,9 +57,10 @@ public void testTextBlockqoute(){ CtStatement stmt2 = m2.getBody().getStatement(0); assertTrue(stmt2.getValueByRole(CtRole.ASSIGNMENT) instanceof CtTextBlock); CtTextBlock l2 = (CtTextBlock) stmt2.getValueByRole(CtRole.ASSIGNMENT); - assertEquals(l2.getValue(), "SELECT \"EMP_ID\", \"LAST_NAME\" FROM \"EMPLOYEE_TB\"\n" + assertEquals("SELECT \"EMP_ID\", \"LAST_NAME\" FROM \"EMPLOYEE_TB\"\n" + "WHERE \"CITY\" = 'INDIANAPOLIS'\n" - + "ORDER BY \"EMP_ID\", \"LAST_NAME\";\n"); + + "ORDER BY \"EMP_ID\", \"LAST_NAME\";\n", + l2.getValue()); } @Test @@ -74,12 +75,13 @@ public void testTextBlockQouteWithinQoute(){ CtInvocation inv = (CtInvocation) stmt5.getDirectChildren().get(1); assertTrue(inv.getArguments().get(0) instanceof CtTextBlock); CtTextBlock l3 = (CtTextBlock) inv.getArguments().get(0); - assertEquals(l3.getValue(), "function hello() {\n" + assertEquals("function hello() {\n" + " print('\"Hello, world\"');\n" + "}\n" + "\n" + "hello();\n" - + ""); + + "", + l3.getValue()); } @Test @@ -92,7 +94,7 @@ public void testTextBlockEmpty(){ CtStatement stmt1 = m4.getBody().getStatement(0); assertTrue(stmt1.getValueByRole(CtRole.ASSIGNMENT) instanceof CtTextBlock); CtTextBlock l1 = (CtTextBlock) stmt1.getValueByRole(CtRole.ASSIGNMENT); - assertEquals(l1.getValue(), ""); + assertEquals("", l1.getValue()); } @Test @@ -118,4 +120,21 @@ public void testTextBlockCreation(){ c.toString() ); } + + @Test + public void testTextBlockEscapes(){ + //contract: Test Text Block usage introduced in Java 15 + Launcher launcher = setUpTest(); + CtClass allstmt = (CtClass) launcher.getFactory().Type().get("textBlock.TextBlockTestClass"); + CtMethod m1 = allstmt.getMethod("m5"); + + CtStatement stmt1 = m1.getBody().getStatement(0); + assertTrue(stmt1.getValueByRole(CtRole.ASSIGNMENT) instanceof CtTextBlock); + CtTextBlock l1 = (CtTextBlock) stmt1.getValueByRole(CtRole.ASSIGNMENT); + assertEquals("no-break space: \\00a0\n" + + "newline: \\n\n" + + "tab: \\t ('\t')\n", + l1.getValue()); + } + } diff --git a/src/test/resources/textBlock/TextBlockTestClass.java b/src/test/resources/textBlock/TextBlockTestClass.java index 47bd8adf70f..661f269a997 100644 --- a/src/test/resources/textBlock/TextBlockTestClass.java +++ b/src/test/resources/textBlock/TextBlockTestClass.java @@ -40,4 +40,12 @@ void m4() { String empty = """ """; } + + void m5() { + String escape = """ + no-break space: \\00a0 + newline: \\n + tab: \\t ('\t') + """; + } } From 4f1c6ac334c030dc7c7651849e76ca5e52a18922 Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Thu, 6 Jan 2022 17:27:03 +0100 Subject: [PATCH 2/5] - change junit imports to junit.jupiter to match the @Test annotation - change testTextBlockEscapes() to expect the correct result and mark it as disabled --- .../spoon/test/textBlocks/TextBlockTest.java | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/test/java/spoon/test/textBlocks/TextBlockTest.java b/src/test/java/spoon/test/textBlocks/TextBlockTest.java index 7673c8707ba..0eb803a6d64 100644 --- a/src/test/java/spoon/test/textBlocks/TextBlockTest.java +++ b/src/test/java/spoon/test/textBlocks/TextBlockTest.java @@ -1,14 +1,14 @@ package spoon.test.textBlocks; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import spoon.Launcher; import spoon.reflect.code.CtBlock; import spoon.reflect.code.CtInvocation; -import spoon.reflect.code.CtLiteral; import spoon.reflect.code.CtReturn; import spoon.reflect.code.CtStatement; import spoon.reflect.code.CtTextBlock; @@ -121,19 +121,32 @@ public void testTextBlockCreation(){ ); } - @Test + @Test @Disabled public void testTextBlockEscapes(){ //contract: Test Text Block usage introduced in Java 15 Launcher launcher = setUpTest(); + launcher.getEnvironment().setAutoImports(true); + CtClass allstmt = (CtClass) launcher.getFactory().Type().get("textBlock.TextBlockTestClass"); - CtMethod m1 = allstmt.getMethod("m5"); + CtMethod m5 = allstmt.getMethod("m5"); - CtStatement stmt1 = m1.getBody().getStatement(0); - assertTrue(stmt1.getValueByRole(CtRole.ASSIGNMENT) instanceof CtTextBlock); - CtTextBlock l1 = (CtTextBlock) stmt1.getValueByRole(CtRole.ASSIGNMENT); - assertEquals("no-break space: \\00a0\n" + - "newline: \\n\n" + - "tab: \\t ('\t')\n", + String m5Text = m5.toString(); + assertEquals("void m5() {\n" + + " String escape = \"\"\"\n" + + " no-break space: \\\\00a0\n" + + " newline: \\\\n\n" + + " tab: \\\\t ('\\t')\n" + + " \"\"\";\n" + + "}", + m5Text); + + CtStatement stmt5 = m5.getBody().getStatement(0); + assertTrue(stmt5.getValueByRole(CtRole.ASSIGNMENT) instanceof CtTextBlock); + + CtTextBlock l1 = (CtTextBlock) stmt5.getValueByRole(CtRole.ASSIGNMENT); + assertEquals("no-break space: \\\\00a0\n" + + "newline: \\\\n\n" + + "tab: \\\\t ('\t')\n", l1.getValue()); } From 92bcf51a76cdfe96e72df7d8189bd2897c36788c Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Fri, 7 Jan 2022 09:58:43 +0100 Subject: [PATCH 3/5] fix LiteralHelper.getTextBlockToken() and enable unit test --- .../spoon/reflect/visitor/LiteralHelper.java | 3 +-- .../spoon/test/textBlocks/TextBlockTest.java | 27 ++++++++++--------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/spoon/reflect/visitor/LiteralHelper.java b/src/main/java/spoon/reflect/visitor/LiteralHelper.java index a0d1baf54f0..dd375ec1bee 100644 --- a/src/main/java/spoon/reflect/visitor/LiteralHelper.java +++ b/src/main/java/spoon/reflect/visitor/LiteralHelper.java @@ -58,12 +58,11 @@ private static String getBasedString(Double value, LiteralBase base) { /** * @param literal CtTextBlock to be converted - * @param numTabs * @return source code representation of the literal */ public static String getTextBlockToken(CtTextBlock literal) { String token = "\"\"\"\n" - + literal.getValue() + + literal.getValue().replaceAll("\\\\", "\\\\\\\\") + "\"\"\""; return token; } diff --git a/src/test/java/spoon/test/textBlocks/TextBlockTest.java b/src/test/java/spoon/test/textBlocks/TextBlockTest.java index 0eb803a6d64..043dc86a0a6 100644 --- a/src/test/java/spoon/test/textBlocks/TextBlockTest.java +++ b/src/test/java/spoon/test/textBlocks/TextBlockTest.java @@ -3,7 +3,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import spoon.Launcher; @@ -121,33 +120,35 @@ public void testTextBlockCreation(){ ); } - @Test @Disabled + @Test public void testTextBlockEscapes(){ - //contract: Test Text Block usage introduced in Java 15 + //contract: text-blocks should retain escape sequences in code Launcher launcher = setUpTest(); launcher.getEnvironment().setAutoImports(true); CtClass allstmt = (CtClass) launcher.getFactory().Type().get("textBlock.TextBlockTestClass"); CtMethod m5 = allstmt.getMethod("m5"); + CtStatement stmt5 = m5.getBody().getStatement(0); + assertTrue(stmt5.getValueByRole(CtRole.ASSIGNMENT) instanceof CtTextBlock); + + // test text block value + CtTextBlock l1 = (CtTextBlock) stmt5.getValueByRole(CtRole.ASSIGNMENT); + assertEquals("no-break space: \\00a0\n" + + "newline: \\n\n" + + "tab: \\t ('\t')\n", + l1.getValue()); + + // escape sequences should be retained in code String m5Text = m5.toString(); assertEquals("void m5() {\n" + " String escape = \"\"\"\n" + " no-break space: \\\\00a0\n" + " newline: \\\\n\n" + - " tab: \\\\t ('\\t')\n" + + " tab: \\\\t ('\t')\n" + " \"\"\";\n" + "}", m5Text); - - CtStatement stmt5 = m5.getBody().getStatement(0); - assertTrue(stmt5.getValueByRole(CtRole.ASSIGNMENT) instanceof CtTextBlock); - - CtTextBlock l1 = (CtTextBlock) stmt5.getValueByRole(CtRole.ASSIGNMENT); - assertEquals("no-break space: \\\\00a0\n" + - "newline: \\\\n\n" + - "tab: \\\\t ('\t')\n", - l1.getValue()); } } From af0ae64e35ddb64a401635be03e2c11e1ad3ca5e Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Fri, 7 Jan 2022 10:19:33 +0100 Subject: [PATCH 4/5] set line separator on windows --- src/test/java/spoon/test/textBlocks/TextBlockTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/spoon/test/textBlocks/TextBlockTest.java b/src/test/java/spoon/test/textBlocks/TextBlockTest.java index 043dc86a0a6..1f6d01553fc 100644 --- a/src/test/java/spoon/test/textBlocks/TextBlockTest.java +++ b/src/test/java/spoon/test/textBlocks/TextBlockTest.java @@ -121,6 +121,7 @@ public void testTextBlockCreation(){ } @Test + @ExtendWith(LineSeperatorExtension.class) public void testTextBlockEscapes(){ //contract: text-blocks should retain escape sequences in code Launcher launcher = setUpTest(); From e3c02df8efe7f86830f2b048f37f946ba39c9904 Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Mon, 10 Jan 2022 11:14:25 +0100 Subject: [PATCH 5/5] use String.replace(CharSequence, CharSequence) instead of String.replaceAll(String, String) --- src/main/java/spoon/reflect/visitor/LiteralHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/spoon/reflect/visitor/LiteralHelper.java b/src/main/java/spoon/reflect/visitor/LiteralHelper.java index dd375ec1bee..c353849c0ca 100644 --- a/src/main/java/spoon/reflect/visitor/LiteralHelper.java +++ b/src/main/java/spoon/reflect/visitor/LiteralHelper.java @@ -62,7 +62,7 @@ private static String getBasedString(Double value, LiteralBase base) { */ public static String getTextBlockToken(CtTextBlock literal) { String token = "\"\"\"\n" - + literal.getValue().replaceAll("\\\\", "\\\\\\\\") + + literal.getValue().replace("\\", "\\\\") + "\"\"\""; return token; }