Просмотр исходного кода

Added type inferrer tests for Bitwise expressions, active statement

Andreas Mülder 13 лет назад
Родитель
Сommit
0cffbedeb2

+ 241 - 4
test-plugins/org.yakindu.sct.model.stext.test/src/org/yakindu/sct/model/stext/test/TypeInferrerTest.java

@@ -29,6 +29,7 @@ import org.yakindu.sct.model.stext.stext.EventValueReferenceExpression;
 import org.yakindu.sct.model.stext.stext.Expression;
 import org.yakindu.sct.model.stext.test.util.AbstractSTextTest;
 import org.yakindu.sct.model.stext.test.util.STextInjectorProvider;
+import org.yakindu.sct.model.stext.test.util.STextTestScopeProvider;
 import org.yakindu.sct.model.stext.validation.ITypeInferrer;
 import org.yakindu.sct.model.stext.validation.TypeCheckException;
 
@@ -54,6 +55,7 @@ public class TypeInferrerTest extends AbstractSTextTest {
 	public void testUnarySuccess() {
 		// int
 		assertTrue(ts.isInteger(getType("1")));
+		assertTrue(ts.isInteger(getType("0x0F")));
 		assertTrue(ts.isInteger(getType("-1")));
 		assertTrue(ts.isInteger(getType("0")));
 		assertTrue(ts.isInteger(getType("myInt")));
@@ -80,6 +82,9 @@ public class TypeInferrerTest extends AbstractSTextTest {
 		assertTrue(ts.isInteger(analyzer.getType(statement)));
 
 		assertTrue(ts.isInteger(getType("1 + 2")));
+		assertTrue(ts.isInteger(getType("1 + 0x0F")));
+		assertTrue(ts.isInteger(getType("0x0F + 0x0F")));
+		assertTrue(ts.isInteger(getType("myInt + 0x0F")));
 		assertTrue(ts.isInteger(getType("myInt + 2")));
 		assertTrue(ts.isReal(getType("1.1 + 2")));
 		assertTrue(ts.isReal(getType("2 + 1.0")));
@@ -138,6 +143,9 @@ public class TypeInferrerTest extends AbstractSTextTest {
 	@Test
 	public void testSubstractSuccess() {
 		assertTrue(ts.isInteger(getType("1 - 2")));
+		assertTrue(ts.isInteger(getType("0x0F - 2")));
+		assertTrue(ts.isInteger(getType("0x0F - 0x0F")));
+		assertTrue(ts.isInteger(getType("0x0F- myInt")));
 		assertTrue(ts.isInteger(getType("myInt - 2")));
 		assertTrue(ts.isReal(getType("1.0 - 2")));
 		assertTrue(ts.isReal(getType("2 - 1.0")));
@@ -197,6 +205,8 @@ public class TypeInferrerTest extends AbstractSTextTest {
 	@Test
 	public void testMultiplySuccess() {
 		assertTrue(ts.isInteger(getType("1 * 2")));
+		assertTrue(ts.isInteger(getType("1 * 0x0F")));
+		assertTrue(ts.isInteger(getType("0x0F * myInt")));
 		assertTrue(ts.isReal(getType("myInt * myReal")));
 		assertTrue(ts.isReal(getType("1.0 * 2")));
 		assertTrue(ts.isReal(getType("2 * 1.0")));
@@ -256,6 +266,9 @@ public class TypeInferrerTest extends AbstractSTextTest {
 	public void testDivideSuccess() {
 		assertTrue(ts.isInteger(getType("1 / 2")));
 		assertTrue(ts.isInteger(getType("1 / myInt")));
+		assertTrue(ts.isInteger(getType("1 / 0x0F")));
+		assertTrue(ts.isInteger(getType("0x0F / 0x0F")));
+		assertTrue(ts.isInteger(getType("myInt / 0x0F")));
 		assertTrue(ts.isReal(getType("1.0 / 2")));
 		assertTrue(ts.isReal(getType("2 / 1.0")));
 		assertTrue(ts.isReal(getType("1 / 2 / 3.0")));
@@ -313,6 +326,9 @@ public class TypeInferrerTest extends AbstractSTextTest {
 	@Test
 	public void testModSuccess() {
 		assertTrue(ts.isInteger(getType("1 % 2")));
+		assertTrue(ts.isInteger(getType("1 % 0x0F")));
+		assertTrue(ts.isInteger(getType("0x0F % 0x0F")));
+		assertTrue(ts.isInteger(getType("myInt % 0x0F")));
 		assertTrue(ts.isReal(getType("1.0 % 2")));
 		assertTrue(ts.isReal(getType("2 % 1.0")));
 		assertTrue(ts.isReal(getType("2 % myReal")));
@@ -373,8 +389,7 @@ public class TypeInferrerTest extends AbstractSTextTest {
 		assertTrue(ts.isBoolean(getType("true || false")));
 		assertTrue(ts.isBoolean(getType("true || myBool")));
 		assertTrue(ts.isBoolean(getType("true || false && true")));
-		assertTrue(ts
-				.isBoolean(getType("true || true &&( false || true)")));
+		assertTrue(ts.isBoolean(getType("true || true &&( false || true)")));
 		assertTrue(ts.isBoolean(getType("!true")));
 		assertTrue(ts.isBoolean(getType("!myBool")));
 		assertTrue(ts.isBoolean(getType("!true && !false")));
@@ -613,6 +628,9 @@ public class TypeInferrerTest extends AbstractSTextTest {
 	@Test
 	public void testAssignmentSuccess() {
 		getType("myInt = 5 * 3");
+		getType("myInt = 0x0F * 3");
+		getType("myInt = 0x0F * 0x0F");
+		getType("myInt = myInt * 0x0F");
 		getType("myBool = true || false");
 		getType("myString = 'string'");
 		getType("myReal = 2.0 - 7");
@@ -634,6 +652,185 @@ public class TypeInferrerTest extends AbstractSTextTest {
 		getType("myInt = myBool");
 	}
 
+	/**
+	 * the {@link STextTestScopeProvider} adds a dummy state named 'chart.r1.A'
+	 * to the Scope.
+	 */
+	@Test
+	public void testActiveSuccess() throws Exception {
+		assertTrue(ts.isBoolean(getType("active(chart.r1.A)")));
+		assertTrue(ts.isBoolean(getType("!active(chart.r1.A)")));
+		assertTrue(ts.isBoolean(getType("true || active(chart.r1.A)")));
+		assertTrue(ts.isBoolean(getType("active(chart.r1.A) && false")));
+		assertTrue(ts.isBoolean(getType("myBool = active(chart.r1.A)")));
+	}
+
+	@Test
+	public void testActiveException1() throws Exception {
+		expectOperatorPlusException();
+		getType("active(chart.r1.A) + 1");
+	}
+
+	@Test
+	public void testActiveException2() throws Exception {
+		expectOperatorSubstractException();
+		getType("active(chart.r1.A) -1");
+	}
+
+	@Test
+	public void testActiveException3() throws Exception {
+		expectOperatorMultiplyException();
+		getType("active(chart.r1.A) *1");
+	}
+
+	@Test
+	public void testActiveException4() throws Exception {
+		expectOperatorDivideException();
+		getType("active(chart.r1.A) /1");
+	}
+
+	@Test
+	public void testActiveException5() throws Exception {
+		expectOperatorModException();
+		getType("active(chart.r1.A) % true");
+	}
+
+	@Test
+	public void testActiveException6() throws Exception {
+		expectLogicalAndException();
+		getType("active(chart.r1.A) && myInt");
+	}
+
+	@Test
+	public void testActiveException7() throws Exception {
+		expectLogicalOrException();
+		getType("active(chart.r1.A) || myString");
+	}
+
+	@Test
+	public void testActiveException8() throws Exception {
+		expectLogicalNotException();
+		getType("active(chart.r1.A) && !myString");
+	}
+
+	@Test
+	public void testBitwiseLogicalRelationSuccess() {
+		assertTrue(ts.isInteger(getType(" 5 & 3")));
+		assertTrue(ts.isInteger(getType(" 5 | 3")));
+		assertTrue(ts.isInteger(getType(" 5 ^ 3")));
+		assertTrue(ts.isInteger(getType(" ~3")));
+		assertTrue(ts.isInteger(getType("3 << 2")));
+		assertTrue(ts.isInteger(getType("5 >> 2")));
+	}
+
+	@Test
+	public void testBitwiseAndException1() throws Exception {
+		expectBitwiseAndException();
+		getType(" 5 & true");
+	}
+
+	@Test
+	public void testBitwiseAndException2() throws Exception {
+		expectBitwiseAndException();
+		getType(" 5 & 1.0");
+	}
+
+	@Test
+	public void testBitwiseAndException3() throws Exception {
+		expectBitwiseAndException();
+		getType(" 5 & 'myString'");
+	}
+
+	@Test
+	public void testBitwiseOrException1() throws Exception {
+		expectBitwiseOrException();
+		getType(" 5 | true");
+	}
+
+	@Test
+	public void testBitwiseOrException2() throws Exception {
+		expectBitwiseOrException();
+		getType(" 5 | 1.0");
+	}
+
+	@Test
+	public void testBitwiseOrException3() throws Exception {
+		expectBitwiseOrException();
+		getType(" 5 | myString");
+	}
+
+	@Test
+	public void testBitwiseXorException1() throws Exception {
+		expectBitwiseXorException();
+		getType(" 5 ^ true");
+	}
+
+	@Test
+	public void testBitwiseXorException2() throws Exception {
+		expectBitwiseXorException();
+		getType(" 5 ^ 7.0");
+	}
+
+	@Test
+	public void testBitwiseXorException3() throws Exception {
+		expectBitwiseXorException();
+		getType(" 5 ^ myString");
+	}
+
+	@Test
+	public void testBitwiseComplementException1() throws Exception {
+		expectBitwiseComplementException();
+		getType(" ~true");
+	}
+
+	@Test
+	public void testBitwiseComplementException2() throws Exception {
+		expectBitwiseComplementException();
+		getType(" ~9.0 ");
+	}
+
+	@Test
+	public void testBitwiseComplementException3() throws Exception {
+		expectBitwiseComplementException();
+		getType(" ~myString");
+	}
+
+	@Test
+	public void testBitwiseLeftShiftException1() throws Exception {
+		expectBitwiseLeftShiftException();
+		getType(" 5 << true");
+	}
+
+	@Test
+	public void testBitwiseLeftShiftException2() throws Exception {
+		expectBitwiseLeftShiftException();
+		getType(" 5 << 7.0");
+	}
+
+	@Test
+	public void testBitwiseLeftShiftException3() throws Exception {
+		expectBitwiseLeftShiftException();
+		getType(" 5 << myString");
+	}
+
+	@Test
+	public void testBitwiseRightShiftException1() throws Exception {
+		expectBitwiseRightShiftException();
+		getType(" 5 >> true");
+	}
+
+	@Test
+	public void testBitwiseRightShiftException2() throws Exception {
+		expectBitwiseRightShiftException();
+		getType(" 5 >> 7.0");
+	}
+
+	@Test
+	public void testBitwiseRightShiftException3() throws Exception {
+		expectBitwiseRightShiftException();
+		getType(" 5 >> myString");
+	}
+
 	@Test
 	public void testComplexExpressionsSuccess() {
 		ts.isBoolean(getType("((((3 * myInt) + 5) % 2) > 97) || false"));
@@ -641,8 +838,6 @@ public class TypeInferrerTest extends AbstractSTextTest {
 		ts.isInteger(getType("3 * 3 + 7 / (3 * myInt % 8)"));
 	}
 
-	// TODO: BitwiseOrExpression, BitwiseAndExpression, BitwiseXOrExpression
-
 	@Test
 	public void testEventRaisingSuccess() {
 
@@ -765,6 +960,48 @@ public class TypeInferrerTest extends AbstractSTextTest {
 				.expectMessage("operator '!' can only be applied to boolean values!");
 	}
 
+	private void expectBitwiseAndException() {
+		exception.expect(TypeCheckException.class);
+		exception
+				.expectMessage("operator '&' can only be applied to integers!");
+
+	}
+
+	private void expectBitwiseOrException() {
+		exception.expect(TypeCheckException.class);
+		exception
+				.expectMessage("operator '|' can only be applied to integers!");
+
+	}
+
+	private void expectBitwiseXorException() {
+		exception.expect(TypeCheckException.class);
+		exception
+				.expectMessage("operator '^' can only be applied to integers!");
+
+	}
+
+	private void expectBitwiseComplementException() {
+		exception.expect(TypeCheckException.class);
+		exception
+				.expectMessage("operator '~' can only be applied to integers!");
+
+	}
+
+	private void expectBitwiseLeftShiftException() {
+		exception.expect(TypeCheckException.class);
+		exception
+				.expectMessage("operator '<<' can only be applied to integers!");
+
+	}
+
+	private void expectBitwiseRightShiftException() {
+		exception.expect(TypeCheckException.class);
+		exception
+				.expectMessage("operator '>>' can only be applied to integers!");
+
+	}
+
 	private Scope createValuedEventsScope() {
 		return createInternalScope("internal: var myBool : boolean event intEvent : integer  event boolEvent : boolean event realEvent : real event stringEvent : string event voidEvent : void");
 	}

+ 1 - 2
test-plugins/org.yakindu.sct.model.stext.test/src/org/yakindu/sct/model/stext/test/util/STextInjectorProvider.java

@@ -11,7 +11,6 @@
 package org.yakindu.sct.model.stext.test.util;
 
 import org.eclipse.xtext.junit4.IInjectorProvider;
-import org.yakindu.sct.model.stext.STextRuntimeModule;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
@@ -24,7 +23,7 @@ import com.google.inject.Injector;
 public class STextInjectorProvider implements IInjectorProvider {
 
 	public Injector getInjector() {
-		return Guice.createInjector(new STextRuntimeModule());
+		return Guice.createInjector(new STextRuntimeTestModule());
 	}
 
 }

+ 27 - 0
test-plugins/org.yakindu.sct.model.stext.test/src/org/yakindu/sct/model/stext/test/util/STextRuntimeTestModule.java

@@ -0,0 +1,27 @@
+/**
+ * Copyright (c) 2012 committers of YAKINDU and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * Contributors:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.model.stext.test.util;
+
+import org.eclipse.xtext.scoping.IScopeProvider;
+import org.yakindu.sct.model.stext.STextRuntimeModule;
+
+/**
+ * 
+ * @author andreas muelder - Initial contribution and API
+ * 
+ */
+public class STextRuntimeTestModule extends STextRuntimeModule {
+
+	@Override
+	public Class<? extends IScopeProvider> bindIScopeProvider() {
+		return STextTestScopeProvider.class;
+	}
+}

+ 70 - 0
test-plugins/org.yakindu.sct.model.stext.test/src/org/yakindu/sct/model/stext/test/util/STextTestScopeProvider.java

@@ -0,0 +1,70 @@
+/**
+ * Copyright (c) 2012 committers of YAKINDU and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * Contributors:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.model.stext.test.util;
+
+import java.util.HashMap;
+import java.util.List;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.xtext.naming.IQualifiedNameProvider;
+import org.eclipse.xtext.resource.EObjectDescription;
+import org.eclipse.xtext.resource.IEObjectDescription;
+import org.eclipse.xtext.scoping.IScope;
+import org.eclipse.xtext.scoping.impl.SimpleScope;
+import org.yakindu.sct.model.sgraph.Region;
+import org.yakindu.sct.model.sgraph.SGraphFactory;
+import org.yakindu.sct.model.sgraph.State;
+import org.yakindu.sct.model.sgraph.Statechart;
+import org.yakindu.sct.model.stext.scoping.STextScopeProvider;
+
+import com.google.common.collect.Lists;
+import com.google.inject.Inject;
+
+/**
+ * 
+ * @author andreas muelder - Initial contribution and API
+ * 
+ */
+public class STextTestScopeProvider extends STextScopeProvider {
+
+	private static final SGraphFactory factory = SGraphFactory.eINSTANCE;
+
+	@Inject
+	private IQualifiedNameProvider qfnProvider;
+
+	public IScope getScope(EObject context, EReference reference) {
+		IScope parentScope = super.getScope(context, reference);
+
+		State dummyState = createDummyModel();
+		IEObjectDescription desc = new EObjectDescription(
+				qfnProvider.getFullyQualifiedName(dummyState), dummyState,
+				new HashMap<String, String>());
+		List<IEObjectDescription> descriptions = Lists.newArrayList(parentScope
+				.getAllElements());
+		descriptions.add(desc);
+		return new SimpleScope(descriptions);
+
+	}
+
+	private State createDummyModel() {
+		Statechart statechart = factory.createStatechart();
+		statechart.setName("chart");
+		Region region = factory.createRegion();
+		region.setName("r1");
+		statechart.getRegions().add(region);
+		State state = factory.createState();
+		state.setName("A");
+		region.getVertices().add(state);
+		return state;
+	}
+
+}

+ 11 - 0
test-plugins/org.yakindu.sct.model.stext.test/src/org/yakindu/sct/model/stext/test/util/StextTestFactory.java

@@ -1,3 +1,14 @@
+/**
+ * Copyright (c) 2012 itemis AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors:
+ * 	itemis AG - initial API and implementation
+ * 
+ */
 package org.yakindu.sct.model.stext.test.util;
 
 import org.yakindu.base.base.NamedElement;