public void IfElseStatementTrue() { // Arrange StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine("b = 15"); scriptBuilder.AppendLine("if b > 10 do"); scriptBuilder.AppendLine(" b = 20"); scriptBuilder.AppendLine(" b = b * 2"); scriptBuilder.AppendLine("else do"); scriptBuilder.AppendLine(" b = 10"); scriptBuilder.AppendLine("end"); TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(scriptBuilder.ToString()); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); TurtleScriptValue variableValue = interpreter.Variables["b"]; Assert.AreEqual(40.0, variableValue.NumericValue); }
public void VariableReassignmentTest() { // Arrange StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine("a = 1"); scriptBuilder.AppendLine("bcd = 200"); scriptBuilder.AppendLine("a = 100"); TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(scriptBuilder.ToString()); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); Assert.AreEqual(2, interpreter.Variables.Count); TurtleScriptValue variableValue = interpreter.Variables["a"]; Assert.AreEqual(100, variableValue.NumericValue); variableValue = interpreter.Variables["bcd"]; Assert.AreEqual(200, variableValue.NumericValue); }
public void DuplicateFunctionDefinition() { // Arrange StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine("def MyFunc(a)"); scriptBuilder.AppendLine(" b = 15 * a"); scriptBuilder.AppendLine("end"); scriptBuilder.AppendLine("def MyFunc(a)"); scriptBuilder.AppendLine(" b = 15 * a"); scriptBuilder.AppendLine("end"); scriptBuilder.AppendLine("MyFunc(1,2,3)"); var script = scriptBuilder.ToString(); TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert OutputResults(script, interpreter); Assert.IsFalse(success); Assert.AreEqual( "A function with the name 'MyFunc' already exists. Line 4, Column 0", interpreter.ErrorMessage); }
private void buttonExecute_Click(object sender, EventArgs e) { var runtimeFunctions = new ScriptedTurtleFunctions(); var mathRuntimeFunctions = new TurtleMath(); TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(txtScript.Text, new List <ITurtleScriptRuntime> { runtimeFunctions, mathRuntimeFunctions }); buttonExecute.Enabled = false; bool result; Cursor = Cursors.WaitCursor; try { result = interpreter.Execute(); } finally { buttonExecute.Enabled = true; Cursor = Cursors.Default; } if (!result) { MessageBox.Show(this, interpreter.ErrorMessage, "Error"); } UpdateStatusBar(); }
public void UnknownVariableReference() { // Arrange var script = "a = 55 + ABC"; TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert OutputResults(script, interpreter); Assert.IsFalse(success); Assert.AreEqual("Reference to an unknown variable, 'ABC'. Line 1, Col 9", interpreter.ErrorMessage); }
public void Garbage5() { // Arrange var script = "55 + ABC"; TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert OutputResults(script, interpreter); Assert.IsFalse(success); Assert.AreEqual("mismatched input '55' expecting <EOF>. Line 1, Col 0", interpreter.ErrorMessage); }
public void Garbage2() { // Arrange var script = "1234"; TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert OutputResults(script, interpreter); Assert.IsFalse(success); Assert.AreEqual("extraneous input '1234' expecting <EOF>. Line 1, Col 0", interpreter.ErrorMessage); }
public void Garbage1() { // Arrange var script = "abcd"; TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert OutputResults(script, interpreter); Assert.IsFalse(success); Assert.AreEqual("no viable alternative at input 'abcd'. Line 1, Col 4", interpreter.ErrorMessage); }
public void ExtraParenthesis1() { // Arrange var script = "a = 1 + 1 )"; TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert OutputResults(script, interpreter); Assert.IsFalse(success); Assert.AreEqual("extraneous input ')' expecting <EOF>. Line 1, Col 10", interpreter.ErrorMessage); }
public void MissingTrailingParenthesis3() { // Arrange var script = "a = (( 5 * ( 1 + 1 ))"; TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert OutputResults(script, interpreter); Assert.IsFalse(success); Assert.AreEqual("missing ')' at '<EOF>'. Line 1, Col 21", interpreter.ErrorMessage); }
public void DivisionFirstThenMultiplication() { // Arrange var script = "a = 100 / 2 * 10"; TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); TurtleScriptValue variableValue = interpreter.Variables["a"]; Assert.AreEqual(500, variableValue.NumericValue); }
public void DivideByZero() { // Arrange var script = "a = 5 / 0"; TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); TurtleScriptValue variableValue = interpreter.Variables["a"]; Assert.AreEqual(0, variableValue.NumericValue); }
public void UnknownFunctionReference() { // Arrange var script = "a = 55 + ABC()"; TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert OutputResults(script, interpreter); Assert.IsFalse(success); Assert.AreEqual( "Invalid identifier. Function name not previously defined. Line 1, Column 9", interpreter.ErrorMessage); }
public void UnknownRuntimeFunctionReference() { // Arrange var script = "a = 55 + ur.ABC()"; TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert OutputResults(script, interpreter); Assert.IsFalse(success); ; Assert.AreEqual( "Invalid runtime library name 'ur' specified on function call. Line 1, Column 9", interpreter.ErrorMessage); }
public void ComplexSubtraction() { // Arrange var script = "a = 10 - 2 - 4"; TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); TurtleScriptValue variableValue = interpreter.Variables["a"]; Assert.AreEqual(4, variableValue.NumericValue); }
public void SimpleMultiplication() { // Arrange var script = "a = 5 * 6"; TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); TurtleScriptValue variableValue = interpreter.Variables["a"]; Assert.AreEqual(30, variableValue.NumericValue); }
public void AssignmentAndReferenceInExpression() { // Arrange StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine("a = 1"); scriptBuilder.AppendLine("b = a + 500"); TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(scriptBuilder.ToString()); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); TurtleScriptValue variableValue = interpreter.Variables["b"]; Assert.AreEqual(501, variableValue.NumericValue); }
public void AssignmentAndSelfReassignment() { // Arrange StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine("a = 12345"); scriptBuilder.AppendLine("a = a"); TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(scriptBuilder.ToString()); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); TurtleScriptValue variableValue = interpreter.Variables["a"]; Assert.AreEqual(12345, variableValue.NumericValue); }
public void FunctionDeclarationAfterCall() { // Arrange StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine("MyFunc()"); scriptBuilder.AppendLine("def MyFunc()"); scriptBuilder.AppendLine(" b = 15"); scriptBuilder.AppendLine("end"); TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(scriptBuilder.ToString()); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); TurtleScriptValue variableValue = interpreter.Variables["b"]; Assert.AreEqual(15.0, variableValue.NumericValue); }
public void FunctionDeclarationAndCallThreeParameters() { // Arrange StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine("def MyFunc(value1, value2, value3)"); scriptBuilder.AppendLine(" b = value1 + value2 - value3"); scriptBuilder.AppendLine("end"); scriptBuilder.AppendLine("MyFunc(100, 200, 50)"); TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(scriptBuilder.ToString()); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); TurtleScriptValue variableValue = interpreter.Variables["b"]; Assert.AreEqual(250, variableValue.NumericValue); }
public void Reverse() { // Arrange StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine("b = 0"); scriptBuilder.AppendLine("for a = 5 to 1 do"); scriptBuilder.AppendLine("b = b + a"); scriptBuilder.AppendLine("end"); TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(scriptBuilder.ToString()); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); TurtleScriptValue variableValue = interpreter.Variables["b"]; Assert.AreEqual(15, variableValue.NumericValue); }
public void ThreeParameters() { // Arrange StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine("a = test.sum(2, 4, 6)"); TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(scriptBuilder.ToString(), new List <ITurtleScriptRuntime>() { new SampleRuntime() }); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); TurtleScriptValue variableValue = interpreter.Variables["a"]; Assert.AreEqual(12, variableValue.NumericValue); }
public void SimpleAssignmentTest() { // Arrange var script = "a = 1"; TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); Assert.AreEqual(1, interpreter.Variables.Count); string variableName = interpreter.Variables.Keys.First(); TurtleScriptValue variableValue = interpreter.Variables[variableName]; Assert.AreEqual("a", variableName); Assert.AreEqual(1, variableValue.NumericValue); }
public void FunctionCallWrongNumberOfParameters() { // Arrange StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine("def MyFunc(a)"); scriptBuilder.AppendLine(" b = 15 * a"); scriptBuilder.AppendLine("end"); scriptBuilder.AppendLine("MyFunc(1,2,3)"); var script = scriptBuilder.ToString(); TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert OutputResults(script, interpreter); Assert.IsFalse(success); Assert.AreEqual( "Invalid identifier. Function name not previously defined. Line 4, Column 0", interpreter.ErrorMessage); }
public void FunctionDeclarationAndCallOneParameter() { // Arrange StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine("def MyFunc(myparameter)"); scriptBuilder.AppendLine(" b = myparameter"); scriptBuilder.AppendLine(" myparameter = b * 2"); scriptBuilder.AppendLine("end"); scriptBuilder.AppendLine("MyFunc(10)"); TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(scriptBuilder.ToString()); // Act bool success = interpreter.Execute(); // Assert Assert.IsTrue(success, interpreter.ErrorMessage); TurtleScriptValue variableValue = interpreter.Variables["myparameter"]; Assert.AreEqual(20, variableValue.NumericValue); }
public void ForLoopStartParameterMustBeNumeric() { // Arrange StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine("b = 0"); scriptBuilder.AppendLine("for a = (2>1) to 5 do"); scriptBuilder.AppendLine("b = b + a"); scriptBuilder.AppendLine("end"); var script = scriptBuilder.ToString(); TurtleScriptInterpreter interpreter = new TurtleScriptInterpreter(script); // Act bool success = interpreter.Execute(); // Assert OutputResults(script, interpreter); Assert.IsFalse(success); Assert.AreEqual( $"For loop starting value must be numeric. Line 2, Column 0", interpreter.ErrorMessage); }