Unverified Commit 9d43ea3d authored by Rohitesh Kumar Jain's avatar Rohitesh Kumar Jain Committed by GitHub
Browse files

test: add test for insertAllFields method (#3995)

parent 5690c4a9
package spoon.test.template;
import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.support.compiler.FileSystemFile;
import spoon.template.StatementTemplate;
import spoon.template.Substitution;
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SubstitutionTest {
@Test
public void testSubstitutionInsertAllFields() {
// contract: Substitution.insertAllFields inserts the only field from a single-field 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();
CtField<String> expectedField = factory.createField();
expectedField.setSimpleName("testString");
expectedField.setAssignment(factory.createLiteral("goodName"));
expectedField.setType(factory.Type().stringType());
CtType<?> targetType = factory.Class().create("someClass");
StatementTemplate template = new SingleFieldTemplate();
// act
Substitution.insertAllFields(targetType, template);
// assert
assertEquals(Collections.singletonList(expectedField), targetType.getFields());
}
private static class SingleFieldTemplate extends StatementTemplate {
String testString = "goodName";
@Override
public void statement() { }
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment