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 tests for substitution class #4018

Merged
merged 6 commits into from
Jul 5, 2021
Merged
Changes from 1 commit
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
58 changes: 58 additions & 0 deletions src/test/java/spoon/test/template/SubstitutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.*;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtTypeReference;
Expand Down Expand Up @@ -206,6 +209,61 @@ public CtClass apply(CtType targetType) {
public void statement() { }
}

@Test
public void testSubstituteMethodBody() {
// contract: Substitution.SubstituteMethodBody gets a body from a template consisting of an executable with single statement

// arrange
Factory factory = createFactoryWithTemplates();
CtClass<?> targetClass = factory.Class().create("testClass");
StatementTemplate template = new executableWithSingleStatementTemplate();

// act
CtBlock<?> ctBlock = Substitution.substituteMethodBody(targetClass, template, "executable");
List<CtStatement> statements = ctBlock.getStatements();
CtNamedElement statement = (CtNamedElement) statements.get(0);

// assert
assertEquals(1, statements.size());
assertEquals("testString", statement.getSimpleName());
}

@Test
public void testSubstituteStatement() {
// contract: Substitution.SubstituteStatement gets a statement from a template consisting of an executable with single statement, having testString at it's 0 index

Factory factory = createFactoryWithTemplates();
CtClass<?> targetClass = factory.Class().create("testClass");
StatementTemplate template = new executableWithSingleStatementTemplate();

CtStatement ctStatement = Substitution.substituteStatement(targetClass, template, 0, "executable");

assertEquals("testString", ((CtNamedElement) ctStatement).getSimpleName());
}

private static class executableWithSingleStatementTemplate extends StatementTemplate {

public void executable() {
String testString;
}

@Override
public void statement() { }
}

@Test
public void testSubstituteFieldDefaultExpression() {
// contract: Substitution.SubstituteFieldDefaultExpression gets a default expression from a single-field template

Factory factory = createFactoryWithTemplates();
CtType<?> targetType = factory.Class().create("testClass");
StatementTemplate template = new SingleFieldTemplate();

CtExpression<?> ctExpression = Substitution.substituteFieldDefaultExpression(targetType, template, "testString");

assertEquals("\"goodName\"", ctExpression.toString());
}

private static Factory createFactoryWithTemplates() {
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/SubstitutionTest.java"));
Expand Down