Skip to content

Commit ddfdfe9

Browse files
authored
test: add tests about #5001 (#6285)
1 parent 463587b commit ddfdfe9

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

src/test/java/spoon/reflect/visitor/DefaultJavaPrettyPrinterTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,38 @@ private static Launcher createLauncherWithOptimizeParenthesesPrinter() {
130130
return launcher;
131131
}
132132

133+
@Nested
134+
class LongStringAssignmentInUnchangedFile {
135+
// This is the string LITERAL that we hope is the pretty-printed value for the "sql" variable.
136+
// This was obtained from the output produced by the pretty printer at the time, but notice importantly
137+
// that the parenthesis presented in the original issue (https://github.com/INRIA/spoon/issues/5001) around "from..."
138+
// and before "binaryIpStart" are not present, which is essentially what the original issue seems to be about.
139+
private String expectedStringLiteral = "\"Select distinct t.NETWORK_IP, t.NETWORK_IP1, t.NETWORK_IP2, " +
140+
"t.NETWORK_IP3, t.NETWORK_IP4 \" + \"from (SELECT DISTINCT t1.ipv4digit1 || '.' || t1.ipv4digit2 " +
141+
"|| '.' || t1.ipv4digit3 \" + \" || '.0' network_ip, \" + \" TO_NUMBER (t1.ipv4digit1) network_ip1, \" + \" TO_NUMBER " +
142+
"(t1.ipv4digit2) network_ip2, \" + \" TO_NUMBER (t1.ipv4digit3) network_ip3, \" + \" TO_NUMBER ('0') " +
143+
"network_ip4, t1.t2_team_id, \" + \" t1.system_owner_id, t1.system_owner_team_id \" + \" FROM ip_info t1 \" + \" where " +
144+
"t1.binary_ip >= '\" + binaryIpStart + \"' \" + \" and t1.binary_ip <= '\" + binaryIpEnd + \"' \" + \" " +
145+
"ORDER BY network_ip1, network_ip2, network_ip3 \" + \" ) t order by t.NETWORK_IP1,t.NETWORK_IP2,t.NETWORK_IP3,t.NETWORK_IP4 \"";
146+
147+
@Test
148+
@GitHubIssue(issueNumber = 5001, fixed = true)
149+
void testSameOutputWithOptimizedParenthesis() {
150+
Launcher launcher = createLauncherWithOptimizeParenthesesPrinter();
151+
launcher.addInputResource("src/test/java/spoon/test/prettyprinter/testclasses/SampleClassIssue5001.java");
152+
launcher.buildModel();
153+
154+
// Since there is only one class, there is only one entity returned by "getAll"
155+
CtCompilationUnit cu = launcher.getFactory().Type().getAll().get(0)
156+
.getPosition().getCompilationUnit();
157+
158+
PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
159+
String output = prettyPrinter.prettyprint(cu);
160+
161+
assertThat(output, containsString(expectedStringLiteral));
162+
}
163+
}
164+
133165

134166
@Test
135167
void testAutoImportPrinterDoesNotImportFunctionalInterfaceTargetedInLambda() {

src/test/java/spoon/test/eval/EvalTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919

2020
import java.io.File;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
import java.nio.file.Paths;
2124
import java.util.Arrays;
2225
import java.util.Collection;
2326
import java.util.List;
@@ -55,13 +58,15 @@
5558
import spoon.reflect.factory.Factory;
5659
import spoon.reflect.reference.CtTypeReference;
5760
import spoon.reflect.visitor.AccessibleVariablesFinder;
61+
import spoon.reflect.visitor.CtScanner;
5862
import spoon.reflect.visitor.OperatorHelper;
5963
import spoon.reflect.visitor.filter.TypeFilter;
6064
import spoon.support.compiler.VirtualFile;
6165
import spoon.support.reflect.eval.EvalHelper;
6266
import spoon.support.reflect.eval.InlinePartialEvaluator;
6367
import spoon.support.reflect.eval.VisitorPartialEvaluator;
6468
import spoon.test.eval.testclasses.Foo;
69+
import spoon.testing.utils.GitHubIssue;
6570

6671
import static org.junit.jupiter.api.Assertions.*;
6772
import static spoon.testing.utils.ModelUtils.build;
@@ -609,4 +614,28 @@ void testVisitCtBinaryOperatorFloatingDivision() {
609614
ctLiteral
610615
);
611616
}
617+
618+
@Test
619+
@GitHubIssue(issueNumber = 5001, fixed = true)
620+
public void testVisitCtLiteralWithLongStringValue() throws Exception {
621+
CtClass<?> ctClass = Launcher.parseClass(Files.readString(Paths.get("src/test/java/spoon/test/prettyprinter/testclasses/SampleClassIssue5001.java")));
622+
CtExpression<?> sql = ctClass.getField("sql").getAssignment();
623+
StringBuilder result = new StringBuilder();
624+
sql.accept(new CtScanner() {
625+
@Override
626+
public <T> void visitCtLiteral(CtLiteral<T> literal) {
627+
result.append(literal.getValue());
628+
}
629+
});
630+
631+
String expectedValue = "Select distinct t.NETWORK_IP, t.NETWORK_IP1, t.NETWORK_IP2, " +
632+
"t.NETWORK_IP3, t.NETWORK_IP4 from (SELECT DISTINCT t1.ipv4digit1 || '.' || t1.ipv4digit2 || '.' || " +
633+
"t1.ipv4digit3 || '.0' network_ip, TO_NUMBER (t1.ipv4digit1) network_ip1, TO_NUMBER (t1.ipv4digit2) " +
634+
"network_ip2, TO_NUMBER (t1.ipv4digit3) network_ip3, TO_NUMBER ('0') network_ip4, t1.t2_team_id, " +
635+
"t1.system_owner_id, t1.system_owner_team_id FROM ip_info t1 where t1.binary_ip >= '' and t1.binary_ip " +
636+
"<= '' ORDER BY network_ip1, network_ip2, network_ip3 ) t order by t.NETWORK_IP1,t.NETWORK_IP2,t.NETWORK_IP3,t.NETWORK_IP4";
637+
638+
String actualValue = result.toString().strip().replaceAll("\\s{2,}", " ");
639+
assertEquals(expectedValue, actualValue);
640+
}
612641
}

src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,15 @@ public void testCorrectTypeCastParenthesisAfterRefactor() {
11751175
refactor.refactor();
11761176
}, (type, result) -> assertThat(result, containsString("((Double) b).toString();")));
11771177
}
1178+
1179+
@Test
1180+
@GitHubIssue(issueNumber = 5001, fixed = true)
1181+
public void testCorrectPrintingOfUnchangedStringAssignment() throws IOException {
1182+
// We want to make sure that if there are no changes made to the source code, then the output is the same
1183+
// as the input.
1184+
testNoChangeDiffFailing(
1185+
Paths.get("src/test/java/spoon/test/prettyprinter/testclasses/SampleClassIssue5001").toFile());
1186+
}
11781187
/**
11791188
* Test various syntax by doing an change to every element that should not
11801189
* result in any change in source. This forces the sniper printer to recreate
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package spoon.test.prettyprinter.testclasses;
2+
3+
public class SampleClassIssue5001 {
4+
String binaryIpStart = "start";
5+
String binaryIpEnd = "end";
6+
String sql = "Select distinct t.NETWORK_IP, t.NETWORK_IP1, t.NETWORK_IP2, t.NETWORK_IP3, t.NETWORK_IP4 " +
7+
"from (SELECT DISTINCT t1.ipv4digit1 || '.' || t1.ipv4digit2 || '.' || t1.ipv4digit3 " +
8+
" || '.0' network_ip, " +
9+
" TO_NUMBER (t1.ipv4digit1) network_ip1, " +
10+
" TO_NUMBER (t1.ipv4digit2) network_ip2, " +
11+
" TO_NUMBER (t1.ipv4digit3) network_ip3, " +
12+
" TO_NUMBER ('0') network_ip4, t1.t2_team_id, " +
13+
" t1.system_owner_id, t1.system_owner_team_id " +
14+
" FROM ip_info t1 " +
15+
" where t1.binary_ip >= '" + binaryIpStart + "' " +
16+
" and t1.binary_ip <= '" + binaryIpEnd + "' " +
17+
" ORDER BY network_ip1, network_ip2, network_ip3 " +
18+
" ) t order by t.NETWORK_IP1,t.NETWORK_IP2,t.NETWORK_IP3,t.NETWORK_IP4 ";
19+
}

0 commit comments

Comments
 (0)