Skip to content

Commit

Permalink
test: improve tests for NameFilter class. (#3975)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohitesh-Kumar-Jain committed Jun 11, 2021
1 parent a4d3aaa commit 414b6d6
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/test/java/spoon/test/filters/FilterTest.java
Expand Up @@ -111,13 +111,46 @@ public void setup() throws Exception {

@Test
public void testNameFilter() throws Exception {
// contract: legacy NameFilter is tested and works
// contract: NameFilter finds the expected amount of elements when filtering

CtClass<?> foo = factory.Package().get("spoon.test.filters.testclasses").getType("Foo");
assertEquals("Foo", foo.getSimpleName());
List<CtNamedElement> elements = foo.getElements(new NameFilter<>("i"));
assertEquals(1, elements.size());
}

@Test
void testNameFilterMatchesReturnsTrueForMatchingName() {
// contract: NameFilter.matches should return true for an element with a matching name

// arrange
String name = "someNiceName";
NameFilter<CtNamedElement> nameFilter = new NameFilter<>(name);
CtNamedElement elemWithMatchingName = new Launcher().getFactory().createLocalVariable();
elemWithMatchingName.setSimpleName(name);

// act
boolean matches = nameFilter.matches(elemWithMatchingName);

// assert
assertTrue(matches);
}

@Test
public void testNameFilterThrowsExceptionForNull() {
// contract: NameFilter constructor should throw IllegalArgumentException when passed null

assertThrows(IllegalArgumentException.class, () -> new NameFilter<>(null));
}

@Test
public void testNameFilterGetType() {
// contract: NameFilter.getType() should return the CtNamedElement class

NameFilter<CtNamedElement> nameFilter = new NameFilter<>("i");
assertEquals(CtNamedElement.class, nameFilter.getType());
}

@Test
public void testFilters() {
CtClass<?> foo = factory.Package().get("spoon.test.filters.testclasses").getType("Foo");
Expand Down

0 comments on commit 414b6d6

Please sign in to comment.