Skip to content

Commit

Permalink
adds new tests for TemplateMatcher
Browse files Browse the repository at this point in the history
everything works now, incredible!

a side-effect on the recent work on references?
  • Loading branch information
monperrus committed Dec 10, 2015
1 parent 876d36d commit 2e956b1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 29 deletions.
11 changes: 0 additions & 11 deletions src/main/java/spoon/support/template/DefaultParameterMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package spoon.support.template;

import spoon.reflect.declaration.CtElement;
import spoon.reflect.reference.CtReference;
import spoon.template.TemplateMatcher;

/**
Expand All @@ -39,14 +38,4 @@ public boolean match(TemplateMatcher templateMatcher, CtElement template,
return true;
}

/**
* Default implementation.
*
* @return always <code>true</code>.
*/
public boolean match(TemplateMatcher templateMatcher, CtReference template,
CtReference toMatch) {
return true;
}

}
15 changes: 0 additions & 15 deletions src/main/java/spoon/support/template/ParameterMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package spoon.support.template;

import spoon.reflect.declaration.CtElement;
import spoon.reflect.reference.CtReference;
import spoon.template.TemplateMatcher;

/**
Expand Down Expand Up @@ -48,18 +47,4 @@ public interface ParameterMatcher {
*/
boolean match(TemplateMatcher templateMatcher, CtElement template, CtElement toMatch);

/**
* To be defined to implement a matching strategy for template parameter(s).
*
* @param templateMatcher
* the instance of the matcher that is currently performing the
* matching (up-caller)
* @param template
* the template reference to match against
* @param toMatch
* the reference to be tested for a match
* @return true if matching
*/
boolean match(TemplateMatcher templateMatcher, CtReference template, CtReference toMatch);

}
6 changes: 6 additions & 0 deletions src/test/java/spoon/test/template/CheckBound.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public void foo() {
throw new IndexOutOfBoundsException();
}

public void foo2() {
if (new ArrayList<>().size() > 11)
throw new IndexOutOfBoundsException();
}

public void bar() {
if (l.size() > 10)
throw new IndexOutOfBoundsException();
Expand All @@ -31,6 +36,7 @@ public void bou() {
}

public void bov() {
System.out.println("noise");
if (new ArrayList<>().size() > 10) // no block
System.out.println();
}
Expand Down
28 changes: 25 additions & 3 deletions src/test/java/spoon/test/template/CheckBoundMatcher.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package spoon.test.template;

import spoon.template.TemplateParameter;

import java.util.Collection;

import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtStatement;
import spoon.template.TemplateParameter;

public class CheckBoundMatcher {

public TemplateParameter<Collection<?>> _col_;

public TemplateParameter<Integer> _x_;
public TemplateParameter<CtBlock> _block_;
public TemplateParameter<CtStatement> _stmt_;

public void matcher1() {
if (_col_.S().size() > 10)
throw new IndexOutOfBoundsException();
Expand All @@ -18,4 +23,21 @@ public void matcher2() {
System.out.println();
}

public void matcher3() {
if (_x_.S() > 10)
throw new IndexOutOfBoundsException();
}

public void matcher4() {
if (_x_.S() > _x_.S())
throw new IndexOutOfBoundsException();
}

public void matcher5() {
if (_x_.S() > _x_.S()) _block_.S();
}

public void matcher6() {
if (_x_.S() > _x_.S()) { _stmt_.S(); }
}
}
31 changes: 31 additions & 0 deletions src/test/java/spoon/test/template/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,37 @@ public void testTemplateMatcher() throws Exception {
assertEquals(1, matcher.find(klass).size());
}

{// testing matcher3
CtClass<?> templateKlass = factory.Class().get(CheckBoundMatcher.class);
CtClass<?> klass = factory.Class().get(CheckBound.class);
CtIf templateRoot = (CtIf) ((CtMethod) templateKlass.getElements(new NameFilter("matcher3")).get(0)).getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);
assertEquals(2, matcher.find(klass).size());
}

{// testing matcher4
CtClass<?> templateKlass = factory.Class().get(CheckBoundMatcher.class);
CtClass<?> klass = factory.Class().get(CheckBound.class);
CtIf templateRoot = (CtIf) ((CtMethod) templateKlass.getElements(new NameFilter("matcher4")).get(0)).getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);
assertEquals(3, matcher.find(klass).size());
}

{// testing matcher5
CtClass<?> templateKlass = factory.Class().get(CheckBoundMatcher.class);
CtClass<?> klass = factory.Class().get(CheckBound.class);
CtIf templateRoot = (CtIf) ((CtMethod) templateKlass.getElements(new NameFilter("matcher5")).get(0)).getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);
assertEquals(6, matcher.find(klass).size());
}

{// testing matcher6
CtClass<?> templateKlass = factory.Class().get(CheckBoundMatcher.class);
CtClass<?> klass = factory.Class().get(CheckBound.class);
CtIf templateRoot = (CtIf) ((CtMethod) templateKlass.getElements(new NameFilter("matcher6")).get(0)).getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);
assertEquals(2, matcher.find(klass).size());
}
}

}

0 comments on commit 2e956b1

Please sign in to comment.