public void TestReplaceVariablesMethodCall()
        {
            var functionDefinition = UserFunctionDefinitionExpression.ParseForTest("function func(i) { j = i }");

            var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { new IntegerConstantExpression(2) });
            var value1       = new IntegerConstantExpression(98);
            var expr         = new DictionaryExpression();

            expr.Add(functionCall, value1);

            var scope = new InterpreterScope();

            scope.AddFunction(functionDefinition);

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.False);
            Assert.That(result, Is.InstanceOf <ParseErrorExpression>());
            var parseError = (ParseErrorExpression)result;

            while (parseError.InnerError != null)
            {
                parseError = parseError.InnerError;
            }
            Assert.That(parseError.Message, Is.EqualTo("func did not return a value"));
        }
        public void TestReplaceVariables()
        {
            var variable1 = new VariableExpression("variable1");
            var variable2 = new VariableExpression("variable2");
            var value1    = new IntegerConstantExpression(98);
            var value2    = new IntegerConstantExpression(99);
            var value3    = new IntegerConstantExpression(1);
            var value4    = new IntegerConstantExpression(2);
            var expr      = new DictionaryExpression();

            expr.Add(variable1, value3);
            expr.Add(value4, variable2);

            var scope = new InterpreterScope();

            scope.AssignVariable(variable1, value1);
            scope.AssignVariable(variable2, value2);

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.True);
            Assert.That(result, Is.InstanceOf <DictionaryExpression>());
            var dictResult = (DictionaryExpression)result;

            Assert.That(dictResult.Count, Is.EqualTo(2));

            // resulting list will be sorted for quicker lookups
            Assert.That(dictResult[0].Key, Is.EqualTo(value4));
            Assert.That(dictResult[0].Value, Is.EqualTo(value2));
            Assert.That(dictResult[1].Key, Is.EqualTo(value1));
            Assert.That(dictResult[1].Value, Is.EqualTo(value3));
        }
Example #3
0
        public void TestReplaceVariablesDuplicateKeyResolved()
        {
            var variable1 = new VariableExpression("variable1");
            var variable2 = new VariableExpression("variable2");
            var value1    = new IntegerConstantExpression(1);
            var value3    = new IntegerConstantExpression(3);
            var value4    = new IntegerConstantExpression(4);
            var expr      = new DictionaryExpression();

            expr.Entries.Add(new DictionaryExpression.DictionaryEntry {
                Key = variable1, Value = value3
            });
            expr.Entries.Add(new DictionaryExpression.DictionaryEntry {
                Key = variable2, Value = value4
            });

            var scope = new InterpreterScope();

            scope.AssignVariable(variable1, value1);
            scope.AssignVariable(variable2, value1);

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.False);
            Assert.That(result, Is.InstanceOf <ParseErrorExpression>());
            Assert.That(((ParseErrorExpression)result).Message, Is.EqualTo("1 already exists in dictionary"));
        }
Example #4
0
        public void TestReplaceVariablesMethodCall()
        {
            var input     = "function func(i) { j = i }";
            var tokenizer = new PositionalTokenizer(Tokenizer.CreateTokenizer(input));

            tokenizer.Match("function");
            var functionDefinition = (FunctionDefinitionExpression)FunctionDefinitionExpression.Parse(tokenizer);

            var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { new IntegerConstantExpression(2) });
            var value1       = new IntegerConstantExpression(98);
            var expr         = new DictionaryExpression();

            expr.Entries.Add(new DictionaryExpression.DictionaryEntry {
                Key = functionCall, Value = value1
            });

            var scope = new InterpreterScope();

            scope.AddFunction(functionDefinition);

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.False);
            Assert.That(result, Is.InstanceOf <ParseErrorExpression>());
            Assert.That(((ParseErrorExpression)result).Message, Is.EqualTo("func did not return a value"));
        }
Example #5
0
        public void TestReplaceVariablesFunctionCall()
        {
            var input     = "function func(i) => 6";
            var tokenizer = new PositionalTokenizer(Tokenizer.CreateTokenizer(input));

            tokenizer.Match("function");
            var functionDefinition = (FunctionDefinitionExpression)FunctionDefinitionExpression.Parse(tokenizer);

            var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { new IntegerConstantExpression(2) });
            var value1       = new IntegerConstantExpression(98);
            var expr         = new DictionaryExpression();

            expr.Entries.Add(new DictionaryExpression.DictionaryEntry {
                Key = functionCall, Value = value1
            });

            var scope = new InterpreterScope();

            scope.AddFunction(functionDefinition);

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.True);
            Assert.That(result, Is.InstanceOf <DictionaryExpression>());
            var dictResult = (DictionaryExpression)result;

            Assert.That(dictResult.Entries.Count, Is.EqualTo(1));
            Assert.That(dictResult.Entries[0].Key, Is.EqualTo(new IntegerConstantExpression(6)));
            Assert.That(dictResult.Entries[0].Value, Is.EqualTo(value1));
        }
Example #6
0
        public void TestReplaceVariables()
        {
            var variable1 = new VariableExpression("variable1");
            var variable2 = new VariableExpression("variable2");
            var value1    = new IntegerConstantExpression(98);
            var value2    = new IntegerConstantExpression(99);
            var value3    = new IntegerConstantExpression(1);
            var value4    = new IntegerConstantExpression(2);
            var expr      = new DictionaryExpression();

            expr.Entries.Add(new DictionaryExpression.DictionaryEntry {
                Key = variable1, Value = value3
            });
            expr.Entries.Add(new DictionaryExpression.DictionaryEntry {
                Key = value4, Value = variable2
            });

            var scope = new InterpreterScope();

            scope.AssignVariable(variable1, value1);
            scope.AssignVariable(variable2, value2);

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.True);
            Assert.That(result, Is.InstanceOf <DictionaryExpression>());
            var dictResult = (DictionaryExpression)result;

            Assert.That(dictResult.Entries.Count, Is.EqualTo(2));
            Assert.That(dictResult.Entries[0].Key, Is.EqualTo(value1));
            Assert.That(dictResult.Entries[0].Value, Is.EqualTo(value3));
            Assert.That(dictResult.Entries[1].Key, Is.EqualTo(value4));
            Assert.That(dictResult.Entries[1].Value, Is.EqualTo(value2));
        }
        public void TestReplaceVariablesDuplicateKey()
        {
            var value1 = new IntegerConstantExpression(1);
            var value3 = new IntegerConstantExpression(3);
            var value4 = new IntegerConstantExpression(4);
            var expr   = new DictionaryExpression();

            expr.Add(value1, value3);
            expr.Add(value1, value4);

            var scope = new InterpreterScope();

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.False);
            Assert.That(result, Is.InstanceOf <ParseErrorExpression>());
            Assert.That(((ParseErrorExpression)result).Message, Is.EqualTo("1 already exists in dictionary"));
        }
        public void TestReplaceVariablesCreatesCopy()
        {
            var key1   = new IntegerConstantExpression(1);
            var key2   = new IntegerConstantExpression(2);
            var value1 = new StringConstantExpression("One");
            var value2 = new StringConstantExpression("Two");
            var dict1  = new DictionaryExpression();

            dict1.Add(key1, value1);
            dict1.Add(key2, value2);

            var scope = new InterpreterScope();

            ExpressionBase result;

            Assert.That(dict1.ReplaceVariables(scope, out result), Is.True);
            Assert.That(result, Is.InstanceOf <DictionaryExpression>());
            var dict2 = (DictionaryExpression)result;

            Assert.That(dict2.Count, Is.EqualTo(2));

            // item added to original dictionary does not appear in evaluated dictionary
            var key3   = new IntegerConstantExpression(3);
            var value3 = new StringConstantExpression("Three");

            dict1.Add(key3, value3);
            Assert.That(dict1.Count, Is.EqualTo(3));
            Assert.That(dict2.Count, Is.EqualTo(2));

            // item added to evaluated dictionary does not appear in original dictionary
            var key4   = new IntegerConstantExpression(4);
            var value4 = new StringConstantExpression("Four");

            dict2.Add(key4, value4);
            Assert.That(dict1.Count, Is.EqualTo(3));
            Assert.That(dict2.Count, Is.EqualTo(3));

            Assert.That(dict1.Keys.Contains(key3));
            Assert.That(!dict2.Keys.Contains(key3));
            Assert.That(!dict1.Keys.Contains(key4));
            Assert.That(dict2.Keys.Contains(key4));
        }
        public void TestReplaceVariablesMemoryAccessor()
        {
            var key   = new IntegerConstantExpression(6);
            var value = new FunctionCallExpression("byte", new[] { new IntegerConstantExpression(1) });
            var expr  = new DictionaryExpression();

            expr.Add(key, value);

            var scope = new InterpreterScope(AchievementScriptInterpreter.GetGlobalScope());

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.True);

            Assert.That(result, Is.InstanceOf <DictionaryExpression>());
            var arrayResult = (DictionaryExpression)result;

            Assert.That(arrayResult.Count, Is.EqualTo(1));
            Assert.That(arrayResult[0].Value.ToString(), Is.EqualTo(value.ToString()));
        }
        public void TestReplaceVariablesLogicalFunctionCall()
        {
            var functionDefinition = UserFunctionDefinitionExpression.ParseForTest("function func(i) => byte(i) == 1");

            var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { new IntegerConstantExpression(2) });
            var value1       = new IntegerConstantExpression(98);
            var expr         = new DictionaryExpression();

            expr.Add(functionCall, value1);

            var scope = new InterpreterScope(AchievementScriptInterpreter.GetGlobalScope());

            scope.AddFunction(functionDefinition);

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.False);
            Assert.That(result, Is.InstanceOf <ParseErrorExpression>());
            Assert.That(((ParseErrorExpression)result).Message, Is.EqualTo("Dictionary key must evaluate to a constant"));
        }
        public void TestReplaceVariablesFunctionCall()
        {
            var functionDefinition = UserFunctionDefinitionExpression.ParseForTest("function func(i) => 6");

            var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { new IntegerConstantExpression(2) });
            var value1       = new IntegerConstantExpression(98);
            var expr         = new DictionaryExpression();

            expr.Add(functionCall, value1);

            var scope = new InterpreterScope();

            scope.AddFunction(functionDefinition);

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.True);
            Assert.That(result, Is.InstanceOf <DictionaryExpression>());
            var dictResult = (DictionaryExpression)result;

            Assert.That(dictResult.Count, Is.EqualTo(1));
            Assert.That(dictResult[0].Key, Is.EqualTo(new IntegerConstantExpression(6)));
            Assert.That(dictResult[0].Value, Is.EqualTo(value1));
        }