Skip to content

Commit

Permalink
test: Add test for Substitution.insertAllConstructors (#4005)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohitesh-Kumar-Jain authored Jun 28, 2021
1 parent 5f2e4f5 commit a3a5f8f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 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,7 @@

import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
Expand All @@ -10,8 +11,10 @@
import spoon.template.Substitution;

import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SubstitutionTest {

Expand Down Expand Up @@ -79,4 +82,36 @@ class nestedClass {
public void statement() {
}
}

@Test
public void testInsertAllConstructor() {
// contract: Substitution.insertAllConstructor inserts the only constructor from a single constructor 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 SingleConstructorTemplate();

// act
Substitution.insertAllConstructors(targetType, template);
List<?> typeMembers = targetType.getTypeMembers();

// assert
assertEquals(1, typeMembers.size());
assertTrue(typeMembers.get(0) instanceof CtConstructor);
}

private static class SingleConstructorTemplate extends StatementTemplate {

public SingleConstructorTemplate() { }

@Override
public void statement() {
}
}
}

0 comments on commit a3a5f8f

Please sign in to comment.