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 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
104 changes: 102 additions & 2 deletions src/test/java/spoon/test/template/SubstitutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

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;
import spoon.support.compiler.FileSystemFile;
import spoon.support.compiler.FileSystemFolder;
import spoon.template.StatementTemplate;
import spoon.template.Substitution;
import spoon.support.reflect.code.CtLiteralImpl;
import spoon.template.*;
import spoon.test.template.testclasses.types.AClassModel;
import spoon.test.template.testclasses.types.AnEnumModel;
import spoon.test.template.testclasses.types.AnIfaceModel;
Expand Down Expand Up @@ -206,6 +209,103 @@ public CtClass apply(CtType targetType) {
public void statement() { }
}

@Test
public void testSubstituteMethodBodyWithTemplatedInitializer() {
// contract: Given a block with a templated initializer, substituteMethodBody should return a
// new block with the initializer replaced with the value bound to the template parameter

// arrange
Factory factory = createFactoryWithTemplates();
CtClass<?> targetClass = factory.Class().create("TargetClass");

String templateExecutableName = "executable";
String templateVariableName = "s";
String initializerToSubstitute = "My chosen initializer";
CtStatement expectedStatement = factory.createLocalVariable(
factory.Type().stringType(), templateVariableName, factory.createLiteral(initializerToSubstitute)
);
final CtBlock<?> expectedMethodBody = factory.Code().createCtBlock(expectedStatement);

StatementWithTemplatedInitializer template = new StatementWithTemplatedInitializer();
template._initializer_ = factory.createLiteral(initializerToSubstitute);

// act
CtBlock<?> substitutedMethodBody = Substitution.substituteMethodBody(
targetClass, template, templateExecutableName
);

// assert
assertEquals(expectedMethodBody, substitutedMethodBody);
}

@Test
public void testSubstituteStatementWithTemplatedInitializer() {
// contract: Given a statement with a templated initializer, substituteStatement should
// return a new statement with the initializer replaced with the value bound to the template
// parameter

// arrange
Factory factory = createFactoryWithTemplates();
CtClass<?> targetClass = factory.Class().create("TargetClass");

String templateExecutableName = "executable";
int templateVariableIndex = 0;
String templateVariableName = "s";
String initializerToSubstitute = "My chosen initializer";
CtStatement expectedStatement = factory.createLocalVariable(
factory.Type().stringType(), templateVariableName, factory.createLiteral(initializerToSubstitute)
);

StatementWithTemplatedInitializer template = new StatementWithTemplatedInitializer();
template._initializer_ = factory.createLiteral(initializerToSubstitute);

// act
CtStatement substitutedStatement = Substitution.substituteStatement(
targetClass, template, templateVariableIndex, templateExecutableName
);

// assert
assertEquals(expectedStatement, substitutedStatement);
}

private static class StatementWithTemplatedInitializer extends ExtensionTemplate {
TemplateParameter<String> _initializer_;

public void executable() {
String s = _initializer_.S();
}
}

@Test
public void testSubstituteFieldDefaultExpressionWithTemplatedInitializer() {
// contract: Give an expression with a templated initializer, substituteFieldDefaultExpression should
// return a new expression with the initializer replaced with the value bound to the template parameter

// arrange
Factory factory = createFactoryWithTemplates();
CtType<?> targetType = factory.Class().create("TargetClass");

String initializerToSubstitute = "My chosen initializer";
String templateFieldName = "s";
CtExpression<String> expectedExpression = factory.createLiteral(initializerToSubstitute);

FieldWithTemplatedInitializer template = new FieldWithTemplatedInitializer();
template._initializer_ = factory.createLiteral(initializerToSubstitute);

// act
CtExpression<?> substitutedExpression = Substitution.substituteFieldDefaultExpression(
targetType, template, templateFieldName
);

// assert
assertEquals(expectedExpression, substitutedExpression);
}

private static class FieldWithTemplatedInitializer extends ExtensionTemplate {
TemplateParameter<String> _initializer_ = new CtLiteralImpl<>();
String s = _initializer_.S();
}

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