|
13 | 13 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
14 | 14 | import static org.junit.jupiter.api.Assertions.assertNull;
|
15 | 15 | import static org.junit.jupiter.api.Assertions.assertSame;
|
| 16 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
16 | 17 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
17 | 18 | import static spoon.testing.utils.ModelUtils.build;
|
18 | 19 | import static spoon.testing.utils.ModelUtils.createFactory;
|
|
31 | 32 | import spoon.reflect.code.CaseKind;
|
32 | 33 | import spoon.reflect.code.CtBreak;
|
33 | 34 | import spoon.reflect.code.CtCase;
|
| 35 | +import spoon.reflect.code.CtExpression; |
34 | 36 | import spoon.reflect.code.CtIf;
|
35 | 37 | import spoon.reflect.code.CtLiteral;
|
36 | 38 | import spoon.reflect.code.CtLocalVariable;
|
@@ -249,4 +251,118 @@ public void testJava14yield() {
|
249 | 251 | }
|
250 | 252 | }
|
251 | 253 |
|
| 254 | + @Nested |
| 255 | + class InsertCaseInSwitch { |
| 256 | + @DisplayName("cases should be inserted at the correct position") |
| 257 | + @Test |
| 258 | + public void test_addCaseAt_addsCaseAtSpecifiedPositionInSwitch() { |
| 259 | + // contract: case should be added at the specified position in `CtSwitch` |
| 260 | + Factory factory = new Launcher().getFactory(); |
| 261 | + |
| 262 | + CtSwitch<Integer> switchBlock = factory.createSwitch(); |
| 263 | + CtExpression<Integer> switchSelector = factory.createCodeSnippetExpression("x"); |
| 264 | + switchBlock.setSelector(switchSelector); |
| 265 | + |
| 266 | + CtBreak ctBreak = factory.createBreak(); |
| 267 | + |
| 268 | + CtCase<Integer> first = factory.createCase(); |
| 269 | + CtExpression<Integer> firstExpression = factory.createCodeSnippetExpression("1"); |
| 270 | + first.addCaseExpression(firstExpression); |
| 271 | + first.setCaseKind(CaseKind.COLON); |
| 272 | + first.addStatement(ctBreak); |
| 273 | + |
| 274 | + CtCase<Integer> second = factory.createCase(); |
| 275 | + CtExpression<Integer> secondExpression = factory.createCodeSnippetExpression("2"); |
| 276 | + second.addCaseExpression(secondExpression); |
| 277 | + second.setCaseKind(CaseKind.COLON); |
| 278 | + second.addStatement(ctBreak); |
| 279 | + |
| 280 | + CtCase<Integer> third = factory.createCase(); |
| 281 | + third.setCaseKind(CaseKind.COLON); |
| 282 | + third.addStatement(ctBreak); |
| 283 | + |
| 284 | + switchBlock.addCaseAt(0, third); |
| 285 | + switchBlock.addCaseAt(0,first); |
| 286 | + switchBlock.addCaseAt(1, second); |
| 287 | + |
| 288 | + assertEquals(Arrays.asList(first, second, third), switchBlock.getCases()); |
| 289 | + } |
| 290 | + |
| 291 | + @DisplayName("should throw IndexOutOfBounds exception") |
| 292 | + @Test |
| 293 | + public void test_addCaseAt_throwsIndexOutOfBoundsException_whenPositionIsOutOfBounds() { |
| 294 | + // contract: `addCaseAt` should throw an out of bounds exception when the the specified position is out of |
| 295 | + // bounds of the case collection |
| 296 | + Factory factory = new Launcher().getFactory(); |
| 297 | + |
| 298 | + CtSwitch<Integer> switchBlock = factory.createSwitch(); |
| 299 | + CtExpression<Integer> switchSelector = factory.createCodeSnippetExpression("x"); |
| 300 | + switchBlock.setSelector(switchSelector); |
| 301 | + |
| 302 | + CtCase<Integer> onlyCase = factory.createCase(); |
| 303 | + CtBreak caseStatement = factory.createBreak(); |
| 304 | + onlyCase.setCaseKind(CaseKind.COLON); |
| 305 | + onlyCase.addStatement(caseStatement); |
| 306 | + |
| 307 | + assertThrows(IndexOutOfBoundsException.class, () -> switchBlock.addCaseAt(5, onlyCase)); |
| 308 | + } |
| 309 | + } |
| 310 | + |
| 311 | + @Nested |
| 312 | + class InsertCaseInSwitchExpression { |
| 313 | + @DisplayName("cases should be inserted at the correct position") |
| 314 | + @Test |
| 315 | + public void test_addCaseAt_addsCaseAtSpecifiedPositionInSwitchExpression() { |
| 316 | + // contract: case should be added at the specified position in `CtSwitchExpression` |
| 317 | + Factory factory = new Launcher().getFactory(); |
| 318 | + |
| 319 | + CtSwitchExpression<Integer, Integer> switchExpression = factory.createSwitchExpression(); |
| 320 | + CtExpression<Integer> switchSelector = factory.createCodeSnippetExpression("x"); |
| 321 | + switchExpression.setSelector(switchSelector); |
| 322 | + |
| 323 | + CtCase<Integer> first = factory.createCase(); |
| 324 | + CtExpression<Integer> firstExpression = factory.createCodeSnippetExpression("1"); |
| 325 | + CtStatement firstStatement = factory.createCodeSnippetStatement("1"); |
| 326 | + first.addCaseExpression(firstExpression); |
| 327 | + first.setCaseKind(CaseKind.ARROW); |
| 328 | + first.addStatement(firstStatement); |
| 329 | + |
| 330 | + CtCase<Integer> second = factory.createCase(); |
| 331 | + CtExpression<Integer> secondExpression = factory.createCodeSnippetExpression("2"); |
| 332 | + CtStatement secondStatement = factory.createCodeSnippetStatement("2"); |
| 333 | + second.addCaseExpression(secondExpression); |
| 334 | + second.setCaseKind(CaseKind.ARROW); |
| 335 | + second.addStatement(secondStatement); |
| 336 | + |
| 337 | + CtCase<Integer> third = factory.createCase(); |
| 338 | + CtStatement thirdStatement = factory.createCodeSnippetStatement("3"); |
| 339 | + third.setCaseKind(CaseKind.ARROW); |
| 340 | + third.addStatement(thirdStatement); |
| 341 | + |
| 342 | + switchExpression.addCaseAt(0, third); |
| 343 | + switchExpression.addCaseAt(0,first); |
| 344 | + switchExpression.addCaseAt(1, second); |
| 345 | + |
| 346 | + assertEquals(Arrays.asList(first, second, third), switchExpression.getCases()); |
| 347 | + } |
| 348 | + |
| 349 | + @DisplayName("should throw IndexOutOfBounds exception") |
| 350 | + @Test |
| 351 | + public void test_addCaseAt_throwsIndexOutOfBoundsException_whenPositionIsOutOfBounds() { |
| 352 | + // contract: `addCaseAt` should throw an out of bounds exception when the the specified position is out of |
| 353 | + // bounds of the case collection |
| 354 | + Factory factory = new Launcher().getFactory(); |
| 355 | + |
| 356 | + CtSwitchExpression<Integer, Integer> switchExpression = factory.createSwitchExpression(); |
| 357 | + CtExpression<Integer> switchSelector = factory.createCodeSnippetExpression("x"); |
| 358 | + switchExpression.setSelector(switchSelector); |
| 359 | + |
| 360 | + CtCase<Integer> onlyCase = factory.createCase(); |
| 361 | + CtStatement caseStatement = factory.createCodeSnippetStatement("5"); |
| 362 | + onlyCase.setCaseKind(CaseKind.ARROW); |
| 363 | + onlyCase.addStatement(caseStatement); |
| 364 | + |
| 365 | + assertThrows(IndexOutOfBoundsException.class, () -> switchExpression.addCaseAt(3, onlyCase)); |
| 366 | + } |
| 367 | + } |
252 | 368 | }
|
0 commit comments