Skip to content

Commit

Permalink
test: add a test to check if TemplateMatcher can match two equivalent…
Browse files Browse the repository at this point in the history
… snippets (#1059)
  • Loading branch information
surli authored and monperrus committed Dec 20, 2016
1 parent ff57253 commit 18b0578
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/test/java/spoon/test/template/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtNamedElement;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.ModelConsistencyChecker;
Expand Down Expand Up @@ -325,4 +326,27 @@ public void testTemplateMatcherWithWholePackage() throws Exception {
assertEquals("Parent of the method is not a class called BServiceImpl", "BServiceImpl", bservice.getSimpleName());
}

@Test
public void testTemplateMatcherMatchTwoSnippets() throws Exception {
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/TwoSnippets.java");
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SecurityCheckerTemplate.java"));

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

CtClass<?> templateKlass = factory.Class().get(SecurityCheckerTemplate.class);
CtMethod templateMethod = (CtMethod) templateKlass.getElements(new NameFilter("matcher1")).get(0);
CtIf templateRoot = (CtIf) templateMethod.getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);

List<CtElement> matches = matcher.find(factory.getModel().getRootPackage());

assertEquals(2, matches.size());

CtElement match1 = matches.get(0);
CtElement match2 = matches.get(1);

assertTrue(match1.equals(match2));
}
}
37 changes: 37 additions & 0 deletions src/test/java/spoon/test/template/testclasses/TwoSnippets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2006-2016 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and abiding by the rules of distribution of free software. You can use, modify and/or redistribute the software under the terms of the CeCILL-C license as circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had knowledge of the CeCILL-C license and that you accept its terms.
*/

package spoon.test.template.testclasses;

/**
* Created by urli on 08/12/2016.
*/
public class TwoSnippets {

private String bDao;

private ContextHelper contextHelper;

public void hello() {
if(!contextHelper.hasPermission("c")) {
throw new SecurityException();
}
bDao.toString();
}

public void toto() {
if(!contextHelper.hasPermission("c")) {
throw new SecurityException();
}
bDao.toString();
}
}

0 comments on commit 18b0578

Please sign in to comment.