コード例 #1
0
        public void ComplexExpressionCorrectCalculation()
        {
            var operationProvider = new BasicOperationProvider();

            var converter = new PostfixConverterSortStation(operationProvider);
            var executor = new PostfixExecutor(operationProvider);

            var input = @"4 * 5 - 27 / 3";

            var calculator = new Calculator(converter, executor);
            var result = calculator.Calculate(input);

            Assert.Equal(11, result);
        }
コード例 #2
0
        public void TwoNumbersAdditionCorrectResult()
        {
            var postfix = @"2 2 +";

            var provider = new Mock<IOperationProvider>();
            provider.Setup(m => m.IsOperation(@"+")).Returns(true);
            provider.Setup(m => m.GetOperation(@"+")).Returns(new AddOperation());

            var executor = new PostfixExecutor(provider.Object);

            var result = executor.Execute(postfix);

            Assert.Equal(4, result);
        }
コード例 #3
0
        public void DivisionByZeroExceptionThrow()
        {
            var operationProvider = new BasicOperationProvider();

            var converter = new PostfixConverterSortStation(operationProvider);
            var executor = new PostfixExecutor(operationProvider);

            var input = @"13 / 0";

            var calculator = new Calculator(converter, executor);

            Assert.Throws(typeof(DivideByZeroException), () =>
            {
                calculator.Calculate(input);
            });
        }
コード例 #4
0
        public void MissingOperandExpressionExceptionThrow()
        {
            var operationProvider = new BasicOperationProvider();

            var converter = new PostfixConverterSortStation(operationProvider);
            var executor = new PostfixExecutor(operationProvider);

            var input = @"2 + + 7";

            var calculator = new Calculator(converter, executor);

            Assert.Throws(typeof(IncorrectExpressionException), () =>
            {
                calculator.Calculate(input);
            });
        }
コード例 #5
0
        public void ComplexExpressionCorrectResult()
        {
            var postfix = @"4 6 2 / +";

            var provider = new Mock<IOperationProvider>();
            provider.Setup(m => m.IsOperation(@"/")).Returns(true);
            provider.Setup(m => m.IsOperation(@"+")).Returns(true);
            provider.Setup(m => m.GetOperation(@"/")).Returns(new DivOperation());
            provider.Setup(m => m.GetOperation(@"+")).Returns(new AddOperation());

            var executor = new PostfixExecutor(provider.Object);

            var result = executor.Execute(postfix);

            Assert.Equal(7, result);
        }