Skip to content

Commit

Permalink
feat(SameFilter): add utility class SameFilter (#3391)
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus authored Jun 4, 2020
1 parent e980d36 commit 12d748f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
27 changes: 27 additions & 0 deletions src/main/java/spoon/reflect/visitor/filter/SameFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* SPDX-License-Identifier: (MIT OR CECILL-C)
*
* Copyright (C) 2006-2019 INRIA and contributors
*
* Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) of the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
*/
package spoon.reflect.visitor.filter;

import spoon.reflect.declaration.CtElement;
import spoon.reflect.visitor.Filter;

/** Finds the element given in parameter, useful for checking if an element is in an ancestor.
* Here "same" refers to the Junit meaning: same object memory, equals with ==
*/
public class SameFilter implements Filter<CtElement> {
private final CtElement argument2;

public SameFilter(CtElement argument2) {
this.argument2 = argument2;
}

@Override
public boolean matches(CtElement element) {
return element == argument2;
}
}
9 changes: 2 additions & 7 deletions src/test/java/spoon/test/replace/ReplaceParametrizedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtScanner;
import spoon.reflect.visitor.CtVisitable;
import spoon.reflect.visitor.Filter;
import spoon.reflect.visitor.filter.SameFilter;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -174,12 +174,7 @@ public void scan(CtRole role, CtElement e) {
argument.replace(argument2);

// the new element is indeed now in this AST
assertTrue(receiver.getClass().getSimpleName() + " failed for " + mmField, receiver.getElements(new Filter<CtElement>() {
@Override
public boolean matches(CtElement element) {
return element == argument2;
}
}).size() == 1);
assertTrue(receiver.getClass().getSimpleName() + " failed for " + mmField, receiver.getElements(new SameFilter(argument2)).size() == 1);
}
if (!problems.isEmpty()) {
fail(getReport(problems));
Expand Down

0 comments on commit 12d748f

Please # to comment.