Package: AstEvaluatorTest$1

AstEvaluatorTest$1

nameinstructionbranchcomplexitylinemethod
{...}
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2015 Obeo.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * Obeo - initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.query.ast.test;
12:
13: import com.google.common.collect.Maps;
14:
15: import java.util.ArrayList;
16: import java.util.Collection;
17: import java.util.Iterator;
18: import java.util.List;
19: import java.util.Map;
20: import java.util.Set;
21:
22: import org.eclipse.acceleo.query.ast.Call;
23: import org.eclipse.acceleo.query.ast.CallType;
24: import org.eclipse.acceleo.query.ast.Conditional;
25: import org.eclipse.acceleo.query.ast.Expression;
26: import org.eclipse.acceleo.query.ast.Lambda;
27: import org.eclipse.acceleo.query.ast.Let;
28: import org.eclipse.acceleo.query.ast.TypeLiteral;
29: import org.eclipse.acceleo.query.parser.AstBuilder;
30: import org.eclipse.acceleo.query.parser.AstBuilderListener;
31: import org.eclipse.acceleo.query.parser.AstEvaluator;
32: import org.eclipse.acceleo.query.runtime.CrossReferenceProvider;
33: import org.eclipse.acceleo.query.runtime.EvaluationResult;
34: import org.eclipse.acceleo.query.runtime.IQueryEnvironment;
35: import org.eclipse.acceleo.query.runtime.Query;
36: import org.eclipse.acceleo.query.runtime.impl.CrossReferencerToAQL;
37: import org.eclipse.acceleo.query.runtime.impl.EvaluationServices;
38: import org.eclipse.acceleo.query.runtime.impl.LambdaValue;
39: import org.eclipse.acceleo.query.runtime.impl.Nothing;
40: import org.eclipse.emf.common.notify.Notifier;
41: import org.eclipse.emf.common.util.Diagnostic;
42: import org.eclipse.emf.ecore.EObject;
43: import org.eclipse.emf.ecore.EcorePackage;
44: import org.eclipse.emf.ecore.resource.Resource;
45: import org.eclipse.emf.ecore.resource.ResourceSet;
46: import org.eclipse.emf.ecore.util.EcoreUtil;
47: import org.eclipse.emf.ecore.util.EcoreUtil.CrossReferencer;
48: import org.junit.Before;
49: import org.junit.Test;
50:
51: import static org.junit.Assert.assertEquals;
52: import static org.junit.Assert.assertFalse;
53: import static org.junit.Assert.assertTrue;
54:
55: public class AstEvaluatorTest extends AstBuilder {
56:
57:         private AstEvaluator evaluator;
58:
59:         /**
60:          * This will create the cross referencer that's to be used by the "eInverse" library. It will attempt to
61:          * create the cross referencer on the target's resourceSet. If it is null, we'll then attempt to create
62:          * the cross referencer on the target's resource. When the resource too is null, we'll create the cross
63:          * referencer on the target's root container.
64:          *
65:          * @param target
66:          * Target of the cross referencing.
67:          */
68:         private CrossReferenceProvider createEInverseCrossreferencer(EObject target) {
69:                 Resource res = null;
70:                 ResourceSet rs = null;
71:                 CrossReferencer crossReferencer;
72:                 if (target.eResource() != null) {
73:                         res = target.eResource();
74:                 }
75:                 if (res != null && res.getResourceSet() != null) {
76:                         rs = res.getResourceSet();
77:                 }
78:
79:                 if (rs != null) {
80:                         // Manually add the ecore.ecore resource in the list of cross
81:                         // referenced notifiers
82:                         final Resource ecoreResource = EcorePackage.eINSTANCE.getEClass().eResource();
83:                         final Collection<Notifier> notifiers = new ArrayList<Notifier>();
84:                         for (Resource crossReferenceResource : rs.getResources()) {
85:                                 if (!"emtl".equals(crossReferenceResource.getURI().fileExtension())) {
86:                                         notifiers.add(crossReferenceResource);
87:                                 }
88:                         }
89:                         notifiers.add(ecoreResource);
90:
91:                         crossReferencer = new CrossReferencer(notifiers) {
92:                                 /** Default SUID. */
93:                                 private static final long serialVersionUID = 1L;
94:
95:                                 // static initializer
96:                                 {
97:                                         crossReference();
98:                                         done();
99:                                 }
100:                         };
101:                 } else if (res != null) {
102:                         crossReferencer = new CrossReferencer(res) {
103:                                 /** Default SUID. */
104:                                 private static final long serialVersionUID = 1L;
105:
106:                                 // static initializer
107:                                 {
108:                                         crossReference();
109:                                         done();
110:                                 }
111:                         };
112:                 } else {
113:                         EObject targetObject = EcoreUtil.getRootContainer(target);
114:                         crossReferencer = new CrossReferencer(targetObject) {
115:                                 /** Default SUID. */
116:                                 private static final long serialVersionUID = 1L;
117:
118:                                 // static initializer
119:                                 {
120:                                         crossReference();
121:                                         done();
122:                                 }
123:                         };
124:                 }
125:                 return new CrossReferencerToAQL(crossReferencer);
126:         }
127:
128:         @Before
129:         public void setup() {
130:                 IQueryEnvironment environment = Query
131:                                 .newEnvironmentWithDefaultServices(createEInverseCrossreferencer(EcorePackage.eINSTANCE));
132:                 evaluator = new AstEvaluator(new EvaluationServices(environment));
133:         }
134:
135:         @Test
136:         public void testIntLiteral() {
137:                 Map<String, Object> varDefinitions = Maps.newHashMap();
138:                 assertOKResultEquals(Integer.valueOf(1), evaluator.eval(varDefinitions, integerLiteral(1)));
139:         }
140:
141:         @Test
142:         public void testRealLiteral() {
143:                 Map<String, Object> varDefinitions = Maps.newHashMap();
144:                 assertOKResultEquals(Double.valueOf(1d), evaluator.eval(varDefinitions, realLiteral(1.0)));
145:         }
146:
147:         @Test
148:         public void testBoolLiteral() {
149:                 Map<String, Object> varDefinitions = Maps.newHashMap();
150:                 assertOKResultEquals(Boolean.TRUE, evaluator.eval(varDefinitions, booleanLiteral(true)));
151:                 assertOKResultEquals(Boolean.FALSE, evaluator.eval(varDefinitions, booleanLiteral(false)));
152:         }
153:
154:         @Test
155:         public void testStringLiteral() {
156:                 Map<String, Object> varDefinitions = Maps.newHashMap();
157:                 assertOKResultEquals("john doe", evaluator.eval(varDefinitions, stringLiteral("john doe")));
158:         }
159:
160:         @Test
161:         public void testTypeLiteral() {
162:                 Map<String, Object> varDefinitions = Maps.newHashMap();
163:                 assertOKResultEquals(EcorePackage.Literals.ECLASS, evaluator.eval(varDefinitions,
164:                                 typeLiteral(EcorePackage.Literals.ECLASS)));
165:                 assertOKResultEquals(Integer.class, evaluator.eval(varDefinitions, typeLiteral(Integer.class)));
166:         }
167:
168:         @Test
169:         public void testFeatureAccess() {
170:                 Map<String, Object> varDefinitions = Maps.newHashMap();
171:                 varDefinitions.put("self", EcorePackage.Literals.ECLASS);
172:                 assertOKResultEquals("EClass", evaluator.eval(varDefinitions, callService(
173:                                 AstBuilderListener.FEATURE_ACCESS_SERVICE_NAME, varRef("self"), stringLiteral("name"))));
174:                 EvaluationResult result = evaluator.eval(varDefinitions, callService(
175:                                 AstBuilderListener.FEATURE_ACCESS_SERVICE_NAME, varRef("self"),
176:                                 stringLiteral(("eAllSuperTypes"))));
177:                 assertTrue(result.getResult() instanceof List);
178:                 assertEquals(Diagnostic.OK, result.getDiagnostic().getSeverity());
179:                 assertTrue(result.getDiagnostic().getChildren().isEmpty());
180:                 List<?> listResult = (List<?>)result.getResult();
181:                 assertEquals(3, listResult.size());
182:                 assertTrue(listResult.contains(EcorePackage.Literals.EMODEL_ELEMENT));
183:                 assertTrue(listResult.contains(EcorePackage.Literals.ENAMED_ELEMENT));
184:                 assertTrue(listResult.contains(EcorePackage.Literals.ECLASSIFIER));
185:         }
186:
187:         @Test
188:         public void testVarRef() {
189:                 Map<String, Object> varDefinitions = Maps.newHashMap();
190:                 varDefinitions.put("self", EcorePackage.Literals.ECLASS);
191:                 assertOKResultEquals(EcorePackage.Literals.ECLASS, evaluator.eval(varDefinitions, varRef("self")));
192:         }
193:
194:         @Test
195:         public void testCall() {
196:                 Map<String, Object> varDefinitions = Maps.newHashMap();
197:                 varDefinitions.put("self", EcorePackage.Literals.ECLASS);
198:                 final Call callService = callService("size", callService(
199:                                 AstBuilderListener.FEATURE_ACCESS_SERVICE_NAME, varRef("self"),
200:                                 stringLiteral("eAllSuperTypes")));
201:                 callService.setType(CallType.COLLECTIONCALL);
202:                 EvaluationResult result = evaluator.eval(varDefinitions, callService);
203:                 assertOKResultEquals(Integer.valueOf(3), result);
204:         }
205:
206:         @Test
207:         public void testLambda() {
208:                 Map<String, Object> varDefinitions = Maps.newHashMap();
209:                 varDefinitions.put("x", new Integer(1));
210:                 Lambda lambda = lambda(varRef("x"));
211:                 EvaluationResult value = evaluator.eval(varDefinitions, lambda);
212:                 assertTrue(value.getResult() instanceof LambdaValue);
213:                 assertEquals(Diagnostic.OK, value.getDiagnostic().getSeverity());
214:                 assertTrue(value.getDiagnostic().getChildren().isEmpty());
215:                 assertEquals(Integer.valueOf(1), ((LambdaValue)value.getResult()).eval(new Object[0]));
216:         }
217:
218:         @Test
219:         public void testNullLiteral() {
220:                 Map<String, Object> varDefinitions = Maps.newHashMap();
221:                 assertOKResultEquals(null, evaluator.eval(varDefinitions, nullLiteral()));
222:         }
223:
224:         @Test
225:         @SuppressWarnings("unchecked")
226:         public void testSetInExtensionLiteral() {
227:                 Map<String, Object> varDefinitions = Maps.newHashMap();
228:
229:                 varDefinitions.put("self", EcorePackage.Literals.ECLASS);
230:                 final List<Expression> values = new ArrayList<Expression>();
231:                 values.add(varRef("self"));
232:                 values.add(varRef("self"));
233:                 values.add(booleanLiteral(true));
234:                 values.add(booleanLiteral(false));
235:
236:                 final EvaluationResult result = evaluator.eval(varDefinitions, setInExtension(values));
237:                 assertTrue(result.getResult() instanceof Set);
238:                 assertEquals(Diagnostic.OK, result.getDiagnostic().getSeverity());
239:                 assertTrue(result.getDiagnostic().getChildren().isEmpty());
240:                 Set<Object> setResult = (Set<Object>)result.getResult();
241:                 assertEquals(3, setResult.size());
242:                 Iterator<Object> it = setResult.iterator();
243:                 assertEquals(EcorePackage.Literals.ECLASS, it.next());
244:                 assertEquals(Boolean.TRUE, it.next());
245:                 assertEquals(Boolean.FALSE, it.next());
246:                 assertFalse(it.hasNext());
247:         }
248:
249:         @Test
250:         @SuppressWarnings("unchecked")
251:         public void testSequenceInExtensionLiteral() {
252:                 Map<String, Object> varDefinitions = Maps.newHashMap();
253:
254:                 varDefinitions.put("self", EcorePackage.Literals.ECLASS);
255:                 final List<Expression> values = new ArrayList<Expression>();
256:                 values.add(varRef("self"));
257:                 values.add(varRef("self"));
258:                 values.add(booleanLiteral(true));
259:                 values.add(booleanLiteral(false));
260:
261:                 final EvaluationResult result = evaluator.eval(varDefinitions, sequenceInExtension(values));
262:                 assertTrue(result.getResult() instanceof List);
263:                 assertEquals(Diagnostic.OK, result.getDiagnostic().getSeverity());
264:                 assertTrue(result.getDiagnostic().getChildren().isEmpty());
265:                 List<Object> listResult = (List<Object>)result.getResult();
266:                 assertEquals(4, listResult.size());
267:                 Iterator<Object> it = listResult.iterator();
268:                 assertEquals(EcorePackage.Literals.ECLASS, it.next());
269:                 assertEquals(EcorePackage.Literals.ECLASS, it.next());
270:                 assertEquals(Boolean.TRUE, it.next());
271:                 assertEquals(Boolean.FALSE, it.next());
272:                 assertFalse(it.hasNext());
273:         }
274:
275:         /**
276:          * Test that the true branch is properly evaluated when the predicate evaluates to <code>true</code>
277:          */
278:         @Test
279:         public void testConditionnalTrueBranch() {
280:                 Map<String, Object> varDefinitions = Maps.newHashMap();
281:                 Conditional conditional = conditional(booleanLiteral(true), stringLiteral("trueBranch"),
282:                                 stringLiteral("falseBranch"));
283:                 assertOKResultEquals("trueBranch", evaluator.eval(varDefinitions, conditional));
284:         }
285:
286:         /**
287:          * Test that the false branch is properly evaluated when the predicate evaluates to <code>false</code>
288:          */
289:         @Test
290:         public void testConditionnalFalseBranch() {
291:                 Map<String, Object> varDefinitions = Maps.newHashMap();
292:                 Conditional conditional = conditional(booleanLiteral(false), stringLiteral("trueBranch"),
293:                                 stringLiteral("falseBranch"));
294:                 assertOKResultEquals("falseBranch", evaluator.eval(varDefinitions, conditional));
295:         }
296:
297:         /**
298:          * Test that the result is <code>Nothing</code> when the predicate isn't a boolean
299:          */
300:         @Test
301:         public void testConditionnalBadPredicate() {
302:                 Map<String, Object> varDefinitions = Maps.newHashMap();
303:                 Conditional conditional = conditional(stringLiteral("Hey, what's this?!"),
304:                                 stringLiteral("trueBranch"), stringLiteral("falseBranch"));
305:                 final EvaluationResult result = evaluator.eval(varDefinitions, conditional);
306:                 assertTrue(result.getResult() instanceof Nothing);
307:                 assertEquals(Diagnostic.WARNING, result.getDiagnostic().getSeverity());
308:                 assertEquals(1, result.getDiagnostic().getChildren().size());
309:                 String message = result.getDiagnostic().getChildren().get(0).getMessage();
310:                 assertTrue(message.contains("Conditional"));
311:                 assertTrue(message.contains("boolean"));
312:         }
313:
314:         @Test
315:         public void testLetBasic() {
316:                 Let let = let(callService("concat", varRef("x"), varRef("y")), binding("x", null,
317:                                 stringLiteral("prefix")), binding("y", null, stringLiteral("suffix")));
318:                 Map<String, Object> varDefinitions = Maps.newHashMap();
319:                 assertOKResultEquals("prefixsuffix", evaluator.eval(varDefinitions, let));
320:         }
321:
322:         @Test
323:         public void testLetArenotRecursive() {
324:                 Let let = let(callService("concat", varRef("x"), varRef("y")), binding("x", null,
325:                                 stringLiteral("prefix")), binding("y", null, callService("concat", varRef("x"),
326:                                 stringLiteral("end"))));
327:                 Map<String, Object> varDefinitions = Maps.newHashMap();
328:                 varDefinitions.put("x", "firstx");
329:                 assertOKResultEquals("prefixfirstxend", evaluator.eval(varDefinitions, let));
330:         }
331:
332:         @Test
333:         public void testLetWithNothingBound() {
334:                 Let let = let(callService("concat", varRef("x"), varRef("y")), binding("x", null, varRef("prefix")),
335:                                 binding("y", null, stringLiteral("suffix")));
336:                 Map<String, Object> varDefinitions = Maps.newHashMap();
337:                 final EvaluationResult result = evaluator.eval(varDefinitions, let);
338:                 assertTrue(result.getResult() instanceof Nothing);
339:                 assertEquals(Diagnostic.ERROR, result.getDiagnostic().getSeverity());
340:                 assertEquals(2, result.getDiagnostic().getChildren().size());
341:                 String message1 = result.getDiagnostic().getChildren().get(0).getMessage();
342:                 assertEquals(Diagnostic.ERROR, result.getDiagnostic().getChildren().get(0).getSeverity());
343:                 assertTrue(message1.contains("Couldn't find the 'prefix' variable"));
344:                 assertEquals(Diagnostic.WARNING, result.getDiagnostic().getChildren().get(1).getSeverity());
345:                 String message2 = result.getDiagnostic().getChildren().get(1).getMessage();
346:                 assertTrue(message2.contains("Couldn't find the 'concat"));
347:         }
348:
349:         @Test
350:         public void testLetWithNothingBody() {
351:                 Let let = let(callService("concat", varRef("novar"), varRef("y")), binding("x", null,
352:                                 stringLiteral("prefix")), binding("y", null, stringLiteral("suffix")));
353:                 Map<String, Object> varDefinitions = Maps.newHashMap();
354:                 final EvaluationResult result = evaluator.eval(varDefinitions, let);
355:                 assertTrue(result.getResult() instanceof Nothing);
356:                 assertEquals(Diagnostic.ERROR, result.getDiagnostic().getSeverity());
357:                 assertEquals(2, result.getDiagnostic().getChildren().size());
358:                 assertEquals(Diagnostic.ERROR, result.getDiagnostic().getChildren().get(0).getSeverity());
359:                 String message1 = result.getDiagnostic().getChildren().get(0).getMessage();
360:                 assertTrue(message1.contains("Couldn't find the 'novar' variable"));
361:                 assertEquals(Diagnostic.WARNING, result.getDiagnostic().getChildren().get(1).getSeverity());
362:                 String message2 = result.getDiagnostic().getChildren().get(1).getMessage();
363:                 assertTrue(message2.contains("Couldn't find the 'concat"));
364:         }
365:
366:         @Test
367:         public void testTypeSetLiteral() {
368:
369:                 final List<TypeLiteral> types = new ArrayList<TypeLiteral>();
370:                 types.add((TypeLiteral)typeLiteral(EcorePackage.eINSTANCE.getEClass()));
371:                 types.add((TypeLiteral)typeLiteral(EcorePackage.eINSTANCE.getEPackage()));
372:                 types.add((TypeLiteral)typeLiteral(EcorePackage.eINSTANCE.getEAttribute()));
373:                 Map<String, Object> varDefinitions = Maps.newHashMap();
374:                 final EvaluationResult result = evaluator.eval(varDefinitions, typeSetLiteral(types));
375:                 assertTrue(result.getResult() instanceof Set);
376:                 assertEquals(3, ((Set<?>)result.getResult()).size());
377:                 final Iterator<?> it = ((Set<?>)result.getResult()).iterator();
378:                 assertEquals(EcorePackage.eINSTANCE.getEClass(), it.next());
379:                 assertEquals(EcorePackage.eINSTANCE.getEPackage(), it.next());
380:                 assertEquals(EcorePackage.eINSTANCE.getEAttribute(), it.next());
381:         }
382:
383:         @Test
384:         public void testLetOverwriteVariable() {
385:                 Let let = let(varRef("self"), binding("self", null, stringLiteral("selfOverritten")));
386:                 Map<String, Object> varDefinitions = Maps.newHashMap();
387:                 varDefinitions.put("self", "self");
388:                 assertOKResultEquals("selfOverritten", evaluator.eval(varDefinitions, let));
389:         }
390:
391:         @Test
392:         public void testLetOverwriteBinding() {
393:                 Let let = let(varRef("a"), binding("a", null, stringLiteral("a")), binding("a", null,
394:                                 stringLiteral("aOverritten")));
395:                 Map<String, Object> varDefinitions = Maps.newHashMap();
396:                 varDefinitions.put("self", "self");
397:                 assertOKResultEquals("aOverritten", evaluator.eval(varDefinitions, let));
398:         }
399:
400:         private void assertOKResultEquals(Object expected, EvaluationResult result) {
401:                 assertEquals(expected, result.getResult());
402:                 assertEquals(Diagnostic.OK, result.getDiagnostic().getSeverity());
403:                 assertTrue(result.getDiagnostic().getChildren().isEmpty());
404:         }
405:
406: }