View Javadoc
1   package net.sourceforge.jenesis4java.impl;
2   
3   /*
4    * #%L
5    * Jenesis 4 Java Code Generator
6    * %%
7    * Copyright (C) 2000 - 2016 jenesis4java
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  import net.sourceforge.jenesis4java.Expression;
26  import net.sourceforge.jenesis4java.Invoke;
27  import net.sourceforge.jenesis4java.VirtualMachine;
28  import net.sourceforge.jenesis4java.util.InstanceReplacingVisitor;
29  import net.sourceforge.jenesis4java.util.WithVmAndMethod;
30  import org.junit.Before;
31  import org.junit.Test;
32  import org.junit.experimental.runners.Enclosed;
33  import org.junit.runner.RunWith;
34  
35  import java.util.Arrays;
36  import java.util.Collections;
37  
38  import static java.util.Collections.singletonList;
39  import static org.junit.Assert.assertEquals;
40  import static org.junit.Assert.assertSame;
41  
42  @RunWith(Enclosed.class)
43  public class InvokeTest {
44  
45      public static class BasicSyntax extends WithVmAndMethod {
46  
47          @Test
48          public void withoutParameters() {
49              assertEquals("method()", vm.newInvoke("method").toString());
50          }
51  
52          @Test
53          public void withOneParameter() {
54              Invoke invoke = vm.newInvoke("method");
55              invoke.addVariableArg("param");
56              assertEquals("method(param)", invoke.toString());
57          }
58  
59          @Test
60          public void withMultipleParameters() {
61              Invoke invoke = vm.newInvoke("method");
62              invoke.addVariableArg("first");
63              invoke.addVariableArg("second");
64              assertEquals("method(first, second)", invoke.toString());
65          }
66      }
67  
68      public static class ParameterShortcuts {
69  
70          private Invoke invoke;
71  
72          @Before
73          public void setUp() {
74              this.invoke = VirtualMachine.getVirtualMachine().newInvoke("method");
75          }
76  
77          @Test
78          public void booleanParam() {
79              Invoke returned = invoke.addArg(true);
80              assertEquals("method(true)", invoke.toString());
81              assertSame(invoke, returned);
82          }
83  
84          @Test
85          public void intParam() {
86              Invoke returned = invoke.addArg(123);
87              assertEquals("method(123)", invoke.toString());
88              assertSame(invoke, returned);
89          }
90  
91          @Test
92          public void longParam() {
93              Invoke returned = invoke.addArg(123L);
94              assertEquals("method(123L)", invoke.toString());
95              assertSame(invoke, returned);
96          }
97  
98          @Test
99          public void doubleParam() {
100             Invoke returned = invoke.addArg(123.4d);
101             assertEquals("method(123.4D)", invoke.toString());
102             assertSame(invoke, returned);
103         }
104 
105         @Test
106         public void floatParam() {
107             Invoke returned = invoke.addArg(123.0f);
108             assertEquals("method(123.0F)", invoke.toString());
109             assertSame(invoke, returned);
110         }
111 
112         @Test
113         public void stringParam() {
114             Invoke returned = invoke.addArg("string");
115             assertEquals("method(\"string\")", invoke.toString());
116             assertSame(invoke, returned);
117         }
118 
119         @Test
120         public void variableParam() {
121             Invoke returned = invoke.addVariableArg("param");
122             assertEquals("method(param)", invoke.toString());
123             assertSame(invoke, returned);
124         }
125     }
126 
127     public static class ArgumentOrder {
128 
129         private VirtualMachine vm;
130 
131         private Invoke invoke;
132 
133         @Before
134         public void setUp() {
135             this.vm = VirtualMachine.getVirtualMachine();
136             this.invoke = vm.newInvoke("method");
137         }
138 
139         @Test
140         public void getNoArguments() {
141             assertEquals(Collections.emptyList(), invoke.getArgs());
142         }
143 
144         @Test
145         public void getMultipleArgs() {
146             Expression arg1 = vm.newInt(1);
147             Expression arg2 = vm.newInt(2);
148             invoke.addArg(arg1).addArg(arg2);
149             assertEquals(Arrays.asList(arg1, arg2), invoke.getArgs());
150         }
151 
152         @Test(expected = IllegalArgumentException.class)
153         public void removeArgIllegalNumber() {
154             invoke.removeArg(-1);
155         }
156 
157         @Test
158         public void removeArg() {
159             Expression arg1 = vm.newInt(1);
160             Expression arg2 = vm.newInt(2);
161             invoke.addArg(arg1).addArg(arg2);
162             invoke.removeArg(0);
163             assertEquals(singletonList(arg2), invoke.getArgs());
164         }
165     }
166 
167     public static class Accessor extends WithVmAndMethod {
168 
169         @Test
170         public void setAndGetQualifier() {
171             Invoke invoke = vm.newInvoke("method");
172             invoke.setQualifier("qualifier");
173             assertEquals("qualifier", invoke.getQualifier());
174 
175             // TODO: this is somewhat inconsistent
176             // why distinguish between String and Expression?
177             assertEquals(null, invoke.getQualExpression());
178         }
179 
180         @Test
181         public void getQualifyingExpression() {
182             Expression qualifier = vm.newString("qualifier");
183             Invoke invoke = vm.newInvoke(qualifier, "method");
184             assertEquals(qualifier, invoke.getQualExpression());
185 
186             // TODO: this is somewhat inconsistent
187             // why not return a string version of the qualifier here?
188             assertEquals(null, invoke.getQualifier());
189         }
190 
191     }
192 
193     public static class Visitor extends WithVmAndMethod {
194 
195         @Test
196         public void replaceArgument() {
197             Invoke invoke = vm.newInvoke("method");
198 
199             Expression arg1 = vm.newInt(1);
200             invoke.addArg(arg1);
201 
202             Expression arg2 = vm.newInt(2);
203             invoke.visit(new InstanceReplacingVisitor(arg1, arg2));
204 
205             assertEquals(singletonList(arg2), invoke.getArgs());
206         }
207 
208         @Test
209         public void replaceQualifierExpression() {
210             Expression qualifier = vm.newString("qualifier");
211             Invoke invoke = vm.newInvoke(qualifier, "method");
212 
213             Expression qualifierReplacement = vm.newString("anotherQualifier");
214             invoke.visit(new InstanceReplacingVisitor(qualifier, qualifierReplacement));
215 
216             assertEquals(qualifierReplacement, invoke.getQualExpression());
217         }
218 
219     }
220 }