-
-
Notifications
You must be signed in to change notification settings - Fork 355
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
review: fix: Adjust the source position for annotated enum values #4780
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
064bbe4
test: Unit tests illustrating issue #4779
6f10fc0
fix: compute the source position after the last enum
73c1caa
fix: use tabs for indentation as the rest of the source file
fba14c2
fix: adjusted the comments for the calculation of the source start
edc4320
test: updated the unit test to use JUnit 5
321f5e6
test: removed incorrect unit test
5b9598d
fix: removed unnecessary new String(...)
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
60 changes: 60 additions & 0 deletions
60
src/test/java/spoon/test/prettyprinter/SniperAnnotatedEnumTest.java
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package spoon.test.prettyprinter; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.hamcrest.CoreMatchers; | ||
import org.junit.jupiter.api.BeforeAll; | ||
|
||
import spoon.Launcher; | ||
import spoon.compiler.Environment; | ||
import spoon.reflect.CtModel; | ||
import spoon.reflect.code.CtComment.CommentType; | ||
import spoon.reflect.declaration.CtClass; | ||
import spoon.reflect.visitor.filter.TypeFilter; | ||
import spoon.support.sniper.SniperJavaPrettyPrinter; | ||
import spoon.test.GitHubIssue; | ||
|
||
public class SniperAnnotatedEnumTest { | ||
private static final Path INPUT_PATH = Paths.get("src/test/java/"); | ||
private static final Path OUTPUT_PATH = Paths.get("target/test-output"); | ||
|
||
@BeforeAll | ||
public static void setup() throws IOException { | ||
FileUtils.deleteDirectory(OUTPUT_PATH.toFile()); | ||
} | ||
|
||
@GitHubIssue(issueNumber = 4779, fixed = true) | ||
public void annotatedEnumTest() throws IOException { | ||
runSniperJavaPrettyPrinter("spoon/test/prettyprinter/testclasses/AnnotatedEnum.java"); | ||
} | ||
|
||
private void runSniperJavaPrettyPrinter(String path) throws IOException { | ||
final Launcher launcher = new Launcher(); | ||
final Environment e = launcher.getEnvironment(); | ||
e.setLevel("INFO"); | ||
e.setPrettyPrinterCreator(() -> new SniperJavaPrettyPrinter(e)); | ||
|
||
launcher.addInputResource(INPUT_PATH.resolve(path).toString()); | ||
launcher.setSourceOutputDirectory(OUTPUT_PATH.toString()); | ||
|
||
CtModel model = launcher.buildModel(); | ||
CtClass ctClass = model.getElements(new TypeFilter<>(CtClass.class)).get(0); | ||
|
||
ctClass.addComment(launcher.getFactory().Code().createComment("test", CommentType.BLOCK)); | ||
|
||
launcher.process(); | ||
launcher.prettyprint(); | ||
// Verify result file exists and is not empty | ||
assertThat("Output file for " + path + " should exist", OUTPUT_PATH.resolve(path).toFile().exists(), | ||
CoreMatchers.equalTo(true)); | ||
|
||
String content = Files.readString(OUTPUT_PATH.resolve(path)); | ||
assertThat(content.trim(), CoreMatchers.containsString("/* test */public enum")); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/java/spoon/test/prettyprinter/testclasses/AnnotatedEnum.java
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package spoon.test.prettyprinter.testclasses; | ||
|
||
public enum AnnotatedEnum { | ||
ONE(1, "one"), | ||
TWO(2, "two"), | ||
THREE(3, "three"), | ||
/** | ||
* @deprecated | ||
*/ | ||
@Deprecated | ||
// There was a typo... | ||
FOR(4, "for"), | ||
FOUR(4, "four"); | ||
|
||
private final int value; | ||
private final String text; | ||
|
||
AnnotatedEnum(int value, String text) { | ||
this.value = value; | ||
this.text = text; | ||
} | ||
} |
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.
There are comments below, starting with
1)
. I think it would make sense to add one for annotations too and change the numeration accordingly.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.
Agreed, I have updated the comments as proposed