private List <Requirement> Evaluate(string input, string expectedError = null) { var requirements = new List <Requirement>(); var funcDef = new OnceFunction(); var expression = ExpressionBase.Parse(new PositionalTokenizer(Tokenizer.CreateTokenizer(input))); Assert.That(expression, Is.InstanceOf <FunctionCallExpression>()); var funcCall = (FunctionCallExpression)expression; ExpressionBase error; var scope = funcCall.GetParameters(funcDef, AchievementScriptInterpreter.GetGlobalScope(), out error); var context = new TriggerBuilderContext { Trigger = requirements }; scope.Context = context; ExpressionBase evaluated; Assert.That(funcDef.ReplaceVariables(scope, out evaluated), Is.True); if (expectedError == null) { Assert.That(funcDef.BuildTrigger(context, scope, funcCall), Is.Null); } else { var parseError = funcDef.BuildTrigger(context, scope, funcCall); Assert.That(parseError, Is.Not.Null); Assert.That(parseError.Message, Is.EqualTo(expectedError)); } return(requirements); }
public void TestFunctionReference() { string input = "once(f)"; var requirements = new List <Requirement>(); var funcDef = new OnceFunction(); var expression = ExpressionBase.Parse(new PositionalTokenizer(Tokenizer.CreateTokenizer(input))); Assert.That(expression, Is.InstanceOf <FunctionCallExpression>()); var funcCall = (FunctionCallExpression)expression; var scope = new InterpreterScope(AchievementScriptInterpreter.GetGlobalScope()); scope.AssignVariable(new VariableExpression("f"), new FunctionReferenceExpression("f2")); ExpressionBase error; scope = funcCall.GetParameters(funcDef, scope, out error); var context = new TriggerBuilderContext { Trigger = requirements }; scope.Context = context; ExpressionBase evaluated; Assert.That(funcDef.ReplaceVariables(scope, out evaluated), Is.True); funcCall = evaluated as FunctionCallExpression; var parseError = funcDef.BuildTrigger(context, scope, funcCall); Assert.That(parseError, Is.Not.Null); Assert.That(parseError.InnermostError.Message, Is.EqualTo("Function used like a variable")); }
public void TestDefinition() { var def = new OnceFunction(); Assert.That(def.Name.Name, Is.EqualTo("once")); Assert.That(def.Parameters.Count, Is.EqualTo(1)); Assert.That(def.Parameters.ElementAt(0).Name, Is.EqualTo("comparison")); }
public void TestExplicitCall() { // not providing a TriggerBuilderContext simulates calling the function at a global scope var funcDef = new OnceFunction(); var input = "once(byte(0x1234) == 56)"; var expression = ExpressionBase.Parse(new PositionalTokenizer(Tokenizer.CreateTokenizer(input))); Assert.That(expression, Is.InstanceOf <FunctionCallExpression>()); var funcCall = (FunctionCallExpression)expression; ExpressionBase error; var scope = funcCall.GetParameters(funcDef, AchievementScriptInterpreter.GetGlobalScope(), out error); Assert.That(funcDef.Evaluate(scope, out error), Is.False); Assert.That(error, Is.InstanceOf <ParseErrorExpression>()); Assert.That(((ParseErrorExpression)error).Message, Is.EqualTo("once has no meaning outside of a trigger clause")); }