-
Notifications
You must be signed in to change notification settings - Fork 3
/
CompletionTests.java
280 lines (249 loc) · 9.93 KB
/
CompletionTests.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*******************************************************************************
* Copyright (c) 2017, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.tests.completion;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.acceleo.Module;
import org.eclipse.acceleo.aql.AcceleoUtil;
import org.eclipse.acceleo.aql.completion.AcceleoCompletor;
import org.eclipse.acceleo.aql.completion.proposals.AcceleoCompletionProposal;
import org.eclipse.acceleo.aql.evaluation.AcceleoEvaluator;
import org.eclipse.acceleo.aql.parser.AcceleoAstResult;
import org.eclipse.acceleo.aql.parser.AcceleoParser;
import org.eclipse.acceleo.aql.parser.ModuleLoader;
import org.eclipse.acceleo.query.AQLUtils;
import org.eclipse.acceleo.query.runtime.impl.namespace.ClassLoaderQualifiedNameResolver;
import org.eclipse.acceleo.query.runtime.impl.namespace.JavaLoader;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameQueryEnvironment;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameResolver;
import org.eclipse.acceleo.tests.utils.AbstractLanguageTestSuite;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/**
* Test the Acceleo AQL completion.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
@RunWith(Parameterized.class)
public class CompletionTests {
/**
* The test {@link Pattern}.
*/
private static final Pattern TEST_PATTERN = Pattern.compile("<<([a-zA-Z0-9_]+)>>");
/**
* The root folder.
*/
private static final String ROOT = "resources" + File.separator + "completion";
/**
* The module name.
*/
private static final String MODULE_NAME = "completion";
/**
* The root folder.
*/
private static final String MODULE = "resources" + File.separator + MODULE_NAME + File.separator
+ MODULE_NAME + ".mtl";
/**
* The test name.
*/
private final String testName;
/**
* The source module to complete.
*/
private final String source;
/**
* The position for the completion.
*/
private final int position;
/**
* Constructor.
*
* @param testName
* the test name
* @param source
* the module source
* @param position
* the completion position
*/
public CompletionTests(String testName, String source, Integer position) {
this.testName = testName;
this.source = source;
this.position = position;
}
/**
* Tests the completion.
*
* @throws FileNotFoundException
* if expected completion file can't be found
* @throws IOException
* if the expected file can't be read or the actual file can't be written
*/
@Test
public void completion() throws FileNotFoundException, IOException {
final AcceleoCompletor completor = new AcceleoCompletor("\n");
final IQualifiedNameResolver resolver = new ClassLoaderQualifiedNameResolver(getClass()
.getClassLoader(), AcceleoParser.QUALIFIER_SEPARATOR);
// TODO get options form ??? or list all possible options ?
// don't add any options ?
final Map<String, String> options = new LinkedHashMap<>();
final ArrayList<Exception> exceptions = new ArrayList<>();
// the ResourceSet will not be used
final ResourceSet resourceSetForModels = AQLUtils.createResourceSetForModels(exceptions, resolver,
new ResourceSetImpl(), options);
// TODO report exceptions
final IQualifiedNameQueryEnvironment queryEnvironment = AcceleoUtil.newAcceleoQueryEnvironment(
options, resolver, resourceSetForModels, true);
try {
final String newLine = options.getOrDefault(AcceleoUtil.NEW_LINE_OPTION, System.lineSeparator());
final AcceleoEvaluator evaluator = new AcceleoEvaluator(queryEnvironment.getLookupEngine(),
newLine);
final AcceleoParser parser = new AcceleoParser();
resolver.addLoader(new ModuleLoader(parser, evaluator));
resolver.addLoader(new JavaLoader(AcceleoParser.QUALIFIER_SEPARATOR, true));
final AcceleoAstResult parsingResult = parser.parse(source, "org::eclipse::acceleo::tests::");
final Module module = parsingResult.getModule();
resolver.register("org::eclipse::acceleo::tests::" + MODULE_NAME, module);
final List<AcceleoCompletionProposal> completionProposals = completor.getProposals(
queryEnvironment, MODULE_NAME, source, position);
final String actualCompletion = serialize(completionProposals);
final File expectedCompletionFile = getExpectedCompletionFile();
if (!expectedCompletionFile.exists()) {
final File actualCompletionFile = getActualCompletionFile();
if (!actualCompletionFile.exists()) {
actualCompletionFile.createNewFile();
}
try (OutputStream stream = new FileOutputStream(actualCompletionFile)) {
AbstractLanguageTestSuite.setContent(stream, AbstractLanguageTestSuite.UTF_8,
actualCompletion);
}
fail("file doesn't exist.");
}
try (InputStream stream = new FileInputStream(expectedCompletionFile)) {
String expectedCompletion = AcceleoUtil.getContent(stream, AbstractLanguageTestSuite.UTF_8);
assertEquals(getPortableString(expectedCompletion), getPortableString(actualCompletion));
}
} finally {
AQLUtils.cleanResourceSetForModels(resolver, resourceSetForModels);
AcceleoUtil.cleanServices(queryEnvironment, resourceSetForModels);
}
}
/**
* Gets the portable version of the given {@link String}.
*
* @param textContent
* the text content
* @return the portable version of the given {@link String}
*/
private String getPortableString(String textContent) {
String res;
res = textContent.replaceAll("@author [a-zA-Z0-9-_ ]+", "@author ..."); // remove author
return res;
}
/**
* Serializes the given {@link List} of {@link AcceleoCompletionProposal}.
*
* @param proposals
* the {@link List} of {@link AcceleoCompletionProposal}
* @return the serialized {@link List} of {@link AcceleoCompletionProposal}
*/
private String serialize(List<AcceleoCompletionProposal> proposals) {
final StringBuilder builder = new StringBuilder();
for (AcceleoCompletionProposal proposal : proposals) {
builder.append("* Label:" + AbstractLanguageTestSuite.DEFAULT_END_OF_LINE_CHARACTER);
builder.append(proposal.getLabel());
builder.append(AbstractLanguageTestSuite.DEFAULT_END_OF_LINE_CHARACTER);
builder.append("* Description:" + AbstractLanguageTestSuite.DEFAULT_END_OF_LINE_CHARACTER);
builder.append(proposal.getDescription());
builder.append(AbstractLanguageTestSuite.DEFAULT_END_OF_LINE_CHARACTER);
builder.append("* Text:" + AbstractLanguageTestSuite.DEFAULT_END_OF_LINE_CHARACTER);
builder.append(proposal.getText());
builder.append(AbstractLanguageTestSuite.DEFAULT_END_OF_LINE_CHARACTER);
builder.append("* Type (optional):" + AbstractLanguageTestSuite.DEFAULT_END_OF_LINE_CHARACTER);
String typeAsString = "null";
if (proposal.getAcceleoType() != null) {
typeAsString = EcoreUtil.getURI(proposal.getAcceleoType()).toString();
}
builder.append(typeAsString);
builder.append(AbstractLanguageTestSuite.DEFAULT_END_OF_LINE_CHARACTER);
builder.append(AbstractLanguageTestSuite.DEFAULT_END_OF_LINE_CHARACTER);
}
return builder.toString().replaceAll("(\\r\\n)|\\r|\\n", Character.toString(
AbstractLanguageTestSuite.DEFAULT_END_OF_LINE_CHARACTER));
}
/**
* Gets the expected completion file.
*
* @return the expected completion file
*/
protected File getExpectedCompletionFile() {
return new File(ROOT + File.separator + testName + "-expected-completion.txt");
}
/**
* Gets the actual completion file.
*
* @return the actual completion file
*/
protected File getActualCompletionFile() {
return new File(ROOT + File.separator + testName + "-actual-completion.txt");
}
/**
* Gets the {@link List} of {testName, source, position} tuples.
*
* @return the {@link List} of {testName, source, position} tuples
* @throws IOException
* if the module can't be read
* @throws FileNotFoundException
* if the module can't be found
*/
@Parameters(name = "{0}")
public static Collection<Object[]> retrieveTests() throws FileNotFoundException, IOException {
final List<Object[]> res = new ArrayList<Object[]>();
final Map<String, Integer> testToPosition = new TreeMap<String, Integer>();
final StringBuilder builder = new StringBuilder();
try (InputStream stream = new FileInputStream(MODULE)) {
String content = AcceleoUtil.getContent(stream, AbstractLanguageTestSuite.UTF_8);
final Matcher matcher = TEST_PATTERN.matcher(content);
int lastMatchEnd = 0;
while (matcher.find()) {
builder.append(content.substring(lastMatchEnd, matcher.start()));
lastMatchEnd = matcher.end();
testToPosition.put(matcher.group(1), builder.length());
}
builder.append(content.substring(lastMatchEnd, content.length()));
}
final String source = builder.toString();
for (Entry<String, Integer> entry : testToPosition.entrySet()) {
res.add(new Object[] {entry.getKey(), source, entry.getValue() });
}
return res;
}
}