Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
algomaster99 committed Oct 12, 2022
1 parent b7492a6 commit 3109fa1
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/test/java/spoon/test/trycatch/TryCatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import spoon.Launcher;
Expand All @@ -36,7 +37,6 @@
import spoon.reflect.code.CtResource;
import spoon.reflect.code.CtTry;
import spoon.reflect.code.CtTryWithResource;
import spoon.reflect.code.CtVariableRead;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtVariable;
Expand All @@ -56,11 +56,13 @@

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static spoon.testing.utils.ModelUtils.build;
Expand Down Expand Up @@ -442,4 +444,60 @@ public void testTryWithVariableAsResource() {
tryStmt.removeResource(ctResource);

}

@Nested
class AddCatcherAt {
@Test
void addsCatcherAtTheSpecifiedPosition() {
// contract: the catcher should be added at the specified position
// arrange
Factory factory = createFactory();

CtTry tryStatement = factory.createTry();

CtCatch first = factory.createCatch();
first.setParameter(factory
.createCatchVariable()
.setType(factory.Type().createReference(IOException.class)));

CtCatch second = factory.createCatch();
second.setParameter(factory
.createCatchVariable()
.setMultiTypes(List.of(
factory.Type().createReference(ExceptionInInitializerError.class),
factory.Type().createReference(NoSuchFieldException.class))));

CtCatch third = factory.createCatch();
third.setParameter(factory
.createCatchVariable()
.setType(factory.Type().createReference(NoSuchMethodException.class)));

// act
tryStatement.addCatcherAt(0, third);
tryStatement.addCatcherAt(0, first);
tryStatement.addCatcherAt(1, second);

// assert
assertThat(tryStatement.getCatchers(), contains(first, second, third));
}

@Test
void throwsOutOfBoundsException_whenPositionIsOutOfBounds() {
// contract: `addCatcherAt` should throw an out-of-bounds exception when the specified position is out of
// bounds of the catcher collection

// arrange
Factory factory = createFactory();

CtTry tryStatement = factory.createTry();
CtCatch catcherAtWrongPosition = factory.createCatch();
catcherAtWrongPosition.setParameter(factory
.createCatchVariable()
.setType(factory.Type().createReference(Exception.class)));

// act & assert
assertThrows(IndexOutOfBoundsException.class,
() -> tryStatement.addCatcherAt(2, catcherAtWrongPosition));
}
}
}

0 comments on commit 3109fa1

Please sign in to comment.