Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review: test: add test for SubstituionInsertAllNestedTypes #3998

Merged
merged 4 commits into from
Jun 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/test/java/spoon/test/template/SubstitutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,36 @@ private static class SingleFieldTemplate extends StatementTemplate {
@Override
public void statement() { }
}

@Test
public void testInsertAllNestedTypes() {
// contract: Substitution.insertAllNestedTypes inserts the only nested class from a singly nested template into the target class

// arrange
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/SubstitutionTest.java"));

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

CtType<?> targetType = factory.Class().create("goodClass");
StatementTemplate template = new SinglyNestedTemplate();

// act
Substitution.insertAllNestedTypes(targetType, template);

// assert
assertEquals(1, targetType.getNestedTypes().size());
Rohitesh-Kumar-Jain marked this conversation as resolved.
Show resolved Hide resolved
assertEquals("nestedClass", targetType.getNestedType("nestedClass").getSimpleName());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new assertion strengthens the test case in the sense that it verifies that there's a nested type with name nestedClass, and that's what was requested. So I think this is fine now.

However, I just wanted to note that the layout of the assertion makes it look like a test for CtType.getNestedType(String). To be clear, it looks like the assertion verifies that getting a nested type by a given name returns a type with that name. It would be sufficient here to just verify that the returned type is non-null, due to the functionality of getNestedType.

Suggested change
assertEquals("nestedClass", targetType.getNestedType("nestedClass").getSimpleName());
assertNotNull(targetType.getNestedType("nestedClass"));

Personally, I would probably have written both assertions like this instead, so as not to have to query targetType multiple times.

        List<CtType<?>> nestedTypes = targetType.getNestedTypes();
        assertThat(nestedTypes.size(), equalTo(1));
        assertThat(nestedTypes.get(0), equalTo("nestedClass"));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions, so I can add this in the PR that I will raise for another new test?

}

private static class SinglyNestedTemplate extends StatementTemplate {

class nestedClass {
}

@Override
public void statement() {
}
}
}