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
+ */
1
17
package spoon .test .ctStatementList ;
2
18
3
19
import org .junit .jupiter .api .Test ;
4
20
import spoon .Launcher ;
5
21
import spoon .reflect .code .CtStatement ;
6
22
import spoon .reflect .code .CtStatementList ;
7
23
import spoon .reflect .factory .Factory ;
8
- import spoon .support .reflect .code .CtStatementListImpl ;
9
24
10
25
import static org .hamcrest .MatcherAssert .assertThat ;
11
26
import static org .hamcrest .CoreMatchers .is ;
12
27
13
28
import static org .junit .jupiter .api .Assertions .assertEquals ;
14
29
15
30
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
+ }
16
37
17
38
@ Test
18
39
void testInsertBeginWithListOfStatements () {
19
40
// contract: insertBegin adds a list of statements at the beginning of the statementList
20
41
21
42
// 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 ();
26
45
CtStatement firstStatementToBeInserted = factory .Code ().createCodeSnippetStatement ("int first = 1;" ).compile ();
27
46
CtStatement secondStatementToBeInserted = factory .Code ().createCodeSnippetStatement ("int second = 2;" ).compile ();
28
47
29
- CtStatementList mainStatementList = new CtStatementListImpl <CtStatement >();
30
- mainStatementList .addStatement (initialStatement );
31
-
32
- CtStatementList statementListToBeAddedToTheMainList = new CtStatementListImpl <CtStatement >();
48
+ CtStatementList statementListToBeAddedToTheMainList = factory .Core ().createStatementList ();
33
49
statementListToBeAddedToTheMainList .addStatement (firstStatementToBeInserted );
34
50
statementListToBeAddedToTheMainList .addStatement (secondStatementToBeInserted );
35
51
@@ -46,4 +62,73 @@ void testInsertBeginWithListOfStatements() {
46
62
assertThat (firstStatementToBeInserted .getParent (), is (mainStatementList ));
47
63
assertThat (secondStatementToBeInserted .getParent (), is (mainStatementList ));
48
64
}
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
+ }
49
134
}
0 commit comments