-
Notifications
You must be signed in to change notification settings - Fork 317
/
AbstractXtendTestCase.java
191 lines (167 loc) · 7.16 KB
/
AbstractXtendTestCase.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
/*******************************************************************************
* Copyright (c) 2012, 2021 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.xtend.core.tests;
import static com.google.common.collect.Lists.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtend.core.tests.util.TemporaryFolder;
import org.eclipse.xtend.core.xtend.XtendAnnotationType;
import org.eclipse.xtend.core.xtend.XtendClass;
import org.eclipse.xtend.core.xtend.XtendConstructor;
import org.eclipse.xtend.core.xtend.XtendEnum;
import org.eclipse.xtend.core.xtend.XtendField;
import org.eclipse.xtend.core.xtend.XtendFile;
import org.eclipse.xtend.core.xtend.XtendFunction;
import org.eclipse.xtend.core.xtend.XtendInterface;
import org.eclipse.xtext.diagnostics.Severity;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.XtextRunner;
import org.eclipse.xtext.util.CancelIndicator;
import org.eclipse.xtext.util.LazyStringInputStream;
import org.eclipse.xtext.util.StringInputStream;
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.Issue;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.runner.RunWith;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.Provider;
/**
* Abstract base for Xtend tests.
*/
@RunWith(XtextRunner.class)
@InjectWith(RuntimeInjectorProvider.class)
public abstract class AbstractXtendTestCase extends Assert {
@Rule
@Inject public TemporaryFolder temporaryFolder;
@Inject
private Provider<XtextResourceSet> resourceSetProvider;
/**
* To build a function or constructor for tests
*/
protected static final String TEMPLATE_CLASS = "class Foo { %s }";
/**
* Use it to subtract from the index position of a source element of a function or constructor
*/
protected static final int TEMPLATE_CLASS_SIZE = TEMPLATE_CLASS.length() - 2; // 2 is the length of placeholder %s
protected XtendClass clazz(String string) throws Exception {
return (XtendClass) file(string).getXtendTypes().get(0);
}
protected XtendFile file(String string) throws Exception {
return file(string, false);
}
protected XtendFile file(String string, boolean validate) throws Exception {
return file(string, validate, true);
}
protected XtendFile file(String string, boolean validate, boolean shouldBeSyntacticallyValid) throws Exception {
XtextResourceSet set = getResourceSet();
String fileName = getFileName(string);
Resource resource = set.createResource(URI.createURI(fileName + ".xtend"));
resource.load(new LazyStringInputStream(string, StandardCharsets.ISO_8859_1.name()), null);
if (shouldBeSyntacticallyValid) {
assertEquals(resource.getErrors().toString(), 0, resource.getErrors().size());
}
if (validate) {
List<Issue> issues = Lists.newArrayList(Iterables.filter(((XtextResource) resource).getResourceServiceProvider().getResourceValidator()
.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl), new Predicate<Issue>() {
@Override
public boolean apply(Issue issue) {
return issue.getSeverity() == Severity.ERROR;
}
}));
assertTrue("Resource contained errors : " + issues.toString(), issues.isEmpty());
}
XtendFile file = (XtendFile) resource.getContents().get(0);
return file;
}
protected XtendFile fileWithErrors(String string) throws Exception {
XtextResourceSet set = getResourceSet();
String fileName = getFileName(string);
Resource resource = set.createResource(URI.createURI(fileName + ".xtend"));
resource.load(new StringInputStream(string), null);
assertTrue(resource.getErrors().toString(), resource.getErrors().size() > 0);
XtendFile file = (XtendFile) resource.getContents().get(0);
return file;
}
protected XtextResourceSet getResourceSet() {
XtextResourceSet set = resourceSetProvider.get();
set.setClasspathURIContext(getClass().getClassLoader());
return set;
}
protected Iterable<XtendFile> files(boolean validate, String ... contents) throws Exception {
XtextResourceSet set = getResourceSet();
List<XtendFile> result = newArrayList();
for (String string : contents) {
String fileName = getFileName(string);
Resource resource = set.createResource(URI.createURI(fileName + ".xtend"));
resource.load(new StringInputStream(string), null);
assertEquals(resource.getErrors().toString(), 0, resource.getErrors().size());
}
for (Resource resource: new ArrayList<Resource>(set.getResources())) {
XtendFile file = (XtendFile) resource.getContents().get(0);
result.add(file);
}
if (validate) {
for (XtendFile file : result) {
List<Issue> issues = ((XtextResource) file.eResource()).getResourceServiceProvider().getResourceValidator()
.validate(file.eResource(), CheckMode.ALL, CancelIndicator.NullImpl);
assertTrue("Resource contained errors : " + issues.toString(), issues.isEmpty());
}
}
return result;
}
protected String getFileName(String string) {
Matcher packMatcher = Pattern.compile("package (\\S+)").matcher(string);
Matcher classMatcher = Pattern.compile("class (\\w+)").matcher(string);
if (classMatcher.find()) {
String className = classMatcher.group(1);
String pathName = "";
if (packMatcher.find() && packMatcher.end(0) < classMatcher.start(0)) {
pathName = packMatcher.group(1).replace('.', '/') + "/";
}
return pathName + className;
}
return "Sample";
}
protected XtendAnnotationType annotationType(String string) throws Exception {
return (XtendAnnotationType) file(string).getXtendTypes().get(0);
}
protected XtendInterface interfaze(String string) throws Exception {
return (XtendInterface) file(string).getXtendTypes().get(0);
}
protected XtendEnum enumeration(String string) throws Exception {
return (XtendEnum) file(string).getXtendTypes().get(0);
}
protected XtendFunction function(String string) throws Exception {
XtendClass clazz = clazz(String.format(TEMPLATE_CLASS, string));
return (XtendFunction) clazz.getMembers().get(0);
}
protected XtendFunction abstractFunction(String string) throws Exception {
XtendClass clazz = clazz("abstract class Foo { " + string + "}");
return (XtendFunction) clazz.getMembers().get(0);
}
protected XtendConstructor constructor(String string) throws Exception {
XtendClass clazz = clazz("class Foo { " + string + "}");
return (XtendConstructor) clazz.getMembers().get(0);
}
protected XtendField field(String string) throws Exception {
XtendClass clazz = clazz("class Foo { " + string + "}");
return (XtendField) clazz.getMembers().get(0);
}
}