1 package net.sourceforge.jenesis4java.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import net.sourceforge.jenesis4java.*;
26 import net.sourceforge.jenesis4java.util.InstanceReplacingVisitor;
27 import net.sourceforge.jenesis4java.util.StatementTestBase;
28 import net.sourceforge.jenesis4java.util.WithVmAndMethod;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.experimental.runners.Enclosed;
32 import org.junit.runner.RunWith;
33
34 import static java.util.Collections.emptyList;
35 import static java.util.Collections.singletonList;
36 import static net.sourceforge.jenesis4java.impl.CommentsTest.MultiLineComment.DEFAULT_MULTILINE_COMMENT;
37 import static net.sourceforge.jenesis4java.util.Shortcuts.dummyClass;
38 import static net.sourceforge.jenesis4java.util.TestHelper.*;
39 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertSame;
41
42 @RunWith(Enclosed.class)
43 public class IfTest {
44
45 public static class IfStatement extends WithVmAndMethod {
46
47 @Test
48 public void simpleIf() {
49 Expression trueExpr = vm.newTrue();
50 If anIf = method.newIf(trueExpr);
51 anIf.getElse();
52 assertEquals("if (true) {" + LS + "}", anIf.toString());
53 }
54
55 @Test
56 public void simpleElseIf() {
57 Expression falseExpr = vm.newFalse();
58 If anIf = method.newIf(falseExpr);
59 Expression trueExpr = vm.newTrue();
60 anIf.newElseIf(trueExpr);
61 assertEquals("if (false) {" + LS + "} else if (true) {" + LS + "}", anIf.toString());
62 }
63
64 @Test
65 public void simpleElseIfElse() {
66 Expression falseExpr = vm.newFalse();
67 If anIf = method.newIf(falseExpr);
68 Expression trueExpr = vm.newTrue();
69 anIf.newElseIf(trueExpr);
70 Statement statementInElse = anIf.getElse().newEmpty();
71
72 String expected = "if (false) {" + LS
73 + "} else if (true) {" + LS
74 + "} else {" + LS
75 + INDENT + statementInElse + LS
76 + "}";
77 assertEquals(expected, anIf.toString());
78 }
79 }
80
81 public static class GettersAndSetters extends WithVmAndMethod {
82
83 @Test
84 public void getElseIf() {
85 If anIf = method.newIf(vm.newFree("irrelevant"));
86 assertEquals(emptyList(), anIf.getElseIfs());
87
88 ElseIf elseIf = anIf.newElseIf(vm.newTrue());
89 assertEquals(singletonList(elseIf), anIf.getElseIfs());
90 }
91
92 @Test
93 public void predicate() {
94 If anIf = method.newIf(vm.newFree("initial_prediacate"));
95 Freeform replacingPredicate = vm.newFree("new_predicate");
96 ConditionalStatement returnedIf = anIf.setPredicate(replacingPredicate);
97 assertSame(anIf, returnedIf);
98 assertEquals(replacingPredicate, anIf.getPredicate());
99 }
100 }
101
102 public static class Visitor extends WithVmAndMethod {
103
104 @Test
105 public void visitIf() {
106 Expression originalExpression = vm.newTrue();
107 If anIf = method.newIf(originalExpression);
108 Expression replacementExpression = vm.newFalse();
109 anIf.visit(new InstanceReplacingVisitor(originalExpression, replacementExpression));
110 assertEquals(replacementExpression, anIf.getPredicate());
111 }
112
113 @Test
114 public void visitElseIf() {
115 If anIf = method.newIf(vm.newFree("irrelevant"));
116 Expression originalExpression = vm.newTrue();
117 ElseIf elseIf = anIf.newElseIf(originalExpression);
118 Expression replacementExpression = vm.newFalse();
119
120 elseIf.visit(new InstanceReplacingVisitor(originalExpression, replacementExpression));
121 assertEquals(replacementExpression, elseIf.getPredicate());
122 }
123
124 @Test
125 public void removeElseIfFromIf() {
126 If anIf = method.newIf(vm.newFree("irrelevant"));
127 ElseIf elseIf1 = anIf.newElseIf(vm.newTrue());
128 ElseIf elseIf2 = anIf.newElseIf(vm.newTrue());
129
130 anIf.visit(new InstanceReplacingVisitor(elseIf1, null));
131 assertEquals(singletonList(elseIf2), anIf.getElseIfs());
132 }
133 }
134
135 public static class IfInheritedFunctionality extends StatementTestBase {
136
137 @Override
138 public Statement getInstance() {
139 return dummyClass().newMethod("method").newIf(vm().newTrue());
140 }
141 }
142
143 public static class ElseIfInheritedFunctionality extends StatementTestBase {
144
145 @Override
146 public Statement getInstance() {
147 return dummyClass().newMethod("method").newIf(vm().newTrue()).newElseIf(vm().newFalse());
148 }
149 }
150
151 public static class ElseInheritedFunctionality extends StatementTestBase {
152
153 @Override
154 public Statement getInstance() {
155 If anIf = dummyClass().newMethod("method").newIf(vm().newFalse());
156 anIf.getElse().newEmpty();
157 return anIf.getElse();
158 }
159
160 }
161
162
163
164
165
166 public static class Comments {
167
168 private If ifStmt;
169
170 @Before
171 public void setUp() {
172 this.ifStmt = dummyClass().newMethod("method").newIf(vm().newFalse());
173 }
174
175 @Test
176 public void singleLineOnElseIfInbetweenBlocks() {
177 String ifOnly = ifStmt.toString();
178
179 ElseIf elseIf = ifStmt.newElseIf(vm().newTrue());
180 String elseIfOnly = elseIf.toString();
181 elseIf.setComment(Comment.SINGLE_LINE, "comment");
182
183 String expectedIfCommentElseIf = ifOnly + LS + "// comment" + LS + elseIfOnly;
184 assertEquals(expectedIfCommentElseIf, ifStmt.toString());
185 }
186
187 @Test
188 public void multiLineOnElseIfInbetweenBlocks() {
189 String ifOnly = ifStmt.toString();
190
191 ElseIf elseIf = ifStmt.newElseIf(vm().newTrue());
192 String elseIfOnly = elseIf.toString();
193 elseIf.setComment(Comment.MULTI_LINE, "comment");
194
195 String expectedIfCommentElseIf = ifOnly + LS + DEFAULT_MULTILINE_COMMENT + elseIfOnly;
196 assertEquals(expectedIfCommentElseIf, ifStmt.toString());
197 }
198
199 @Test
200 public void singleLineCommentInbetweenBlocks() {
201 String ifOnly = ifStmt.toString();
202 Else elsePart = ifStmt.getElse();
203 elsePart.newEmpty();
204 String elseIfOnly = elsePart.toString();
205 elsePart.setComment(Comment.SINGLE_LINE, "comment");
206
207 String expectedIfCommentElseIf = ifOnly + LS + "// comment" + LS + elseIfOnly;
208 assertEquals(expectedIfCommentElseIf, ifStmt.toString());
209 }
210
211 @Test
212 public void multiLineCommentInbetweenBlocks() {
213 String ifOnly = ifStmt.toString();
214
215 Else elsePart = ifStmt.getElse();
216 elsePart.newEmpty();
217 String elseIfOnly = elsePart.toString();
218 elsePart.setComment(Comment.MULTI_LINE, "comment");
219
220 String expectedIfCommentElseIf = ifOnly + LS + DEFAULT_MULTILINE_COMMENT + elseIfOnly;
221 assertEquals(expectedIfCommentElseIf, ifStmt.toString());
222 }
223
224 }
225 }