コード例 #1
0
 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));
 }
コード例 #2
0
 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);
 }
コード例 #3
0
 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);
 }
コード例 #4
0
 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));
 }
コード例 #5
0
 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));
 }
コード例 #6
0
 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);
 }
コード例 #7
0
 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));
 }