|
25 | 25 | import spoon.reflect.declaration.CtClass;
|
26 | 26 | import spoon.reflect.declaration.CtExecutable;
|
27 | 27 | import spoon.reflect.declaration.CtMethod;
|
| 28 | +import spoon.reflect.declaration.CtType; |
28 | 29 | import spoon.reflect.factory.Factory;
|
29 | 30 | import spoon.reflect.reference.CtExecutableReference;
|
30 | 31 | import spoon.reflect.visitor.filter.AbstractFilter;
|
31 | 32 | import spoon.reflect.visitor.filter.TypeFilter;
|
32 | 33 | import spoon.test.invocations.testclasses.Bar;
|
33 | 34 | import spoon.test.invocations.testclasses.Foo;
|
34 | 35 |
|
| 36 | +import java.util.Collections; |
35 | 37 | import java.util.List;
|
36 | 38 | import java.util.stream.Collectors;
|
37 | 39 |
|
| 40 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 41 | +import static org.hamcrest.MatcherAssert.assertThat; |
38 | 42 | import static org.junit.Assert.assertEquals;
|
39 | 43 | import static org.junit.Assert.assertNull;
|
40 | 44 | import static org.junit.Assert.assertNotNull;
|
@@ -97,4 +101,35 @@ public void testIssue1753() {
|
97 | 101 | final CtExecutable exe = executables.get(0);
|
98 | 102 | assertNotNull(exe.getReference().getDeclaration());
|
99 | 103 | }
|
| 104 | + |
| 105 | + @Test |
| 106 | + public void test_addArgumentAt_addsArgumentAtSpecifiedPosition() { |
| 107 | + // contract: addArgumentAt should add arguments to the specified position. |
| 108 | + |
| 109 | + // arrange |
| 110 | + Factory factory = new Launcher().getFactory(); |
| 111 | + factory.getEnvironment().setAutoImports(true);; |
| 112 | + CtType<?> collections = factory.Type().get(Collections.class); |
| 113 | + CtInvocation<?> addAllInv = factory.createInvocation( |
| 114 | + factory.createTypeAccess(collections.getReference()), |
| 115 | + collections.getMethodsByName("addAll").get(0).getReference()); |
| 116 | + |
| 117 | + // act |
| 118 | + // want to add the arguments new ArrayList<Integer>, 4, 99, 7, -2 s.t. they end up in that |
| 119 | + // order, but we do it haphazardly with addArgumentAt. |
| 120 | + |
| 121 | + // AL |
| 122 | + addAllInv.addArgumentAt(0, factory.createCodeSnippetExpression("new java.util.ArrayList<Integer>()").compile()) |
| 123 | + // AL, 99 |
| 124 | + .addArgumentAt(1, factory.createLiteral(99)) |
| 125 | + // AL, 99, -2 |
| 126 | + .addArgumentAt(2, factory.createLiteral(-2)) |
| 127 | + // AL, 4, 99, -2 |
| 128 | + .addArgumentAt(1, factory.createLiteral(4)) |
| 129 | + // AL, 4, 99, 7, -2 |
| 130 | + .addArgumentAt(3, factory.createLiteral(7)); |
| 131 | + |
| 132 | + // assert |
| 133 | + assertThat(addAllInv.toString(), equalTo("Collections.addAll(new ArrayList<Integer>(), 4, 99, 7, -2)")); |
| 134 | + } |
100 | 135 | }
|
0 commit comments