View Javadoc
1   package net.sourceforge.jenesis4java.impl;
2   
3   /*
4    * #%L
5    * Jenesis 4 Java Code Generator
6    * %%
7    * Copyright (C) 2000 - 2015 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.Block;
26  import net.sourceforge.jenesis4java.Codeable;
27  import net.sourceforge.jenesis4java.ReplacingVisitor;
28  import net.sourceforge.jenesis4java.PrimitiveType;
29  import net.sourceforge.jenesis4java.Type;
30  import net.sourceforge.jenesis4java.util.CollectingVisitor;
31  import org.junit.Before;
32  import org.junit.Test;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  import static net.sourceforge.jenesis4java.util.TestHelper.*;
38  import static org.junit.Assert.*;
39  
40  public class MLambdaTest {
41  
42      private MLambda lambda;
43  
44      @Before
45      public void setup() {
46          lambda = new MLambda(vm());
47      }
48  
49      @Test
50      public void testAddOneTypeParameter() throws Exception {
51          lambda.setBody(vm().newInvoke("System.out", "println").addArg("Hallo"));
52          lambda.addParameter(vm().newType(Type.INT), "a");
53          assertEquals("(int a) -> System.out.println(\"Hallo\");", toCodeAsString(lambda));
54      }
55  
56      @Test
57      public void testAddOneClassParameter() throws Exception {
58          lambda.setBody(vm().newInvoke("System.out", "println").addArg("Hallo"));
59          lambda.addParameter(int.class, "a");
60          assertEquals("(int a) -> System.out.println(\"Hallo\");", toCodeAsString(lambda));
61      }
62  
63      @Test
64      public void testAddTwoTypeParameter() throws Exception {
65          lambda.setBody(vm().newInvoke("System.out", "println").addArg("Hallo"));
66          lambda.addParameter(vm().newType(Type.INT), "a").addParameter(vm().newType(Type.LONG), "b");
67          assertEquals("(int a, long b) -> System.out.println(\"Hallo\");", toCodeAsString(lambda));
68      }
69  
70      @Test
71      public void testAddOneParameterByName() throws Exception {
72          lambda.setBody(vm().newInvoke("System.out", "println").addArg("Hallo")).addParameter("a");
73          assertEquals("a -> System.out.println(\"Hallo\");", toCodeAsString(lambda));
74      }
75  
76      @Test
77      public void testAddTwoParametersByName() throws Exception {
78          lambda.setBody(vm().newInvoke("System.out", "println").addArg("Hallo"));
79          lambda.addParameter("a").addParameter("b");
80          assertEquals("(a, b) -> System.out.println(\"Hallo\");", toCodeAsString(lambda));
81      }
82  
83      @Test(expected = RuntimeException.class)
84      public void testMixParametersTypes() throws Exception {
85          lambda.addParameter("a").addParameter(int.class, "b");
86      }
87  
88      @Test(expected = RuntimeException.class)
89      public void testMixParametersTypes2() throws Exception {
90          lambda.addParameter(int.class, "a").addParameter("b");
91      }
92  
93      @Test
94      public void testSetBodyExpr1() throws Exception {
95          lambda.setBody(vm().newInvoke("System.out", "println").addArg("Hallo"));
96          assertEquals("() -> System.out.println(\"Hallo\");", toCodeAsString(lambda));
97      }
98  
99      @Test
100     public void testSetBodyExpr2() throws Exception {
101         lambda.setBody(vm().newInvoke("System.out", "println").addArg("Hello"));
102         assertEquals("() -> System.out.println(\"Hello\");", toCodeAsString(lambda));
103     }
104 
105     @Test
106     public void testEmptyBodyBlock() throws Exception {
107         lambda.newBodyBlock();
108         assertEquals("() -> {" + LS + //
109                 "};", toCodeAsString(lambda));
110     }
111 
112     @Test
113     public void testSimpleBodyBlock() throws Exception {
114         Block block = lambda.newBodyBlock();
115         block.newReturn().setExpression(vm().newInt(10));
116         assertEquals("() -> {" + LS + //
117                 "    return 10;" + LS + //
118                 "};", toCodeAsString(lambda));
119     }
120 
121     @Test
122     public void testVisitEmptyLambda() throws Exception {
123         List<Codeable> visited = new ArrayList<>();
124         ReplacingVisitor visitor = new CollectingVisitor(visited);
125 
126         lambda.visit(visitor);
127 
128         assertEquals(1, visited.size());
129         assertSame("expected null comment", null, visited.get(0));
130     }
131 
132     @Test
133     public void testVisitEmptyLambdaWithTypelessParameter() throws Exception {
134         List<Codeable> visited = new ArrayList<>();
135         ReplacingVisitor visitor = new CollectingVisitor(visited);
136 
137         lambda.addParameter("a");
138         lambda.visit(visitor);
139 
140         assertEquals(1, visited.size());
141         assertSame("expected null comment", null, visited.get(0));
142     }
143 
144     @Test
145     public void testVisitEmptyLambdaWithTypedParameter() throws Exception {
146         List<Codeable> visited = new ArrayList<>();
147         ReplacingVisitor visitor = new CollectingVisitor(visited);
148 
149         PrimitiveType intType = vm().newType(Type.INT);
150         lambda.addParameter(intType, "a");
151         lambda.visit(visitor);
152 
153         assertEquals(3, visited.size());
154         assertSame("expected null lambda comment", null, visited.get(0));
155         assertSame("expected null type comment", null, visited.get(1));
156         assertSame(intType, visited.get(2));
157     }
158 
159     @Test
160     public void testVisitSimpleLambda() throws Exception {
161         List<Codeable> visited = new ArrayList<>();
162         ReplacingVisitor visitor = new CollectingVisitor(visited);
163 
164         PrimitiveType intType = vm().newType(Type.INT);
165         lambda.addParameter(intType, "a");
166         Block block = lambda.newBodyBlock();
167         block.newReturn().setExpression(vm().newInt(10));
168 
169         lambda.visit(visitor);
170 
171         // TODO: this is wrong
172         assertEquals(3, visited.size());
173         assertSame("expected null lambda comment", null, visited.get(0));
174         assertSame("expected null type comment", null, visited.get(1));
175         assertSame(intType, visited.get(2));
176     }
177 }