Skip to content

Commit

Permalink
test: PatternParameterConfigurator substitute by element
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Sep 27, 2018
1 parent 7c534a5 commit faa40ac
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/test/java/spoon/test/template/PatternTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,34 @@ public void testMatchType() {
}
}

@Test
public void testSubstituteExactElements() {
//contract: one can substitute exactly defined element
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] {"--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/template/testclasses/logger/Logger.java");

launcher.buildModel();
Factory factory = launcher.getFactory();

final CtClass<?> aTargetType = launcher.getFactory().Class().get(Logger.class);
CtMethod tobeSubstititedMethod = aTargetType.getMethodsByName("enter").get(0);
Pattern pattern = PatternBuilder.create(aTargetType)
.configurePatternParameters(pb -> {
//substitute NAME of method
pb.parameter("methodName").byRole(CtRole.NAME, tobeSubstititedMethod);
//substitute Body of method
pb.parameter("methodBody").byElement(tobeSubstititedMethod.getBody());
}).build();

List<Match> matches = pattern.getMatches(aTargetType);
assertEquals(1, matches.size());
Match match = matches.get(0);
assertSame(aTargetType, match.getMatchingElement());
assertEquals("enter", match.getParameters().getValue("methodName"));
assertSame(tobeSubstititedMethod.getBody(), match.getParameters().getValue("methodBody"));
}

private Map<String, Object> getMap(Match match, String name) {
Object v = match.getParametersMap().get(name);
assertNotNull(v);
Expand Down

0 comments on commit faa40ac

Please sign in to comment.