Skip to content
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

refactor: improve error message when sniper printer is set after building model #3694

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ public SniperJavaPrettyPrinter(Environment env) {
private ChangeCollector getChangeCollector() {
ChangeCollector changeCollector = ChangeCollector.getChangeCollector(env);
if (changeCollector == null) {
throw new SpoonException(ChangeCollector.class.getSimpleName() + " was not attached to the Environment");
throw new SpoonException(ChangeCollector.class.getSimpleName() + " was not attached to the Environment. "
+ "This typically means that the Sniper printer was set after building the model. "
+ "It must be set before building the model.");
}
return changeCollector;
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,34 @@ public void testPrintTypesThrowsWhenPassedTypesFromMultipleCompilationUnits() {
}
}

@Test
public void testCalculateCrashesWithInformativeMessageWhenSniperPrinterSetAfterModelBuild() {
// contract: The sniper printer must be set before building the model, and the error message
// one gets when this has not been done should say so.

Launcher launcher = new Launcher();
launcher.addInputResource(getResourcePath("visibility.YamlRepresenter"));

// build model, then set sniper
launcher.buildModel();
launcher.getEnvironment().setPrettyPrinterCreator(
() -> new SniperJavaPrettyPrinter(launcher.getEnvironment()));

CtCompilationUnit cu = launcher.getFactory().CompilationUnit().getMap().values().stream()
.findFirst().get();

// crash because sniper was set after model was built, and so the ChangeCollector was not
// attached to the environment
try {
launcher.createPrettyPrinter().calculate(cu, cu.getDeclaredTypes());
} catch (SpoonException e) {
assertThat(e.getMessage(), containsString(
"This typically means that the Sniper printer was set after building the model."));
assertThat(e.getMessage(), containsString(
"It must be set before building the model."));
}
}

@Test
public void testNewlineInsertedBetweenCommentAndTypeMemberWithAddedModifier() {
assumeNotWindows(); // FIXME Make test case pass on Windows
Expand Down