예제 #1
0
        public void ShouldApplyOperatorRaiseExceptionWhenStackEmpty()
        {
            CalculatorContainer container = new CalculatorContainer();
            CalculatorExecutor  executor  = new CalculatorExecutor();

            container.PushItem(5);

            InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => executor.ApplyOperator(container, "+"));

            Assert.That(ex.Message, Is.EqualTo("The stack must have at least two items to apply an operator"));
        }
예제 #2
0
        public void ShouldApplyOperatorRaiseExceptionWhenOperatorInvalid()
        {
            CalculatorContainer container = new CalculatorContainer();
            CalculatorExecutor  executor  = new CalculatorExecutor();

            container.PushItem(5);
            container.PushItem(5);
            container.PushItem(5);

            InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => executor.ApplyOperator(container, ":"));

            Assert.That(ex.Message, Is.EqualTo("The symbol ':' is not recognised as a valid operator. Accepted operators are: (+, -, *, /)"));
        }
예제 #3
0
        public void ShouldApplyOperatorSuccefully()
        {
            CalculatorContainer container = new CalculatorContainer();
            CalculatorExecutor  executor  = new CalculatorExecutor();

            container.PushItem(5);
            container.PushItem(7);
            container.PushItem(8);
            executor.ApplyOperator(container, "+");
            Assert.AreEqual("5;15", container.PrettyDisplay());
            container.PushItem(10);
            executor.ApplyOperator(container, "-");
            Assert.AreEqual("5;-5", container.PrettyDisplay());
            executor.ApplyOperator(container, "*");
            Assert.AreEqual("-25", container.PrettyDisplay());
        }
예제 #4
0
        public void DividingByZeroShouldBeHandledByCode()
        {
            var calculatorItems = new CalculatorItems
            {
                firstNumberText  = "1",
                secondNumberText = "0",
                operationText    = "/"
            };

            try
            {
                var returnedCalculatorItems = new CalculatorExecutor(calculatorItems).Run();
            }
            catch (Exception exception)
            {
                Assert.IsTrue(exception.Message.Contains("0-val"));
            }
        }
예제 #5
0
        public void AddingTwoNumbersShouldReturnCorrectResult()
        {
            var calculatorItems = new CalculatorItems
            {
                firstNumberText  = "33333.3333",
                secondNumberText = "77777.7777",
                operationText    = "+"
            };

            var expectedResult = Convert.ToDouble(calculatorItems.firstNumberText) +
                                 Convert.ToDouble(calculatorItems.secondNumberText);

            try
            {
                var returnedCalculatorItems = new CalculatorExecutor(calculatorItems).Run();
                Assert.IsTrue(Math.Abs(returnedCalculatorItems.result - expectedResult) <= double.Epsilon);
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }
예제 #6
0
        public void Positive(string expression, int expectedResult)
        {
            var actualResult = CalculatorExecutor.Calculate(expression);

            actualResult.Should().Be(expectedResult);
        }