Exemplo n.º 1
0
        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);
        }
Exemplo n.º 3
0
        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();
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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);
        }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        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);
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 21
0
        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);
        }
Exemplo n.º 22
0
        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);
        }
Exemplo n.º 24
0
        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);
        }
Exemplo n.º 26
0
        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);
        }