Skip to content

Commit 0ccaa63

Browse files
Rohitesh-Kumar-Jainwoutersmeenk
authored andcommitted
test: add tests for CtStatementList (INRIA#4089)
1 parent d54e803 commit 0ccaa63

File tree

1 file changed

+94
-9
lines changed

1 file changed

+94
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,51 @@
1+
/**
2+
* Copyright (C) 2006-2021 INRIA and contributors
3+
* Spoon - http://spoon.gforge.inria.fr/
4+
*
5+
* This software is governed by the CeCILL-C License under French law and
6+
* abiding by the rules of distribution of free software. You can use, modify
7+
* and/or redistribute the software under the terms of the CeCILL-C license as
8+
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
9+
*
10+
* This program is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
13+
*
14+
* The fact that you are presently reading this means that you have had
15+
* knowledge of the CeCILL-C license and that you accept its terms.
16+
*/
117
package spoon.test.ctStatementList;
218

319
import org.junit.jupiter.api.Test;
420
import spoon.Launcher;
521
import spoon.reflect.code.CtStatement;
622
import spoon.reflect.code.CtStatementList;
723
import spoon.reflect.factory.Factory;
8-
import spoon.support.reflect.code.CtStatementListImpl;
924

1025
import static org.hamcrest.MatcherAssert.assertThat;
1126
import static org.hamcrest.CoreMatchers.is;
1227

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

1530
public class CtStatementListTest {
31+
private static CtStatementList getStatementListInitializedWithOneStatement() {
32+
Factory factory = new Launcher().getFactory();
33+
CtStatementList statementList = factory.Core().createStatementList();
34+
statementList.addStatement(factory.createCodeSnippetStatement("int preExisting = 0;").compile());
35+
return statementList;
36+
}
1637

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

2142
// arrange
22-
Factory factory = new Launcher().getFactory();
23-
24-
CtStatement initialStatement = factory.Code().createCodeSnippetStatement("int preexisting = 42;").compile();
25-
43+
CtStatementList mainStatementList = getStatementListInitializedWithOneStatement();
44+
Factory factory = mainStatementList.getFactory();
2645
CtStatement firstStatementToBeInserted = factory.Code().createCodeSnippetStatement("int first = 1;").compile();
2746
CtStatement secondStatementToBeInserted = factory.Code().createCodeSnippetStatement("int second = 2;").compile();
2847

29-
CtStatementList mainStatementList = new CtStatementListImpl<CtStatement>();
30-
mainStatementList.addStatement(initialStatement);
31-
32-
CtStatementList statementListToBeAddedToTheMainList = new CtStatementListImpl<CtStatement>();
48+
CtStatementList statementListToBeAddedToTheMainList = factory.Core().createStatementList();
3349
statementListToBeAddedToTheMainList.addStatement(firstStatementToBeInserted);
3450
statementListToBeAddedToTheMainList.addStatement(secondStatementToBeInserted);
3551

@@ -46,4 +62,73 @@ void testInsertBeginWithListOfStatements() {
4662
assertThat(firstStatementToBeInserted.getParent(), is(mainStatementList));
4763
assertThat(secondStatementToBeInserted.getParent(), is(mainStatementList));
4864
}
65+
66+
@Test
67+
void testInsertBeginWithSingleStatement() {
68+
// contract: insertBegin adds a statement at the beginning of the statementList
69+
70+
CtStatementList statementList = getStatementListInitializedWithOneStatement();
71+
Factory factory = statementList.getFactory();
72+
CtStatement statementToBeInserted = factory.Code().createCodeSnippetStatement("int first = 1").compile();
73+
74+
statementList.insertBegin(statementToBeInserted);
75+
76+
CtStatement statementAtTheBeginningAfterInsertion = statementList.getStatements().get(0);
77+
assertThat(statementAtTheBeginningAfterInsertion, is(statementToBeInserted));
78+
assertThat(statementAtTheBeginningAfterInsertion.getParent(), is(statementList));
79+
}
80+
81+
@Test
82+
void testRemoveStatement() {
83+
// contract: removeStatement removes a statement form a StatementList having a single statement
84+
85+
CtStatementList statementList = getStatementListInitializedWithOneStatement();
86+
assertThat(statementList.getStatements().size(), is(1));
87+
88+
statementList.removeStatement(statementList.getStatements().get(0));
89+
90+
assertThat(statementList.getStatements().size(), is(0));
91+
}
92+
93+
@Test
94+
void testInsertEndWithSingleStatement() {
95+
// contract: insertEnd adds a statement at the end of a StatementList, i.e below an already existing statement
96+
97+
CtStatementList statementList = getStatementListInitializedWithOneStatement();
98+
Factory factory = statementList.getFactory();
99+
CtStatement statementToBeInserted = factory.Code().createCodeSnippetStatement("int first = 1;").compile();
100+
101+
statementList.insertEnd(statementToBeInserted);
102+
103+
int lastStatementIndex = statementList.getStatements().size() - 1;
104+
assertThat(statementList.getStatement(lastStatementIndex), is(statementToBeInserted));
105+
assertThat(statementToBeInserted.getParent(), is(statementList));
106+
}
107+
108+
@Test
109+
void testInsertEndWithListOfStatements() {
110+
// contract: insertEnd adds a list of statements at the end of a statementList, i.e the list is added below an
111+
// existing statement, and the order of statements in the list remains the same
112+
113+
// arrange
114+
CtStatementList mainStatementList = getStatementListInitializedWithOneStatement();
115+
Factory factory = mainStatementList.getFactory();
116+
CtStatement firstStatementToBeInserted = factory.createCodeSnippetStatement("int first = 1;").compile();
117+
CtStatement secondStatementToBeInserted = factory.createCodeSnippetStatement("int second = 2;").compile();
118+
119+
CtStatementList statementList = factory.Core().createStatementList();
120+
statementList.addStatement(firstStatementToBeInserted);
121+
statementList.addStatement(secondStatementToBeInserted);
122+
123+
// act
124+
mainStatementList.insertEnd(statementList);
125+
126+
// assert
127+
int lastStatementIndex = mainStatementList.getStatements().size() - 1;
128+
int secondLastStatementIndex = lastStatementIndex - 1;
129+
assertThat(mainStatementList.getStatement(secondLastStatementIndex), is(firstStatementToBeInserted));
130+
assertThat(mainStatementList.getStatement(lastStatementIndex), is(secondStatementToBeInserted));
131+
assertThat(firstStatementToBeInserted.getParent(), is(mainStatementList));
132+
assertThat(secondStatementToBeInserted.getParent(), is(mainStatementList));
133+
}
49134
}

0 commit comments

Comments
 (0)