public void MathExpressionContext_Can_Define_And_Resolve_Expression() { MathExpressionContext context = new MathExpressionContext(); context.Define("x", 5); context.Define("y", MathExpression.Parse("5x")); Assert.AreEqual(25, context.Resolve("y").Evaluate(context)); }
public void MathExpressionContext_Cannot_Define_Variable_That_Implicitly_Refers_To_Itself() { MathExpressionContext context = new MathExpressionContext(); context.Define("x", 5); context.Define("y", MathExpression.Parse("5x")); var exception = Assert.Throws<MathExpressionException>(() => context.Define("x", MathExpression.Parse("y"))); Assert.AreEqual("Variable 'x' is refering to itself recursively.", exception.Message); }
public void MathExpressionContext_Can_Forget_Variable() { MathExpressionContext context = new MathExpressionContext(); context.Define("x", 42); Assert.AreEqual(42, context.Resolve("x").Evaluate(context)); context.Forget("x"); var exception = Assert.Throws<MathExpressionException>(() => context.Resolve("x")); Assert.AreEqual("The variable 'x' has not been defined.", exception.Message); }
public void MathExpressionParser_Can_Parse_And_Evaluate_Variable() { var input = this.GetPostfixTokenStream("x"); MathExpressionParser parser = new MathExpressionParser(); MathExpression expression = parser.Parse(input); MathExpressionContext context = new MathExpressionContext(); context.Define("x", 42); Assert.AreEqual(42D, expression.Evaluate(context)); }
public void MathExpressionContext_Can_Redefine_Variable() { MathExpressionContext context = new MathExpressionContext(); context.Define("x", 42); Assert.AreEqual(42, context.Resolve("x").Evaluate(context)); context.Define("x", 107); Assert.AreEqual(107, context.Resolve("x").Evaluate(context)); }
public void MathExpressionContext_Variable_Declarations_Can_Occur_In_Any_Order() { MathExpression expression = MathExpression.Parse("10+x"); MathExpressionContext context = new MathExpressionContext(); context.Define("x", MathExpression.Parse("5y")); context.Define("y", 3); double result = expression.Evaluate(context); Assert.AreEqual(25, result); }
public void MathExpression_Can_Evaluate_Variable_Expression() { var expression = new VariableExpression("x"); var visitor = new EvaluationVisitor(); var context = new MathExpressionContext(); context.Define("x", 42D); Assert.AreEqual(42, visitor.Evaluate(expression, context)); }