Skip to content

Commit d7f28a1

Browse files
committed
add more test cases and fix the processing of disabled annotation
1 parent 0efdaa6 commit d7f28a1

File tree

2 files changed

+137
-4
lines changed

2 files changed

+137
-4
lines changed

src/main/java/com/antkorwin/betterstrings/ast/InnerStringVarsAstTranslator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public <T extends JCTree> T translate(T t) {
4242
@Override
4343
public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
4444

45-
doWithSkipResolving(() -> isAnnotatedBySkip(jcClassDecl.getModifiers()),
45+
doWithSkipResolving(() -> skip || isAnnotatedBySkip(jcClassDecl.getModifiers()),
4646
() -> super.visitClassDef(jcClassDecl));
4747
}
4848

@@ -56,14 +56,14 @@ public void visitAnnotation(JCTree.JCAnnotation jcAnnotation) {
5656
@Override
5757
public void visitMethodDef(JCTree.JCMethodDecl jcMethodDecl) {
5858

59-
doWithSkipResolving(() -> isAnnotatedBySkip(jcMethodDecl.getModifiers()),
59+
doWithSkipResolving(() -> skip || isAnnotatedBySkip(jcMethodDecl.getModifiers()),
6060
() -> super.visitMethodDef(jcMethodDecl));
6161
}
6262

6363
@Override
6464
public void visitVarDef(JCTree.JCVariableDecl jcVariableDecl) {
6565

66-
doWithSkipResolving(() -> isAnnotatedBySkip(jcVariableDecl.getModifiers()),
66+
doWithSkipResolving(() -> skip || isAnnotatedBySkip(jcVariableDecl.getModifiers()),
6767
() -> super.visitVarDef(jcVariableDecl));
6868
}
6969

src/test/java/com/antkorwin/betterstrings/BetterStringsProcessorTest.java

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.jupitertools.compiletest.CompileTest;
44
import org.intellij.lang.annotations.Language;
5+
import org.junit.jupiter.api.Nested;
56
import org.junit.jupiter.api.Test;
67

78
import static org.assertj.core.api.Assertions.assertThat;
@@ -10,7 +11,7 @@
1011
class BetterStringsProcessorTest {
1112

1213
@Test
13-
void compileTest() {
14+
void simple() {
1415

1516
@Language("Java") String classCode = "public class Test { " +
1617
" public static String hello(){ " +
@@ -27,4 +28,136 @@ void compileTest() {
2728

2829
assertThat(result).isEqualTo("Hey-Ho!");
2930
}
31+
32+
@Test
33+
void evaluateExpression() {
34+
35+
@Language("Java") String classCode = "public class Test { " +
36+
" public static String sum(){ " +
37+
" int x = 3;" +
38+
" int y = 4;" +
39+
" return \"${x} + ${y} = ${x+y}\";" +
40+
" }" +
41+
"}";
42+
43+
Object result = new CompileTest().classCode("Test", classCode)
44+
.processor(new BetterStringsProcessor())
45+
.compile()
46+
.loadClass("Test")
47+
.invokeStatic("sum");
48+
49+
assertThat(result).isEqualTo("3 + 4 = 7");
50+
}
51+
52+
@Test
53+
void createNewClassInExpression() {
54+
@Language("Java") String innerClass = "public class Inner {" +
55+
" public String password(){" +
56+
" return \"1234\";" +
57+
" }" +
58+
"}";
59+
@Language("Java") String classCode = "public class Test { " +
60+
" public static String test(){ " +
61+
" int x = 3;" +
62+
" int y = 4;" +
63+
" return \"password = ${new Inner().password()}\";" +
64+
" }" +
65+
"}";
66+
67+
Object result = new CompileTest().classCode("Test", classCode)
68+
.classCode("Inner", innerClass)
69+
.processor(new BetterStringsProcessor())
70+
.compile()
71+
.loadClass("Test")
72+
.invokeStatic("test");
73+
74+
assertThat(result).isEqualTo("password = 1234");
75+
}
76+
77+
78+
@Nested
79+
class DisabledAnnotationTests {
80+
81+
@Test
82+
void onField() {
83+
@Language("Java") String classCode = "public class Test { " +
84+
" @com.antkorwin.betterstrings.DisabledStringInterpolation" +
85+
" public String field = \"${3+4}\";" +
86+
" public String getField(){ " +
87+
" return \"${field}\";" +
88+
" }" +
89+
"}";
90+
91+
Object result = new CompileTest().classCode("Test", classCode)
92+
.processor(new BetterStringsProcessor())
93+
.compile()
94+
.createClass("Test")
95+
.invoke("getField");
96+
97+
assertThat(result).isEqualTo("${3+4}");
98+
}
99+
100+
@Test
101+
void onMethod() {
102+
@Language("Java") String classCode = "public class Test { " +
103+
" @com.antkorwin.betterstrings.DisabledStringInterpolation" +
104+
" public static String hello(){ " +
105+
" String x = \"Ho!\"; " +
106+
" return \"Hey-${x}\";" +
107+
" }" +
108+
"}";
109+
110+
Object result = new CompileTest().classCode("Test", classCode)
111+
.processor(new BetterStringsProcessor())
112+
.compile()
113+
.loadClass("Test")
114+
.invokeStatic("hello");
115+
116+
assertThat(result).isEqualTo("Hey-${x}");
117+
}
118+
119+
@Test
120+
void onClass() {
121+
@Language("Java") String classCode = "@com.antkorwin.betterstrings.DisabledStringInterpolation " +
122+
"public class Test { " +
123+
" public static String hello(){ " +
124+
" String x = \"Ho!\"; " +
125+
" return \"Hey-${x}\";" +
126+
" }" +
127+
"}";
128+
129+
Object result = new CompileTest().classCode("Test", classCode)
130+
.processor(new BetterStringsProcessor())
131+
.compile()
132+
.loadClass("Test")
133+
.invokeStatic("hello");
134+
135+
assertThat(result).isEqualTo("Hey-${x}");
136+
}
137+
138+
@Test
139+
void onNestedClass() {
140+
@Language("Java") String classCode = "public class Test { " +
141+
" public static String sum(){ " +
142+
" return \"sum = ${new NestedClass().test()}\";" +
143+
" } " +
144+
" @com.antkorwin.betterstrings.DisabledStringInterpolation " +
145+
" public static class NestedClass {" +
146+
" public String test(){" +
147+
" return \"${3+4}\";" +
148+
" }" +
149+
" }" +
150+
"}";
151+
152+
Object result = new CompileTest().classCode("Test", classCode)
153+
.processor(new BetterStringsProcessor())
154+
.compile()
155+
.loadClass("Test")
156+
.invokeStatic("sum");
157+
158+
assertThat(result).isEqualTo("sum = ${3+4}");
159+
}
160+
}
161+
162+
30163
}

0 commit comments

Comments
 (0)