-
Notifications
You must be signed in to change notification settings - Fork 316
/
ExpressionScopeTest.java
193 lines (173 loc) · 8.51 KB
/
ExpressionScopeTest.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
/**
* Copyright (c) 2014, 2022 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.typesystem;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.XtextRunner;
import org.eclipse.xtext.xbase.XBlockExpression;
import org.eclipse.xtext.xbase.XExpression;
import org.eclipse.xtext.xbase.XIfExpression;
import org.eclipse.xtext.xbase.XVariableDeclaration;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.tests.AbstractXbaseTestCase;
import org.eclipse.xtext.xbase.tests.XbaseInjectorProviderWithScopeTracking;
import org.eclipse.xtext.xbase.typesystem.IBatchTypeResolver;
import org.eclipse.xtext.xbase.typesystem.IExpressionScope;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
/**
* @author Sebastian Zarnekow - Initial contribution and API
*/
@InjectWith(XbaseInjectorProviderWithScopeTracking.class)
@RunWith(XtextRunner.class)
public class ExpressionScopeTest extends AbstractXbaseTestCase {
@Inject
private IBatchTypeResolver batchTypeResolver;
protected void contains(IExpressionScope scope, String name) {
assertContains(scope.getFeatureScope(), QualifiedName.create(name));
}
protected void assertContains(IScope scope, QualifiedName name) {
Iterable<IEObjectDescription> elements = scope.getAllElements();
String toString = elements.toString();
assertNotNull(toString, scope.getSingleElement(name));
assertFalse(toString, Iterables.isEmpty(scope.getElements(name)));
assertTrue(toString, IterableExtensions.exists(elements, it -> Objects.equal(it.getName(), name)));
}
protected void containsNot(IExpressionScope scope, String name) {
assertContainsNot(scope.getFeatureScope(), QualifiedName.create(name));
}
protected void assertContainsNot(IScope scope, QualifiedName name) {
Iterable<IEObjectDescription> elements = scope.getAllElements();
String toString = elements.toString();
assertNull(toString, scope.getSingleElement(name));
assertTrue(toString, Iterables.isEmpty(scope.getElements(name)));
assertFalse(toString, IterableExtensions.exists(elements, it -> Objects.equal(it.getName(), name)));
}
@Test
public void testGetExpressionScope_01() throws Exception {
XBlockExpression block = (XBlockExpression) expression("{ var x = 1 }", false);
IExpressionScope expressionScope = batchTypeResolver.resolveTypes(block).getExpressionScope(block, IExpressionScope.Anchor.AFTER);
containsNot(expressionScope, "x");
}
@Test
public void testGetExpressionScope_02() throws Exception {
XBlockExpression block = (XBlockExpression) expression("{ var x = 1 }", false);
IExpressionScope expressionScope = batchTypeResolver.resolveTypes(block).getExpressionScope(block, IExpressionScope.Anchor.BEFORE);
containsNot(expressionScope, "x");
}
@Test
public void testGetExpressionScope_03() throws Exception {
XExpression varDecl = Iterables.getFirst(((XBlockExpression) expression("{ var x = 1 }", false)).getExpressions(), null);
IExpressionScope expressionScope = batchTypeResolver.resolveTypes(varDecl).getExpressionScope(varDecl,
IExpressionScope.Anchor.AFTER);
contains(expressionScope, "x");
}
@Test
public void testGetExpressionScope_04() throws Exception {
XExpression varDecl = Iterables.getFirst(((XBlockExpression) expression("{ var x = 1 }", false)).getExpressions(), null);
IExpressionScope expressionScope = batchTypeResolver.resolveTypes(varDecl).getExpressionScope(varDecl,
IExpressionScope.Anchor.BEFORE);
containsNot(expressionScope, "x");
}
@Test
public void testMemberOnIt_01() throws Exception {
XExpression varDecl = Iterables.getFirst(((XBlockExpression) expression("{ var it = \"\" }", false)).getExpressions(), null);
IExpressionScope expressionScope = batchTypeResolver.resolveTypes(varDecl).getExpressionScope(varDecl,
IExpressionScope.Anchor.AFTER);
contains(expressionScope, "charAt");
contains(expressionScope, "it");
contains(expressionScope, "operator_lessThan");
}
@Test
public void testMemberOnIt_02() throws Exception {
XExpression varDecl = Iterables.getFirst(((XBlockExpression) expression("{ var it = \"\" }", false)).getExpressions(), null);
IExpressionScope expressionScope = batchTypeResolver.resolveTypes(varDecl).getExpressionScope(varDecl,
IExpressionScope.Anchor.BEFORE);
containsNot(expressionScope, "charAt");
containsNot(expressionScope, "it");
containsNot(expressionScope, "operator_lessThan");
}
@Test
public void testMemberOnIt_03() throws Exception {
XVariableDeclaration first = (XVariableDeclaration) Iterables
.getFirst(((XBlockExpression) expression("{ var (int)=>int it = [] }", false)).getExpressions(), null);
Assert.assertNotNull(first);
XExpression varInit = first.getRight();
IExpressionScope expressionScope = batchTypeResolver.resolveTypes(varInit).getExpressionScope(varInit,
IExpressionScope.Anchor.BEFORE);
contains(expressionScope, "it");
}
@Test
public void testMemberOnIt_04() throws Exception {
XVariableDeclaration first = (XVariableDeclaration) Iterables
.getFirst(((XBlockExpression) expression("{ var it = [] }", false)).getExpressions(), null);
Assert.assertNotNull(first);
XExpression varInit = first.getRight();
IExpressionScope expressionScope = batchTypeResolver.resolveTypes(varInit).getExpressionScope(varInit,
IExpressionScope.Anchor.BEFORE);
containsNot(expressionScope, "it");
}
@Test
public void testMemberOnIt_05() throws Exception {
XVariableDeclaration first = (XVariableDeclaration) Iterables
.getFirst(((XBlockExpression) expression("{ var (int)=>int it = null }", false)).getExpressions(), null);
Assert.assertNotNull(first);
XExpression varInit = first.getRight();
IExpressionScope expressionScope = batchTypeResolver.resolveTypes(varInit).getExpressionScope(varInit,
IExpressionScope.Anchor.BEFORE);
containsNot(expressionScope, "it");
}
@Test
public void testDefaultStaticMethods_01() throws Exception {
XExpression varDecl = Iterables.getFirst(((XBlockExpression) expression("{ var it = \"\" }", false)).getExpressions(), null);
IExpressionScope expressionScope = batchTypeResolver.resolveTypes(varDecl).getExpressionScope(varDecl,
IExpressionScope.Anchor.BEFORE);
contains(expressionScope, "newArrayList");
}
@Test
public void testReassignedType_01() throws Exception {
XIfExpression ifExpr = (XIfExpression) IterableExtensions
.lastOrNull(((XBlockExpression) expression("{ var it = new Object() if (it instanceof String) {} }", false)).getExpressions());
XBlockExpression block = (XBlockExpression) ifExpr.getThen();
IExpressionScope expressionScope = batchTypeResolver.resolveTypes(block).getExpressionScope(block, IExpressionScope.Anchor.BEFORE);
contains(expressionScope, "charAt");
contains(expressionScope, "it");
contains(expressionScope, "operator_lessThan");
}
@Test
public void testReassignedType_02() throws Exception {
XIfExpression ifExpr = (XIfExpression) IterableExtensions
.lastOrNull(((XBlockExpression) expression("{ var it = new Object() if (it instanceof String) { it = new Object() } }", false))
.getExpressions());
XBlockExpression block = (XBlockExpression) ifExpr.getThen();
IExpressionScope expressionScope = batchTypeResolver.resolveTypes(block).getExpressionScope(block, IExpressionScope.Anchor.BEFORE);
contains(expressionScope, "charAt");
contains(expressionScope, "it");
contains(expressionScope, "operator_lessThan");
}
@Test
public void testReassignedType_03() throws Exception {
XIfExpression ifExpr = (XIfExpression) IterableExtensions
.lastOrNull(((XBlockExpression) expression("{ var it = new Object() if (it instanceof String) { it = new Object() } }", false))
.getExpressions());
XBlockExpression block = (XBlockExpression) ifExpr.getThen();
XExpression assignment = Iterables.getFirst(block.getExpressions(), null);
IExpressionScope expressionScope = batchTypeResolver.resolveTypes(assignment).getExpressionScope(assignment,
IExpressionScope.Anchor.AFTER);
containsNot(expressionScope, "charAt");
contains(expressionScope, "it");
containsNot(expressionScope, "operator_lessThan");
}
}