Пример #1
0
        [InlineData(0, "Cannot divide by zero.", "2", "/", "0", "+")]                                   // Chained operation error
        public void TestCalculatorInputResults(decimal expectedDecimalResult, string expectedErrorResult, params string[] inputs)
        {
            //Arrange
            var calculator = new Calculator.CalculatorEngine.Implementations.Calculator(CalculatorStateFactoryMock.Object);
            Validated <decimal> lastValidatedResult = null;

            //Act
            foreach (var input in inputs)
            {
                lastValidatedResult = resultOfSubmittedInput(input);
            }

            //Assert
            lastValidatedResult.Should().Be(expectedValidatedResult());

            //Local functions
            Validated <decimal> resultOfSubmittedInput(string input)
            => input.Equals("+") ? calculator.SubmitOperationInputAndGetResult(additionOperation())
                    : input.Equals("/") ? calculator.SubmitOperationInputAndGetResult(divisionOperation())
                    : input.Equals("SquareRoot") ? calculator.SubmitOperationInputAndGetResult(squareRootOperation())
                    : input.Equals("PI") ? calculator.SubmitOperationInputAndGetResult(piOperation())
                    : input.Equals("i") ? calculator.SubmitOperationInputAndGetResult(iOperation())
                    : input.Equals("CompoundInterest") ? calculator.SubmitOperationInputAndGetResult(compoundInterestOperation())
                    : input.Equals("=") ? calculator.SubmitEqualsRequestAndGetResult()
                    : calculator.SubmitValueInputAndGetResult(decimal.Parse(input));
            IOperation additionOperation()
            => Operation(2, new Func <IEnumerable <decimal>, Validated <decimal> >((operands)
                                                                                   => new Validated <decimal>(operands.ToList()[0] + operands.ToList()[1])));
            IOperation divisionOperation()
            => Operation(2, new Func <IEnumerable <decimal>, Validated <decimal> >((operands)
                                                                                   => operands.ToList()[1] != 0
                        ? new Validated <decimal>(operands.ToList()[0] / operands.ToList()[1])
                        : new StringError("Cannot divide by zero.")));
            IOperation squareRootOperation()
            => Operation(1, new Func <IEnumerable <decimal>, Validated <decimal> >((operands)
                                                                                   => operands.ToList()[0] >= 0
                        ? new Validated <decimal>((decimal)Math.Sqrt((double)operands.ToList()[0]))
                        : new StringError("Cannot square root a negative.")));
            IOperation piOperation()
            => Operation(0, new Func <IEnumerable <decimal>, Validated <decimal> >((operands)
                                                                                   => new Validated <decimal>(3.14M)));
            IOperation iOperation()
            => Operation(0, new Func <IEnumerable <decimal>, Validated <decimal> >((operands)
                                                                                   => new StringError("Imaginary numbers are not valid.")));
            IOperation compoundInterestOperation()
            => Operation(3, new Func <IEnumerable <decimal>, Validated <decimal> >((operands)
                                                                                   => new Validated <decimal>(operands.ToList()[0] * (decimal)Math.Pow((double)(1 + operands.ToList()[1]), (double)operands.ToList()[2]))));
            Validated <decimal> expectedValidatedResult()
            => expectedErrorResult == null
                    ? new Validated <decimal>(expectedDecimalResult)
                    : new StringError(expectedErrorResult);
        }