|
20 | 20 | import spoon.Launcher;
|
21 | 21 | import spoon.reflect.declaration.CtClass;
|
22 | 22 | import spoon.reflect.declaration.CtMethod;
|
| 23 | +import spoon.reflect.declaration.CtParameter; |
23 | 24 | import spoon.reflect.declaration.CtType;
|
24 | 25 | import spoon.reflect.declaration.ModifierKind;
|
25 | 26 | import spoon.reflect.factory.Factory;
|
| 27 | +import spoon.reflect.reference.CtTypeReference; |
26 | 28 | import spoon.reflect.visitor.filter.NamedElementFilter;
|
27 | 29 | import spoon.test.delete.testclasses.Adobada;
|
28 | 30 | import spoon.test.method.testclasses.Methods;
|
29 | 31 | import spoon.test.method.testclasses.Tacos;
|
30 | 32 |
|
31 | 33 | import java.util.ArrayList;
|
| 34 | +import java.util.Arrays; |
32 | 35 | import java.util.ConcurrentModificationException;
|
33 | 36 | import java.util.HashSet;
|
34 | 37 | import java.util.Set;
|
35 | 38 |
|
| 39 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 40 | +import static org.hamcrest.MatcherAssert.assertThat; |
36 | 41 | import static org.junit.Assert.assertEquals;
|
| 42 | +import static org.junit.Assert.assertThrows; |
37 | 43 | import static org.junit.Assert.assertTrue;
|
38 | 44 | import static org.junit.Assert.fail;
|
39 | 45 | import static spoon.testing.utils.ModelUtils.build;
|
@@ -133,4 +139,45 @@ public void testGetAllMethodsAdaptingType() {
|
133 | 139 |
|
134 | 140 | assertTrue(compareFound);
|
135 | 141 | }
|
| 142 | + |
| 143 | + @Test |
| 144 | + public void test_addParameterAt_addsParameterToSpecifiedPosition() { |
| 145 | + // contract: the parameter should be added at the specified position |
| 146 | + Factory factory = new Launcher().getFactory(); |
| 147 | + |
| 148 | + CtMethod<?> method = factory.createMethod(); |
| 149 | + |
| 150 | + CtParameter<String> first = factory.createParameter(); |
| 151 | + CtTypeReference<String> firstType = factory.Type().stringType(); |
| 152 | + first.setSimpleName("x"); |
| 153 | + first.setType(firstType); |
| 154 | + |
| 155 | + CtParameter<Integer> second = factory.createParameter(); |
| 156 | + CtTypeReference<Integer> secondType = factory.Type().integerType(); |
| 157 | + second.setSimpleName("y"); |
| 158 | + second.setType(secondType); |
| 159 | + |
| 160 | + CtParameter<Boolean> third = factory.createParameter(); |
| 161 | + CtTypeReference<Boolean> thirdType = factory.Type().booleanType(); |
| 162 | + third.setSimpleName("z"); |
| 163 | + third.setType(thirdType); |
| 164 | + |
| 165 | + method.addParameterAt(0, second); |
| 166 | + method.addParameterAt(1, third); |
| 167 | + method.addParameterAt(0, first); |
| 168 | + |
| 169 | + assertThat(method.getParameters(), equalTo(Arrays.asList(first, second, third))); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void test_addParameterAt_throwsOutOfBoundsException_whenPositionIsOutOfBounds() { |
| 174 | + // contract: `addParameterAt` should throw an out of bounds exception when the specified position is out of |
| 175 | + // bounds of the parameter collection |
| 176 | + Factory factory = new Launcher().getFactory(); |
| 177 | + CtMethod<?> method = factory.createMethod(); |
| 178 | + CtParameter<?> paramater = factory.createParameter(); |
| 179 | + |
| 180 | + assertThrows(IndexOutOfBoundsException.class, |
| 181 | + () -> method.addParameterAt(2, paramater)); |
| 182 | + } |
136 | 183 | }
|
0 commit comments