diff --git a/src/main/java/spoon/reflect/visitor/LiteralHelper.java b/src/main/java/spoon/reflect/visitor/LiteralHelper.java index a0d1baf54f0..c353849c0ca 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().replace("\\", "\\\\") + "\"\"\""; return token; } diff --git a/src/test/java/spoon/test/textBlocks/TextBlockTest.java b/src/test/java/spoon/test/textBlocks/TextBlockTest.java index 4d855d3a455..1f6d01553fc 100644 --- a/src/test/java/spoon/test/textBlocks/TextBlockTest.java +++ b/src/test/java/spoon/test/textBlocks/TextBlockTest.java @@ -1,14 +1,13 @@ 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.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; @@ -44,7 +43,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
\nHello, कसौटी 🥲
\n \n\n"); + assertEquals("\n \nHello, कसौटी 🥲
\n \n\n", l1.getValue()); } @Test @@ -57,9 +56,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 +74,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 +93,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 +119,37 @@ public void testTextBlockCreation(){ c.toString() ); } + + @Test + @ExtendWith(LineSeperatorExtension.class) + public void testTextBlockEscapes(){ + //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" + + " \"\"\";\n" + + "}", + m5Text); + } + } 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') + """; + } }