Skip to content

Commit

Permalink
fix(getparent): getParent(Filter) does not return the current element…
Browse files Browse the repository at this point in the history
… when it matches the filter

close #637
  • Loading branch information
tdurieux committed Apr 28, 2016
1 parent 9c01a53 commit cf946ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public <P extends CtElement> P getParent(Class<P> parentType) throws ParentNotIn
@Override
@SuppressWarnings("unchecked")
public <E extends CtElement> E getParent(Filter<E> filter) throws ParentNotInitializedException {
E current = (E) this;
E current = (E) getParent();
while (true) {
try {
while (current != null && !filter.matches(current)) {
Expand Down
29 changes: 19 additions & 10 deletions src/test/java/spoon/test/parent/ParentTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
package spoon.test.parent;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static spoon.testing.utils.ModelUtils.build;

import java.util.Stack;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import spoon.Launcher;
import spoon.compiler.SpoonResourceHelper;
import spoon.reflect.code.BinaryOperatorKind;
Expand All @@ -21,9 +12,9 @@
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtVariableRead;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtStatementList;
import spoon.reflect.code.CtVariableRead;
import spoon.reflect.code.CtWhile;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtElement;
Expand All @@ -42,6 +33,14 @@
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.replace.testclasses.Tacos;

import java.util.Stack;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static spoon.testing.utils.ModelUtils.build;

public class ParentTest {

Factory factory;
Expand Down Expand Up @@ -267,6 +266,16 @@ public boolean matches(CtType element) {
// not present element
CtWhile ctWhile = ctStatement1.getParent(new TypeFilter<CtWhile>(CtWhile.class));
assertEquals(null, ctWhile);

CtStatement statementParent = statement
.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {
@Override
public boolean matches(CtStatement element) {
return true;
}
});
// getParent must not return the current element
assertNotEquals(statement, statementParent);
}

}

0 comments on commit cf946ee

Please sign in to comment.