forked from INRIA/spoon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from INRIA/master
fix: fix crash with SpoonException in sniper mode (INRIA#3205)
- Loading branch information
Showing
5 changed files
with
108 additions
and
8 deletions.
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
70 changes: 70 additions & 0 deletions
70
src/test/java/spoon/test/prettyprinter/SniperInnerTypeTest.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,70 @@ | ||
package spoon.test.prettyprinter; | ||
|
||
import org.apache.commons.io.*; | ||
import org.hamcrest.*; | ||
import org.junit.*; | ||
import static org.junit.Assert.*; | ||
|
||
import spoon.Launcher; | ||
import spoon.compiler.*; | ||
import spoon.reflect.*; | ||
import spoon.reflect.code.CtComment.*; | ||
import spoon.reflect.declaration.*; | ||
import spoon.reflect.visitor.filter.*; | ||
import spoon.support.sniper.*; | ||
|
||
import java.io.*; | ||
import java.nio.charset.*; | ||
import java.nio.file.*; | ||
|
||
public class SniperInnerTypeTest { | ||
private static final Path INPUT_PATH = Paths.get("src/test/java/"); | ||
private static final Path OUTPUT_PATH = Paths.get("target/test-output"); | ||
|
||
@BeforeClass | ||
public static void setup() throws IOException { | ||
FileUtils.deleteDirectory(OUTPUT_PATH.toFile()); | ||
} | ||
|
||
@Test | ||
public void innerTypeCrash() throws IOException { | ||
runSniperJavaPrettyPrinter("spoon/test/prettyprinter/testclasses/innertype/InnerTypeCrash.java"); | ||
} | ||
|
||
@Test | ||
public void innerTypeOk() throws IOException { | ||
runSniperJavaPrettyPrinter("spoon/test/prettyprinter/testclasses/innertype/InnerTypeOk.java"); | ||
} | ||
|
||
@Test | ||
public void innerTypeOk2() throws IOException { | ||
runSniperJavaPrettyPrinter("spoon/test/prettyprinter/testclasses/innertype/InnerTypeOk2.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 exist and is not empty | ||
assertThat("Output file for " + path + " should exist", OUTPUT_PATH.resolve(path).toFile().exists(), | ||
CoreMatchers.equalTo(true)); | ||
|
||
String content = new String(Files.readAllBytes(OUTPUT_PATH.resolve(path)), StandardCharsets.UTF_8); | ||
|
||
assertThat(content, CoreMatchers.notNullValue()); | ||
assertThat("Result class should not be empty", content.trim(), CoreMatchers.not(CoreMatchers.equalTo(""))); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/test/java/spoon/test/prettyprinter/testclasses/innertype/InnerTypeCrash.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,10 @@ | ||
package spoon.test.prettyprinter.testclasses.innertype; | ||
|
||
import java.util.*; | ||
|
||
public class InnerTypeCrash { | ||
private void test() | ||
{ | ||
Map.Entry<String, String> test; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/test/java/spoon/test/prettyprinter/testclasses/innertype/InnerTypeOk.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,10 @@ | ||
|
||
package spoon.test.prettyprinter.testclasses.innertype; | ||
|
||
import java.util.Map.*; | ||
|
||
public class InnerTypeOk { | ||
private void test() { | ||
Entry<String, String> test; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/java/spoon/test/prettyprinter/testclasses/innertype/InnerTypeOk2.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,9 @@ | ||
package spoon.test.prettyprinter.testclasses.innertype; | ||
|
||
import java.util.*; | ||
|
||
public class InnerTypeOk2 { | ||
private void test() { | ||
Map.Entry test; | ||
} | ||
} |