Пример #1
0
        public void VisitDivisionExpressionReturnsCorrectResult(int x, int y, EquationPrintingVisitor sut)
        {
            var left     = new Constant(x);
            var right    = new Constant(y);
            var division = new DivisionExpression(left, right);

            sut.Visit(division);
            var expected = $"({x} / {y})";

            Assert.Equal(expected, sut.Equation);
        }
Пример #2
0
        public void VisitAssignmentReturnsCorrectResult(int y, EquationPrintingVisitor sut)
        {
            var variableName = "x";
            var left         = new Variable(variableName);
            var right        = new Constant(y);
            var assignment   = new Assignment(left, right);

            sut.Visit(assignment);
            var expected = $"{variableName} = {y}";

            Assert.Equal(expected, sut.Equation);
        }
Пример #3
0
        static void Calculate()
        {
            //a=5*b+2
            var expression = new Assignment(new Variable("a"),
                                            new AdditionExpression(new MultiplicationExpression(new Constant(5),
                                                                                                new Variable("b", 2)),
                                                                   new Constant(2)));


            var equationPrintingVisitor = new EquationPrintingVisitor();

            equationPrintingVisitor.Visit(expression);

            var calculationVisitor = new CalculationVisitor();

            calculationVisitor.Visit(expression);

            Console.WriteLine($"Expression: {equationPrintingVisitor.Equation}");
            Console.WriteLine($"The result is: {calculationVisitor.Result}");
        }
Пример #4
0
 public void VisitConstantReturnsCorrectResult(Constant constant, EquationPrintingVisitor sut)
 {
     sut.Visit(constant);
     Assert.Equal(constant.Value.ToString(), sut.Equation);
 }
Пример #5
0
 public void VisitVariableReturnsCorrectResult(Variable variable, EquationPrintingVisitor sut)
 {
     sut.Visit(variable);
     Assert.Equal(variable.Name, sut.Equation);
 }