-
Notifications
You must be signed in to change notification settings - Fork 316
/
CompilationTestHelperTest.java
211 lines (200 loc) · 7 KB
/
CompilationTestHelperTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* Copyright (c) 2016, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xbase.tests.compiler;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.resource.FileExtensionProvider;
import org.eclipse.xtext.util.JavaVersion;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.compiler.GeneratorConfig;
import org.eclipse.xtext.xbase.compiler.GeneratorConfigProvider;
import org.eclipse.xtext.xbase.lib.Pair;
import org.eclipse.xtext.xbase.testing.CompilationTestHelper;
import org.eclipse.xtext.xbase.testing.CompilationTestHelper.Result;
import org.eclipse.xtext.xbase.testing.TemporaryFolder;
import org.eclipse.xtext.xbase.tests.jvmmodel.AbstractJvmModelTest;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import com.google.inject.Inject;
/**
* @author Lorenzo Bettini - Initial contribution and API
*/
public class CompilationTestHelperTest extends AbstractJvmModelTest {
@Rule
@Inject
public TemporaryFolder temporaryFolder;
@Inject
private CompilationTestHelper compilationTestHelper;
@Inject
private GeneratorConfigProvider generatorConfigProvider;
@Inject
private FileExtensionProvider extensionProvider;
@Test
public void testSwitchJavaVersion6() throws Exception {
compilationTestHelper.setJavaVersion(JavaVersion.JAVA6);
String source =
"{\n" +
" val s = \"\"\n" +
" switch (s) {\n" +
" case \"foo\": \"foo\"\n" +
" default: \"bar\"\n" +
" }\n" +
"}\n";
compilationTestHelper.compile(source, (CompilationTestHelper.Result it) -> {
String expectation =
"import java.util.Objects;\n" +
"\n" +
"@SuppressWarnings(\"all\")\n" +
"public class Test {\n" +
" public String doStuff(final String s) {\n" +
" String _xblockexpression = null;\n" +
" {\n" +
" final String s_1 = \"\";\n" +
" String _switchResult = null;\n" +
" boolean _matched = false;\n" +
" if (Objects.equals(s_1, \"foo\")) {\n" +
" _matched=true;\n" +
" _switchResult = \"foo\";\n" +
" }\n" +
" if (!_matched) {\n" +
" _switchResult = \"bar\";\n" +
" }\n" +
" _xblockexpression = _switchResult;\n" +
" }\n" +
" return _xblockexpression;\n" +
" }\n" +
"}\n";
Assert.assertEquals(expectation, Strings.toUnixLineSeparator(it.getSingleGeneratedCode()));
it.getCompiledClass();
});
}
@Test
public void testSwitchJavaVersion7() throws Exception {
compilationTestHelper.setJavaVersion(JavaVersion.JAVA7);
String source =
"{\n" +
" val s = \"\"\n" +
" switch (s) {\n" +
" case \"foo\": \"foo\"\n" +
" default: \"bar\"\n" +
" }\n" +
"}\n";
compilationTestHelper.compile(source, (CompilationTestHelper.Result it) -> {
String expectation =
"@SuppressWarnings(\"all\")\n" +
"public class Test {\n" +
" public String doStuff(final String s) {\n" +
" String _xblockexpression = null;\n" +
" {\n" +
" final String s_1 = \"\";\n" +
" String _switchResult = null;\n" +
" if (s_1 != null) {\n" +
" switch (s_1) {\n" +
" case \"foo\":\n" +
" _switchResult = \"foo\";\n" +
" break;\n" +
" default:\n" +
" _switchResult = \"bar\";\n" +
" break;\n" +
" }\n" +
" } else {\n" +
" _switchResult = \"bar\";\n" +
" }\n" +
" _xblockexpression = _switchResult;\n" +
" }\n" +
" return _xblockexpression;\n" +
" }\n" +
"}\n";
Assert.assertEquals(expectation, Strings.toUnixLineSeparator(it.getSingleGeneratedCode()));
it.getCompiledClass();
});
}
@Test
public void testLambdaJavaVersion6() throws Exception {
compilationTestHelper.setJavaVersion(JavaVersion.JAVA6);
String source =
"{\n" +
" val f = [ int i | i + 1 ]\n" +
"}\n";
compilationTestHelper.compile(source, (CompilationTestHelper.Result it) -> {
String expectation =
"import org.eclipse.xtext.xbase.lib.Functions.Function1;\n" +
"\n" +
"@SuppressWarnings(\"all\")\n" +
"public class Test {\n" +
" public String doStuff(final String s) {\n" +
" final Function1<Integer, Integer> _function = new Function1<Integer, Integer>() {\n" +
" @Override\n" +
" public Integer apply(final Integer i) {\n" +
" return Integer.valueOf((i + 1));\n" +
" }\n" +
" };\n" +
" final Function1<Integer, Integer> f = _function;\n" +
" return null;\n" +
" }\n" +
"}\n";
Assert.assertEquals(expectation, Strings.toUnixLineSeparator(it.getSingleGeneratedCode()));
it.getCompiledClass();
});
}
@Test
public void testLambdaJavaVersion8() throws Exception {
compilationTestHelper.setJavaVersion(JavaVersion.JAVA8);
String source =
"{\n" +
" val f = [ int i | i + 1 ]\n" +
"}\n";
compilationTestHelper.compile(source, (CompilationTestHelper.Result it) -> {
String expectation =
"import org.eclipse.xtext.xbase.lib.Functions.Function1;\n" +
"\n" +
"@SuppressWarnings(\"all\")\n" +
"public class Test {\n" +
" public String doStuff(final String s) {\n" +
" final Function1<Integer, Integer> _function = (Integer i) -> {\n" +
" return Integer.valueOf((i + 1));\n" +
" };\n" +
" final Function1<Integer, Integer> f = _function;\n" +
" return null;\n" +
" }\n" +
"}\n";
Assert.assertEquals(expectation, Strings.toUnixLineSeparator(it.getSingleGeneratedCode()));
it.getCompiledClass();
});
}
@Test
public void testCustomConfigIsNotLost() throws Exception {
compilationTestHelper.setJavaVersion(JavaVersion.JAVA8);
String model =
"{\n" +
" val f = [ int i | i + 1 ]\n" +
"}\n";
@SuppressWarnings("unchecked")
ResourceSet resourceSet = compilationTestHelper
.resourceSet(Pair.of("MyFile." + extensionProvider.getPrimaryFileExtension(), model));
GeneratorConfig generatorConfig = new GeneratorConfig();
generatorConfig.setGenerateSyntheticSuppressWarnings(false);
generatorConfigProvider.install(resourceSet, generatorConfig);
compilationTestHelper.compile(resourceSet, (Result it) -> {
String expectation =
"import org.eclipse.xtext.xbase.lib.Functions.Function1;\n" +
"\n" +
"public class Test {\n" +
" public String doStuff(final String s) {\n" +
" final Function1<Integer, Integer> _function = (Integer i) -> {\n" +
" return Integer.valueOf((i + 1));\n" +
" };\n" +
" final Function1<Integer, Integer> f = _function;\n" +
" return null;\n" +
" }\n" +
"}\n";
Assert.assertEquals(expectation, Strings.toUnixLineSeparator(it.getSingleGeneratedCode()));
});
}
}