Skip to content

Commit

Permalink
refactor: improve error message when sniper printer is set after buil…
Browse files Browse the repository at this point in the history
…ding model (INRIA#3694)
  • Loading branch information
slarse authored and woutersmeenk committed Aug 29, 2021
1 parent 04bf1bc commit 129ce16
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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

0 comments on commit 129ce16

Please sign in to comment.