Skip to content

Commit

Permalink
fix: Fix the insertion order for CtStatementListImpl.insertBegin (#4065)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohitesh-Kumar-Jain authored Aug 10, 2021
1 parent 55db471 commit c5e8ade
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import static spoon.reflect.ModelElementContainerDefaultCapacities.BLOCK_STATEMENTS_CONTAINER_DEFAULT_CAPACITY;
import static spoon.reflect.path.CtRole.STATEMENT;
Expand Down Expand Up @@ -83,7 +84,10 @@ private void ensureModifiableStatementsList() {
@Override
public <T extends CtStatementList> T insertBegin(CtStatementList statements) {
ensureModifiableStatementsList();
for (CtStatement statement : statements.getStatements()) {
List<CtStatement> list = statements.getStatements();
ListIterator listIterator = list.listIterator(list.size());
while (listIterator.hasPrevious()) {
CtStatement statement = (CtStatement) listIterator.previous();
statement.setParent(this);
this.addStatement(0, statement);
}
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/spoon/test/ctStatementList/CtStatementListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package spoon.test.ctStatementList;

import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtStatementList;
import spoon.reflect.factory.Factory;
import spoon.support.reflect.code.CtStatementListImpl;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;

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

public class CtStatementListTest {

@Test
void testInsertBeginWithListOfStatements() {
// contract: insertBegin adds a list of statements at the beginning of the statementList

// arrange
Factory factory = new Launcher().getFactory();

CtStatement initialStatement = factory.Code().createCodeSnippetStatement("int preexisting = 42;").compile();

CtStatement firstStatementToBeInserted = factory.Code().createCodeSnippetStatement("int first = 1;").compile();
CtStatement secondStatementToBeInserted = factory.Code().createCodeSnippetStatement("int second = 2;").compile();

CtStatementList mainStatementList = new CtStatementListImpl<CtStatement>();
mainStatementList.addStatement(initialStatement);

CtStatementList statementListToBeAddedToTheMainList = new CtStatementListImpl<CtStatement>();
statementListToBeAddedToTheMainList.addStatement(firstStatementToBeInserted);
statementListToBeAddedToTheMainList.addStatement(secondStatementToBeInserted);

// act
mainStatementList.insertBegin(statementListToBeAddedToTheMainList);

// assert
CtStatement statementAtTheBeginningAfterInsertion = mainStatementList.getStatements().get(0);
CtStatement secondStatementAtTheBeginningOfTheListAfterInsertion = mainStatementList.getStatements().get(1);

assertEquals(firstStatementToBeInserted, statementAtTheBeginningAfterInsertion);
assertEquals(secondStatementToBeInserted, secondStatementAtTheBeginningOfTheListAfterInsertion);

assertThat(firstStatementToBeInserted.getParent(), is(mainStatementList));
assertThat(secondStatementToBeInserted.getParent(), is(mainStatementList));
}
}

0 comments on commit c5e8ade

Please sign in to comment.