-
-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add CtTry.addCatcherAt
#4954
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b7492a6
Add method to add catcher at a specified position in the try statement
algomaster99 3109fa1
Add tests
algomaster99 8bc0965
Add Javadoc
algomaster99 44f4d89
Improve Javadoc
algomaster99 0510d3e
Update src/test/java/spoon/test/trycatch/TryCatchTest.java
algomaster99 3b7bd25
Update src/test/java/spoon/test/trycatch/TryCatchTest.java
algomaster99 1401c80
Incorporate changes
algomaster99 0a4c803
Give co-authorship to @slarse
algomaster99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -442,4 +444,52 @@ 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 = createCatch(factory, IOException.class); | ||
CtCatch second = createCatch(factory, ExceptionInInitializerError.class); | ||
CtCatch third = createCatch(factory, NoSuchMethodException.class); | ||
|
||
// act | ||
tryStatement | ||
.addCatcherAt(0, third) | ||
.addCatcherAt(0, first) | ||
.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 | ||
SirYwell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// arrange | ||
Factory factory = createFactory(); | ||
|
||
CtTry tryStatement = factory.createTry(); | ||
CtCatch catcherAtWrongPosition = createCatch(factory, Exception.class); | ||
|
||
// act & assert | ||
assertThrows(IndexOutOfBoundsException.class, | ||
() -> tryStatement.addCatcherAt(1, catcherAtWrongPosition)); | ||
} | ||
|
||
private <T extends Throwable> CtCatch createCatch(Factory factory, Class<T> typeToCatch) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made it type stricter by replacing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
CtTypeReference<T> typeReference = factory.Type().createReference(typeToCatch); | ||
return factory.createCatch().setParameter( | ||
factory.createCatchVariable().setType(typeReference) | ||
); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This test really tests three separate cases in one go:
Given that this functionality is neither complicated nor critical to Spoon, that's probably fine. But it always bears thinking about how much is tested in any single test. The fact that we never insert at the end of a non-empty list also isn't a big deal considering what the implementation looks like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Ideally, we should test only one case per unit test. I will keep this in mind. We can skip refactoring for this feature because, like you said, it is not that critical and is rather a very simple feature.
Tests of
addCatcher
would already test that so we do not need to think about them I guess.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Precisely, "considering what the implementation looks like" :). White box testing has merits in that we can omit redundant testing, but it has drawbacks to match (e.g. creating tests based on the implementation rather than the specification).