Exemple #1
0
        public void EvaluateShouldThrowTheExpectedExceptionIfOnlyOperandIsSpecified()
        {
            var factory = new Mock<IOperationFactory>();
            var calculator = new Calculator(factory.Object);

            var expression = new[] { "+" };

            Assert.Throws<Exception>(() => calculator.Evaluate(expression));
        }
Exemple #2
0
        public void EvaluateShouldThrowTheExpectedExceptionIfTwoOperandsAreSpecifiedAfterConstant()
        {
            var factory = new Mock<IOperationFactory>();
            var calculator = new Calculator(factory.Object);

            var expression = new[] { "10", "+", "+" };

            Assert.Throws<Exception>(() => calculator.Evaluate(expression));
        }
Exemple #3
0
        public void EvaluateShouldReturnConstantIfOnlyConstantIsSpecified()
        {
            var factory = new Mock<IOperationFactory>();
            var calculator = new Calculator(factory.Object);

            var expression = new[] { Value1.ToString() };

            Assert.AreEqual(Value1, calculator.Evaluate(expression));
        }
Exemple #4
0
        public void EvaluateShouldWorkCorrectly()
        {
            const int expectedResult1 = 30;
            const int expectedResult = 90;
            const string token1 = "+";
            const string token2 = "*";

            // Here you can test the internal algorithm and expected behavior
            // up to the way that methods should be called in the expected order
            // on mocks.
            // Though - be very careful. As you can see the test setup may grow
            // very quickly which implies that future maintenance may be very difficult.
            var sequence = new MockSequence();

            var factory = new Mock<IOperationFactory>(MockBehavior.Strict);
            var operation1 = new Mock<IOperation>(MockBehavior.Strict);
            var operation2 = new Mock<IOperation>(MockBehavior.Strict);

            factory.InSequence(sequence).Setup(f => f.Create(token2)).Returns(operation2.Object).Verifiable();
            factory.InSequence(sequence).Setup(f => f.Create(token1)).Returns(operation1.Object).Verifiable();

            operation1.InSequence(sequence).Setup(o => o.Evaluate(10, 10)).Returns(expectedResult1).Verifiable();
            operation2.InSequence(sequence).Setup(o => o.Evaluate(expectedResult1, 30)).Returns(expectedResult).Verifiable();

            var calculator = new Calculator(factory.Object);

            var expression = new[] { "10", token1, "10", token2, "30" };

            Assert.AreEqual(expectedResult, calculator.Evaluate(expression));

            factory.VerifyAll();
            operation1.VerifyAll();
            operation2.VerifyAll();
        }