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

Added String Literal tests to interpreter, refactored stext.test

Andreas Mülder 14 лет назад
Родитель
Сommit
ea806dd733

+ 3 - 1
test-plugins/org.yakindu.sct.model.sexec.interpreter.test/META-INF/MANIFEST.MF

@@ -13,6 +13,8 @@ Require-Bundle: org.eclipse.core.runtime,
  org.yakindu.sct.model.stext;bundle-version="1.0.0",
  org.yakindu.sct.model.stext.resource;bundle-version="1.0.0",
  de.itemis.xtext.utils.gmf;bundle-version="1.0.0",
- org.eclipse.gmf.runtime.emf.core;bundle-version="1.4.1"
+ org.eclipse.gmf.runtime.emf.core;bundle-version="1.4.1",
+ org.eclipse.xtext.junit4;bundle-version="2.0.1",
+ org.yakindu.sct.model.stext.test;bundle-version="1.0.0"
 Bundle-RequiredExecutionEnvironment: J2SE-1.5
 Bundle-ActivationPolicy: lazy

+ 155 - 73
test-plugins/org.yakindu.sct.model.sexec.interpreter.test/src/org/yakindu/sct/model/sexec/interpreter/test/STextInterpreterTest.java

@@ -12,30 +12,34 @@ package org.yakindu.sct.model.sexec.interpreter.test;
 
 import static junit.framework.Assert.assertEquals;
 
+import org.eclipse.xtext.junit4.InjectWith;
+import org.eclipse.xtext.junit4.XtextRunner;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
-
-import util.AbstractSTextTest;
-
+import org.junit.runner.RunWith;
+import org.yakindu.sct.model.sexec.interpreter.stext.StextStatementInterpreter;
+import org.yakindu.sct.model.sgraph.Scope;
+import org.yakindu.sct.model.sgraph.Statement;
+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.simulation.core.runtime.IExecutionContext;
+import org.yakindu.sct.simulation.core.runtime.impl.ExecutionContextImpl;
+import org.yakindu.sct.simulation.core.runtime.impl.ExecutionEvent;
+import org.yakindu.sct.simulation.core.runtime.impl.ExecutionVariable;
+
+import com.google.inject.Inject;
+
+
+@RunWith(XtextRunner.class)
+@InjectWith(STextInjectorProvider.class)
 public class STextInterpreterTest extends AbstractSTextTest {
 
-//	@Test
-//	public void testRaise() {
-//		executeWithDefaultScope("raise abc");
-//		assertTrue(getContext().getRaisedEvents().size() == 1);
-//	}
-//
-//	@Test
-//	public void testRaiseWithValue() {
-//		executeWithDefaultScope("raise abc:42");
-//		assertTrue(getContext().getRaisedEvents().size() == 1);
-//		assertEquals(42, getContext().getRaisedEvents().get(0).getValue());
-//	}
-
-//	@Test
-//	public void testProcedureCall() {
-//		executeWithDefaultScope("foo()");
-//		assertEquals("foo", getContext().lastProcedureId);
-//	}
+	@Inject
+	private ExecutionContextImpl context;
+	@Inject
+	private StextStatementInterpreter interpreter;
 
 	@Test
 	public void testIntVariableAssignment() {
@@ -58,36 +62,21 @@ public class STextInterpreterTest extends AbstractSTextTest {
 	@Test
 	public void testBoolFalseVariableAssignment() {
 		executeWithDefaultScope("myBool = false");
-		assertEquals(false, getContext().getVariable("myBool")
-				.getValue());
+		assertEquals(false, getContext().getVariable("myBool").getValue());
 	}
 
 	@Test
 	public void testFloatVariableAssignment() {
-		executeWithDefaultScope("myFloat = 42.0");
-		assertEquals(42.0f, getContext().getVariable("myFloat")
-				.getValue());
+		executeWithDefaultScope("myReal = 42.0");
+		assertEquals(42.0f, getContext().getVariable("myReal").getValue());
+	}
+
+	@Test
+	public void testStringVariableAssignment() {
+		executeWithDefaultScope("myString = 'fortytwo'");
+		assertEquals("fortytwo", getStringValue());
 	}
 
-	//
-	// @Test
-	// public void testStringVariableAssignment() {
-	// executeWithDefaultScope("a = \"fortytwo\";");
-	// scope.addVariable(new RTVariable("a"));
-	// stmt.execute(scope);
-	//
-	// assertEquals("fortytwo", scope.getValue("a"));
-	// }
-	//
-	// @Test
-	// public void testIntStringVariableAssignment() {
-	// executeWithDefaultScope("a = \"42\";");
-	// scope.addVariable(new RTVariable("a"));
-	// stmt.execute(scope);
-	//
-	// assertEquals("42", scope.getValue("a"));
-	// }
-	//
 	@Test
 	public void testConditionalTrue() {
 		executeWithDefaultScope("myInt = true ? 42 : 1");
@@ -115,30 +104,26 @@ public class STextInterpreterTest extends AbstractSTextTest {
 	@Test
 	public void testBooleanAnd() {
 		executeWithDefaultScope("myBool = true && false");
-		assertEquals(false, getContext().getVariable("myBool")
-				.getValue());
+		assertEquals(false, getContext().getVariable("myBool").getValue());
 	}
 
 	@Test
 	public void testBitwiseXor() {
 		executeWithDefaultScope("myInt = 0xF0F0 ^ 0xFF00");
-		assertEquals(0x0FF0, getContext().getVariable("myInt")
-				.getValue());
+		assertEquals(0x0FF0, getContext().getVariable("myInt").getValue());
 
 	}
 
 	@Test
 	public void testBitwiseOr() {
 		executeWithDefaultScope("myInt = 0xF0F0 | 0xFFFF");
-		assertEquals(0xFFFF, getContext().getVariable("myInt")
-				.getValue());
+		assertEquals(0xFFFF, getContext().getVariable("myInt").getValue());
 	}
 
 	@Test
 	public void testBitwiseAnd() {
 		executeWithDefaultScope("myInt = 0xF0F0 & 0xFFFF");
-		assertEquals(0x0F0F0, getContext().getVariable("myInt")
-				.getValue());
+		assertEquals(0x0F0F0, getContext().getVariable("myInt").getValue());
 	}
 
 	@Test
@@ -158,6 +143,11 @@ public class STextInterpreterTest extends AbstractSTextTest {
 		executeWithDefaultScope("myBool = 1.0f == 1.0f");
 		assertEquals(true, getBoolValue());
 	}
+	@Test
+	public void testStringEqual() {
+		executeWithDefaultScope("myBool = 'string' == 'string'");
+		assertEquals(true, getBoolValue());
+	}
 
 	@Test
 	public void testBoolNotEqual() {
@@ -176,6 +166,11 @@ public class STextInterpreterTest extends AbstractSTextTest {
 		executeWithDefaultScope("myBool = 1.0f != 2.0f");
 		assertEquals(true, getBoolValue());
 	}
+	@Test
+	public void testStringNotEqual() {
+		executeWithDefaultScope("myBool = 'string' != 'string2'");
+		assertEquals(true, getBoolValue());
+	}
 
 	@Test
 	public void testIntGreaterEqual() {
@@ -236,7 +231,7 @@ public class STextInterpreterTest extends AbstractSTextTest {
 
 	@Test
 	public void testFloatPositive() {
-		executeWithDefaultScope("myFloat = +1.0");
+		executeWithDefaultScope("myReal = +1.0");
 		assertEquals(1.0f, getFloatValue());
 	}
 
@@ -262,7 +257,7 @@ public class STextInterpreterTest extends AbstractSTextTest {
 
 	@Test
 	public void testFloatNegative() {
-		executeWithDefaultScope("myFloat = -1.0f");
+		executeWithDefaultScope("myReal = -1.0f");
 		assertEquals(-1.0f, getFloatValue());
 	}
 
@@ -274,7 +269,7 @@ public class STextInterpreterTest extends AbstractSTextTest {
 
 	@Test
 	public void testFloatPlus() {
-		executeWithDefaultScope("myFloat = 42.0 + 1.0");
+		executeWithDefaultScope("myReal = 42.0 + 1.0");
 		assertEquals(43.0f, getFloatValue());
 	}
 
@@ -286,7 +281,7 @@ public class STextInterpreterTest extends AbstractSTextTest {
 
 	@Test
 	public void testFloatMinus() {
-		executeWithDefaultScope("myFloat = 42.0f - 1.0f");
+		executeWithDefaultScope("myReal = 42.0f - 1.0f");
 		assertEquals(41.0f, getFloatValue());
 	}
 
@@ -298,7 +293,7 @@ public class STextInterpreterTest extends AbstractSTextTest {
 
 	@Test
 	public void testFloatMultiply() {
-		executeWithDefaultScope("myFloat = 42.0f * 2.0f");
+		executeWithDefaultScope("myReal = 42.0f * 2.0f");
 		assertEquals(84.0f, getFloatValue());
 	}
 
@@ -310,7 +305,7 @@ public class STextInterpreterTest extends AbstractSTextTest {
 
 	@Test
 	public void testFloatDivide() {
-		executeWithDefaultScope("myFloat = 42.0f / 2.0f");
+		executeWithDefaultScope("myReal = 42.0f / 2.0f");
 		assertEquals(21.0f, getFloatValue());
 	}
 
@@ -322,7 +317,7 @@ public class STextInterpreterTest extends AbstractSTextTest {
 
 	@Test
 	public void testFloatModulo() {
-		executeWithDefaultScope("myFloat = 42.0f % 2.0f");
+		executeWithDefaultScope("myReal = 42.0f % 2.0f");
 		assertEquals(0.0f, getFloatValue());
 	}
 
@@ -468,9 +463,9 @@ public class STextInterpreterTest extends AbstractSTextTest {
 
 	@Test
 	public void testFloatPlusAssign() {
-		executeWithDefaultScope("myFloat = 42.0");
+		executeWithDefaultScope("myReal = 42.0");
 		System.out.println(getFloatValue());
-		executeWithDefaultScope("myFloat+=42.0");
+		executeWithDefaultScope("myReal+=42.0");
 		assertEquals(84.0f, getFloatValue());
 	}
 
@@ -483,8 +478,8 @@ public class STextInterpreterTest extends AbstractSTextTest {
 
 	@Test
 	public void testFloatMinusAssign() {
-		executeWithDefaultScope("myFloat=42.0f");
-		executeWithDefaultScope("myFloat-=10.0");
+		executeWithDefaultScope("myReal=42.0f");
+		executeWithDefaultScope("myReal-=10.0");
 		assertEquals(32.0f, getFloatValue());
 	}
 
@@ -497,8 +492,8 @@ public class STextInterpreterTest extends AbstractSTextTest {
 
 	@Test
 	public void testFloatMultAssign() {
-		executeWithDefaultScope("myFloat=42.0f");
-		executeWithDefaultScope("myFloat*=1.0");
+		executeWithDefaultScope("myReal=42.0f");
+		executeWithDefaultScope("myReal*=1.0");
 		assertEquals(42.0f, getFloatValue());
 	}
 
@@ -511,8 +506,8 @@ public class STextInterpreterTest extends AbstractSTextTest {
 
 	@Test
 	public void testFloatDivAssign() {
-		executeWithDefaultScope("myFloat=42.0f");
-		executeWithDefaultScope("myFloat/=1.0f");
+		executeWithDefaultScope("myReal=42.0f");
+		executeWithDefaultScope("myReal/=1.0f");
 		assertEquals(42.0f, getFloatValue());
 	}
 
@@ -525,8 +520,8 @@ public class STextInterpreterTest extends AbstractSTextTest {
 
 	@Test
 	public void testFloatModAssign() {
-		executeWithDefaultScope("myFloat=42.0f");
-		executeWithDefaultScope("myFloat%=1.0f");
+		executeWithDefaultScope("myReal=42.0f");
+		executeWithDefaultScope("myReal%=1.0f");
 		assertEquals(0.0f, getFloatValue());
 	}
 
@@ -626,11 +621,98 @@ public class STextInterpreterTest extends AbstractSTextTest {
 	// }
 	// }
 
-	@Test public void testPlainTrue() {
+	@Test
+	public void testPlainTrue() {
 		assertEquals(true, executeExpression("", "true"));
 	}
-	
-	@Test public void testPlainFalse() {
+
+	@Test
+	public void testPlainFalse() {
 		assertEquals(false, executeExpression("", "false"));
 	}
+
+	// Convenience...
+
+	@Before
+	public void setup() {
+		initContext();
+	}
+
+	private void initContext() {
+		// "event abc operation foo() var myInt : integer var MyBool : boolean
+		// var myReal : real
+		ExecutionVariable myInt = new ExecutionVariable("myInt", Integer.class,
+				0);
+		context.declareVariable(myInt);
+		ExecutionVariable myBool = new ExecutionVariable("myBool",
+				Boolean.class, false);
+		context.declareVariable(myBool);
+		ExecutionVariable myReal = new ExecutionVariable("myReal",
+				Float.class, 0.0f);
+		context.declareVariable(myReal);
+		ExecutionVariable myString = new ExecutionVariable("myString",
+				String.class, "");
+		context.declareVariable(myString);
+		ExecutionEvent event = new ExecutionEvent("abc", Integer.class);
+		context.declareEvent(event);
+	}
+
+	protected Object getBoolValue() {
+		return context.getVariable("myBool").getValue();
+	}
+
+	protected Object getIntValue() {
+		return context.getVariable("myInt").getValue();
+	}
+
+	protected Object getFloatValue() {
+		return context.getVariable("myReal").getValue();
+	}
+
+	protected Object getStringValue() {
+		return context.getVariable("myString").getValue();
+	}
+
+	protected Object executeWithDefaultScope(String expression) {
+		Scope defaultScope = createDefaultScope();
+		Expression statement = (Expression) parseExpression(expression,
+				defaultScope, Expression.class.getSimpleName());
+		return interpreter.evaluateStatement(statement, context);
+	}
+
+	protected Object execute(String scope, String expression) {
+		Scope defaultScope = createContextScope(scope);
+		Expression statement = (Expression) parseExpression(expression,
+				defaultScope, Expression.class.getSimpleName());
+		return interpreter.evaluateStatement(statement, context);
+	}
+
+	protected Object executeExpression(String scope, String expression) {
+		Scope defaultScope = createContextScope(scope);
+		Statement statement = (Statement) parseExpression(expression,
+				defaultScope, Expression.class.getSimpleName());
+		return interpreter.evaluateStatement(statement, context);
+	}
+
+	public IExecutionContext getContext() {
+		return context;
+	}
+
+	@After
+	public void tearDown() {
+		context = null;
+	}
+
+	protected static final class TestExecutionContext extends
+			ExecutionContextImpl {
+
+		public String lastProcedureId;
+
+		@Override
+		public void call(String procedureId) {
+			super.call(procedureId);
+			lastProcedureId = procedureId;
+		}
+	}
+
 }

+ 0 - 168
test-plugins/org.yakindu.sct.model.sexec.interpreter.test/src/util/AbstractSTextTest.java

@@ -1,168 +0,0 @@
-/**
- * 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 util;
-
-import java.io.StringReader;
-
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.xtext.ParserRule;
-import org.eclipse.xtext.XtextFactory;
-import org.eclipse.xtext.linking.ILinker;
-import org.eclipse.xtext.nodemodel.INode;
-import org.eclipse.xtext.parser.IParseResult;
-import org.eclipse.xtext.parser.IParser;
-import org.eclipse.xtext.resource.XtextResource;
-import org.eclipse.xtext.resource.impl.ListBasedDiagnosticConsumer;
-import org.junit.After;
-import org.junit.Before;
-import org.yakindu.sct.model.sexec.interpreter.stext.StextStatementInterpreter;
-import org.yakindu.sct.model.sgraph.Scope;
-import org.yakindu.sct.model.sgraph.Statement;
-import org.yakindu.sct.model.stext.STextStandaloneSetup;
-import org.yakindu.sct.model.stext.stext.Expression;
-import org.yakindu.sct.simulation.core.runtime.IExecutionContext;
-import org.yakindu.sct.simulation.core.runtime.impl.ExecutionContextImpl;
-import org.yakindu.sct.simulation.core.runtime.impl.ExecutionEvent;
-import org.yakindu.sct.simulation.core.runtime.impl.ExecutionVariable;
-
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-
-/**
- * 
- * @author andreas muelder - Initial contribution and API
- * 
- */
-public abstract class AbstractSTextTest {
-
-	@Inject
-	private IParser parser;
-	@Inject
-	private ILinker linker;
-	@Inject
-	private XtextResource resource;
-	@Inject
-	private ExecutionContextImpl context;
-	@Inject
-	private StextStatementInterpreter interpreter;
-
-	protected static final class TestExecutionContext extends
-			ExecutionContextImpl {
-
-		public String lastProcedureId;
-
-		@Override
-		public void call(String procedureId) {
-			super.call(procedureId);
-			lastProcedureId = procedureId;
-		}
-	}
-
-	@Before
-	public void setup() {
-		Injector injector = new STextStandaloneSetup()
-				.createInjectorAndDoEMFRegistration();
-		injector.injectMembers(this);
-		initContext();
-	}
-
-	private void initContext() {
-		// "event abc operation foo() var myInt : integer var MyBool : boolean
-		// var myReal : real
-		ExecutionVariable myInt = new ExecutionVariable("myInt", Integer.class,
-				0);
-		context.declareVariable(myInt);
-		ExecutionVariable myBool = new ExecutionVariable("myBool",
-				Boolean.class, false);
-		context.declareVariable(myBool);
-		ExecutionVariable myFloat = new ExecutionVariable("myFloat",
-				Float.class, 0.0f);
-		context.declareVariable(myFloat);
-		ExecutionEvent event = new ExecutionEvent("abc", Integer.class);
-		context.declareEvent(event);
-	}
-
-	protected Object getBoolValue() {
-		return context.getVariable("myBool").getValue();
-	}
-
-	protected Object getIntValue() {
-		return context.getVariable("myInt").getValue();
-	}
-
-	protected Object getFloatValue() {
-		return context.getVariable("myFloat").getValue();
-	}
-
-	@After
-	public void tearDown() {
-		context = null;
-	}
-
-	protected EObject parseExpression(String expression, Scope context,
-			String ruleName) {
-		ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
-		parserRule.setName(ruleName);
-		IParseResult result = parser.parse(parserRule, new StringReader(
-				expression));
-		EObject rootASTElement = result.getRootASTElement();
-		resource.getContents().add(rootASTElement);
-		if (context != null)
-			resource.getContents().add(context);
-		linker.linkModel(result.getRootASTElement(),
-				new ListBasedDiagnosticConsumer());
-		if (result.hasSyntaxErrors()) {
-			StringBuilder errorMessages = new StringBuilder();
-			Iterable<INode> syntaxErrors = result.getSyntaxErrors();
-			for (INode iNode : syntaxErrors) {
-				errorMessages.append(iNode.getSyntaxErrorMessage());
-				errorMessages.append("\n");
-			}
-			throw new RuntimeException(
-					"Could not parse expression, syntax errors: "
-							+ errorMessages);
-		}
-		return rootASTElement;
-	}
-
-	protected Scope createContextScope(String contextScope) {
-		ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
-		parserRule.setName("InternalScope");
-		IParseResult result = parser.parse(parserRule, new StringReader(
-				contextScope));
-		return (Scope) result.getRootASTElement();
-	}
-
-	protected Object executeWithDefaultScope(String expression) {
-		Scope defaultScope = createContextScope("internal: in event abc : integer operation foo() var myInt : integer var myBool : boolean var myFloat : real");
-		Expression statement = (Expression) parseExpression(expression,
-				defaultScope, Expression.class.getSimpleName());
-		return interpreter.evaluateStatement(statement, context);
-	}
-
-	protected Object execute(String scope, String expression) {
-		Scope defaultScope = createContextScope(scope);
-		Expression statement = (Expression) parseExpression(expression,
-				defaultScope, Expression.class.getSimpleName());
-		return interpreter.evaluateStatement(statement, context);
-	}
-
-	protected Object executeExpression(String scope, String expression) {
-		Scope defaultScope = createContextScope(scope);
-		Statement statement = (Statement) parseExpression(expression,
-				defaultScope, Expression.class.getSimpleName());
-		return interpreter.evaluateStatement(statement, context);
-	}
-
-	public IExecutionContext getContext() {
-		return context;
-	}
-}

+ 1 - 0
test-plugins/org.yakindu.sct.model.stext.test/META-INF/MANIFEST.MF

@@ -9,3 +9,4 @@ Require-Bundle: org.eclipse.xtext.junit4;bundle-version="2.0.1",
  org.yakindu.sct.model.sgraph;bundle-version="1.0.0",
  org.yakindu.sct.model.stext;bundle-version="1.0.0",
  org.junit;bundle-version="4.8.2"
+Export-Package: org.yakindu.sct.model.stext.test.util

+ 83 - 15
test-plugins/org.yakindu.sct.model.stext.test/src/org/yakindu/sct/model/stext/test/StaticTypeAnalyzerTest.java

@@ -12,14 +12,9 @@ package org.yakindu.sct.model.stext.test;
 
 import static org.junit.Assert.assertTrue;
 
-import java.io.StringReader;
-
 import org.eclipse.emf.ecore.EObject;
-import org.eclipse.xtext.ParserRule;
-import org.eclipse.xtext.XtextFactory;
 import org.eclipse.xtext.junit4.InjectWith;
 import org.eclipse.xtext.junit4.XtextRunner;
-import org.eclipse.xtext.parser.IParseResult;
 import org.eclipse.xtext.parser.IParser;
 import org.junit.Before;
 import org.junit.Rule;
@@ -28,6 +23,8 @@ import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
 import org.yakindu.base.types.Type;
 import org.yakindu.sct.model.sgraph.Statement;
+import org.yakindu.sct.model.stext.test.util.AbstractSTextTest;
+import org.yakindu.sct.model.stext.test.util.STextInjectorProvider;
 import org.yakindu.sct.model.stext.validation.ErrorAcceptor;
 import org.yakindu.sct.model.stext.validation.ITypeAnalyzer;
 import org.yakindu.sct.model.stext.validation.TypeCheckException;
@@ -40,7 +37,7 @@ import com.google.inject.Inject;
  */
 @RunWith(XtextRunner.class)
 @InjectWith(STextInjectorProvider.class)
-public class StaticTypeAnalyzerTest {
+public class StaticTypeAnalyzerTest extends AbstractSTextTest {
 
 	@Inject
 	private ITypeAnalyzer analyzer;
@@ -61,21 +58,26 @@ public class StaticTypeAnalyzerTest {
 		assertTrue(analyzer.isInteger(inferType("1")));
 		assertTrue(analyzer.isInteger(inferType("-1")));
 		assertTrue(analyzer.isInteger(inferType("0")));
+		assertTrue(analyzer.isInteger(inferType("myInt")));
 		// real
 		assertTrue(analyzer.isReal(inferType("1.0")));
 		assertTrue(analyzer.isReal(inferType("-1.0")));
 		assertTrue(analyzer.isReal(inferType("0.0")));
+		assertTrue(analyzer.isReal(inferType("myReal")));
 		// string
 		assertTrue(analyzer.isString(inferType("'42'")));
+		assertTrue(analyzer.isString(inferType("myString")));
 		// boolean
 		assertTrue(analyzer.isBoolean(inferType("true")));
 		assertTrue(analyzer.isBoolean(inferType("false")));
+		assertTrue(analyzer.isBoolean(inferType("myBool")));
 	}
 
 	// Add
 	@Test
 	public void testAddSuccess() {
 		assertTrue(analyzer.isInteger(inferType("1 + 2")));
+		assertTrue(analyzer.isInteger(inferType("myInt + 2")));
 		assertTrue(analyzer.isReal(inferType("1.0 + 2")));
 		assertTrue(analyzer.isReal(inferType("2 + 1.0")));
 		assertTrue(analyzer.isInteger(inferType("1 + 2 + 3.0")));
@@ -123,12 +125,20 @@ public class StaticTypeAnalyzerTest {
 		inferType("3.0 + 'string'");
 	}
 
+	@Test
+	public void testAddException8() {
+		expectOperatorPlusException();
+		inferType("myInt + 'string'");
+	}
+
 	// substract
 	@Test
 	public void testSubstractSuccess() {
 		assertTrue(analyzer.isInteger(inferType("1 - 2")));
+		assertTrue(analyzer.isInteger(inferType("myInt - 2")));
 		assertTrue(analyzer.isReal(inferType("1.0 - 2")));
 		assertTrue(analyzer.isReal(inferType("2 - 1.0")));
+		assertTrue(analyzer.isReal(inferType("myReal - 1.0")));
 		assertTrue(analyzer.isInteger(inferType("1 - 2 - 3.0")));
 	}
 
@@ -174,10 +184,17 @@ public class StaticTypeAnalyzerTest {
 		inferType("3.0 -  'string'");
 	}
 
+	@Test
+	public void testSubstractException8() {
+		expectOperatorSubstractException();
+		inferType("myReal -  'string'");
+	}
+
 	// multiply
 	@Test
 	public void testMultiplySuccess() {
 		assertTrue(analyzer.isInteger(inferType("1 * 2")));
+		assertTrue(analyzer.isReal(inferType("myInt * myReal")));
 		assertTrue(analyzer.isReal(inferType("1.0 * 2")));
 		assertTrue(analyzer.isReal(inferType("2 * 1.0")));
 		assertTrue(analyzer.isInteger(inferType("1 * 2 * 3.0")));
@@ -225,10 +242,17 @@ public class StaticTypeAnalyzerTest {
 		inferType("3.0 *  'string'");
 	}
 
+	@Test
+	public void testMultiplyException8() {
+		expectOperatorMultiplyException();
+		inferType("myReal *  'string'");
+	}
+
 	// divide
 	@Test
 	public void testDivideSuccess() {
 		assertTrue(analyzer.isInteger(inferType("1 / 2")));
+		assertTrue(analyzer.isInteger(inferType("1 / myInt")));
 		assertTrue(analyzer.isReal(inferType("1.0 / 2")));
 		assertTrue(analyzer.isReal(inferType("2 / 1.0")));
 		assertTrue(analyzer.isInteger(inferType("1 / 2 / 3.0")));
@@ -276,12 +300,19 @@ public class StaticTypeAnalyzerTest {
 		inferType("3.0 /  'string'");
 	}
 
+	@Test
+	public void testDivideException8() {
+		expectOperatorDivideException();
+		inferType("3.0 /  myString");
+	}
+
 	// mod
 	@Test
 	public void testModSuccess() {
 		assertTrue(analyzer.isInteger(inferType("1 % 2")));
 		assertTrue(analyzer.isReal(inferType("1.0 % 2")));
 		assertTrue(analyzer.isReal(inferType("2 % 1.0")));
+		assertTrue(analyzer.isReal(inferType("2 % myReal")));
 		assertTrue(analyzer.isInteger(inferType("1 % 2 % 3.0")));
 	}
 
@@ -327,14 +358,22 @@ public class StaticTypeAnalyzerTest {
 		inferType("3.0 % 'string'");
 	}
 
+	@Test
+	public void testModException8() {
+		expectOperatorModException();
+		inferType("3.0 % myString");
+	}
+
 	// Logical And Or Not
 	@Test
 	public void testLogicalSuccess() {
 		assertTrue(analyzer.isBoolean(inferType("true || false")));
+		assertTrue(analyzer.isBoolean(inferType("true || myBool")));
 		assertTrue(analyzer.isBoolean(inferType("true || false && true")));
 		assertTrue(analyzer
 				.isBoolean(inferType("true || true &&( false || true)")));
 		assertTrue(analyzer.isBoolean(inferType("!true")));
+		assertTrue(analyzer.isBoolean(inferType("!myBool")));
 		assertTrue(analyzer.isBoolean(inferType("!true && !false")));
 	}
 
@@ -392,27 +431,36 @@ public class StaticTypeAnalyzerTest {
 		inferType("!'Test'");
 	}
 
-	// LogicalRelation 
+	@Test
+	public void testLogicalException10() {
+		expectLogicalNotException();
+		inferType("!myString");
+	}
+
+	// LogicalRelation
 	@Test
 	public void testLogicalRelationSuccess() {
 		assertTrue(analyzer.isBoolean(inferType("5 < 3")));
 		assertTrue(analyzer.isBoolean(inferType("5.0 < 3")));
+		assertTrue(analyzer.isBoolean(inferType("5.0 < myInt")));
 
 		assertTrue(analyzer.isBoolean(inferType("5 <= 3")));
 		assertTrue(analyzer.isBoolean(inferType("5.0 <= 3")));
+		assertTrue(analyzer.isBoolean(inferType("5.0 <= myInt")));
 
 		assertTrue(analyzer.isBoolean(inferType("5 > 3")));
 		assertTrue(analyzer.isBoolean(inferType("5.0 >= 3")));
+		assertTrue(analyzer.isBoolean(inferType("5.0 >= myInt")));
 
 		assertTrue(analyzer.isBoolean(inferType("5 == 3")));
 		assertTrue(analyzer.isBoolean(inferType("'string' == 'string'")));
 		assertTrue(analyzer.isBoolean(inferType("5.0 == 3")));
-		assertTrue(analyzer.isBoolean(inferType("true == false")));
+		assertTrue(analyzer.isBoolean(inferType("true == myBool")));
 
 		assertTrue(analyzer.isBoolean(inferType("5 != 3")));
 		assertTrue(analyzer.isBoolean(inferType("'string' != 'string'")));
 		assertTrue(analyzer.isBoolean(inferType("5.0 != 3")));
-		assertTrue(analyzer.isBoolean(inferType("true != false")));
+		assertTrue(analyzer.isBoolean(inferType("true != myBool")));
 	}
 
 	@Test
@@ -558,11 +606,33 @@ public class StaticTypeAnalyzerTest {
 				.expectMessage("Incompatible operands real and boolean for operator '!='");
 		inferType("1.0 != false");
 	}
+	@Test
+	public void testAssignmentSuccess() {
+		inferType("myInt = 5 * 3");
+		inferType("myBool = true || false");
+		inferType("myString = 'string'");
+		inferType("myReal = 2.0 - 7");
+	}
+
+	@Test
+	public void testAssignmentException1() {
+		exception.expect(TypeCheckException.class);
+		exception
+				.expectMessage("Can not assign a value of type boolean to a variable of type integer");
+		inferType("myInt = true");
+	}
+	@Test
+	public void testAssignmentException2() {
+		exception.expect(TypeCheckException.class);
+		exception
+				.expectMessage("Can not assign a value of type boolean to a variable of type boolean");
+		inferType("myInt = myBoolean");
+	}
 
 	@Test
 	public void testComplexExpressionsSuccess() {
-		analyzer.isBoolean(inferType("((((3 * 7) + 5) % 2) > 97) || false"));
-		analyzer.isBoolean(inferType("!true != false && (3 > (7 * 5 + 3))"));
+		analyzer.isBoolean(inferType("((((3 * myInt) + 5) % 2) > 97) || false"));
+		analyzer.isBoolean(inferType("!true != myBool && (3 > (myReal * 5 + 3))"));
 	}
 
 	// TODO: BitwiseOrExpression, BitwiseAndExpression, BitwiseXOrExpression
@@ -614,10 +684,8 @@ public class StaticTypeAnalyzerTest {
 	}
 
 	protected Type inferType(String expression) {
-		ParserRule rule = XtextFactory.eINSTANCE.createParserRule();
-		rule.setName("Expression");
-		IParseResult parse = parser.parse(rule, new StringReader(expression));
-		EObject statement = parse.getRootASTElement();
+		EObject statement = super.parseExpression(expression,
+				super.createDefaultScope(), "Expression");
 		return analyzer.inferType((Statement) statement);
 	}
 

+ 86 - 0
test-plugins/org.yakindu.sct.model.stext.test/src/org/yakindu/sct/model/stext/test/util/AbstractSTextTest.java

@@ -0,0 +1,86 @@
+/**
+ * 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.io.StringReader;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.xtext.ParserRule;
+import org.eclipse.xtext.XtextFactory;
+import org.eclipse.xtext.diagnostics.Severity;
+import org.eclipse.xtext.linking.ILinker;
+import org.eclipse.xtext.nodemodel.INode;
+import org.eclipse.xtext.parser.IParseResult;
+import org.eclipse.xtext.parser.IParser;
+import org.eclipse.xtext.resource.XtextResource;
+import org.eclipse.xtext.resource.impl.ListBasedDiagnosticConsumer;
+import org.yakindu.sct.model.sgraph.Scope;
+
+import com.google.inject.Inject;
+
+/**
+ * 
+ * @author andreas muelder - Initial contribution and API
+ * 
+ */
+public abstract class AbstractSTextTest {
+
+	@Inject
+	private IParser parser;
+	@Inject
+	private ILinker linker;
+	@Inject
+	private XtextResource resource;
+
+	protected EObject parseExpression(String expression, Scope context,
+			String ruleName) {
+		ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
+		parserRule.setName(ruleName);
+		IParseResult result = parser.parse(parserRule, new StringReader(
+				expression));
+		EObject rootASTElement = result.getRootASTElement();
+		resource.getContents().add(rootASTElement);
+		if (context != null)
+			resource.getContents().add(context);
+		ListBasedDiagnosticConsumer diagnosticsConsumer = new ListBasedDiagnosticConsumer();
+		linker.linkModel(result.getRootASTElement(), diagnosticsConsumer);
+		linker.linkModel(context, diagnosticsConsumer);
+		if (result.hasSyntaxErrors()) {
+			StringBuilder errorMessages = new StringBuilder();
+			Iterable<INode> syntaxErrors = result.getSyntaxErrors();
+			for (INode iNode : syntaxErrors) {
+				errorMessages.append(iNode.getSyntaxErrorMessage());
+				errorMessages.append("\n");
+			}
+			throw new RuntimeException(
+					"Could not parse expression, syntax errors: "
+							+ errorMessages);
+		}
+		if (diagnosticsConsumer.hasConsumedDiagnostics(Severity.ERROR)) {
+			throw new RuntimeException("Error during linking: "
+					+ diagnosticsConsumer.getResult(Severity.ERROR));
+		}
+		return rootASTElement;
+	}
+
+	protected Scope createContextScope(String contextScope) {
+		ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
+		parserRule.setName("InternalScope");
+		IParseResult result = parser.parse(parserRule, new StringReader(
+				contextScope));
+		return (Scope) result.getRootASTElement();
+	}
+
+	protected Scope createDefaultScope() {
+		return createContextScope("internal: in event abc : integer operation foo() var myInt : integer var myBool : boolean var myReal : real var myString : string");
+	}
+
+}

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

@@ -8,7 +8,7 @@
  * 	committers of YAKINDU - initial API and implementation
  * 
  */
-package org.yakindu.sct.model.stext.test;
+package org.yakindu.sct.model.stext.test.util;
 
 import org.eclipse.xtext.junit4.IInjectorProvider;
 import org.yakindu.sct.model.stext.STextRuntimeModule;