public void Results_ShouldHaveAllCalculatedResults()
        {
            List <EquationCalculation> equations = EquationsToBeStored;
            var calucaltorModel = new CalculatorModel();

            foreach (EquationCalculation data in equations)
            {
                calucaltorModel.CalculateFromText(data.Equation);
            }

            for (int i = 0, j = 1, count = equations.Count; i < count; i++, j++)
            {
                // An equation is trimmed before saved in the history in the calculator model.
                string trimedEquation = equations[i].Equation.Trim();

                Assert.Equal(equations[i].Result, calucaltorModel.Results[count - j].Result);
                Assert.Equal(trimedEquation, calucaltorModel.Results[count - j].Equation);
            }

            try
            {
                calucaltorModel.CalculateFromText(null);
            }
            catch (ArgumentNullException)
            {
                // Error provoked to test if invalid equation is not added to history.
            }

            Assert.Equal(3, calucaltorModel.Results.Count);
        }
        public void CalculateFromText_ShouldCalculateWithRadians()
        {
            var calculator = new CalculatorModel()
            {
                UsesRadians = true
            };

            double actualResult = calculator.CalculateFromText("sin(2.0)");

            Assert.Equal(0.91, Math.Round(actualResult, 2));

            actualResult = calculator.CalculateFromText("cotan(0.5)");

            Assert.Equal(0.46, Math.Round(actualResult, 2));
        }
        public void CalculateFromText_ShouldThrowExceptionForEmptyString()
        {
            var calculator = new CalculatorModel();

            Assert.Throws <ArgumentException>(
                () => calculator.CalculateFromText(String.Empty)
                );
        }
        public void CalculateFromText_ShouldThrowExceptionForNull()
        {
            var calculator = new CalculatorModel();

            Assert.Throws <ArgumentNullException>(
                () => calculator.CalculateFromText(null)
                );
        }
        public void CalculateFromText_ShouldThrowExceptionForTooBigNbr(
            string invalidEquation
            )
        {
            var calculator = new CalculatorModel();

            Assert.Throws <OverflowException>(
                () => calculator.CalculateFromText(invalidEquation)
                );
        }
        public void CalculateFromText_ShouldThrowExceptionZeroDivition(
            string invalidEquation
            )
        {
            var calculator = new CalculatorModel();

            Assert.Throws <DivideByZeroException>(
                () => calculator.CalculateFromText(invalidEquation)
                );
        }
        public void RoundingPrecision_ShouldReturnResultWithRoundedDigits()
        {
            var calculator = new CalculatorModel()
            {
                RoundingPrecision = 3
            };
            double actualResult = calculator.CalculateFromText("1/3");

            Assert.Equal(0.333, actualResult);
        }
        public void FractionFromCurrentResult_ShouldReturnFractionalPart(
            string text,
            double expectedResult
            )
        {
            var calculator = new CalculatorModel();

            calculator.CalculateFromText(text);
            Assert.Equal(expectedResult, calculator.FractionFromCurrentResult);
        }
        public void CalculateFromText_ShouldReturnExactResult(
            string text,
            double expectedResult
            )
        {
            var    calculator   = new CalculatorModel();
            double actualResult = calculator.CalculateFromText(text);

            Assert.Equal(expectedResult, actualResult);
        }
        public void CalculateFromText_ShouldReturnRoundedResult(
            string text,
            double expectedResult,
            int fractionalDigits
            )
        {
            var    calculator   = new CalculatorModel();
            double actualResult = calculator.CalculateFromText(text);

            actualResult = Math.Round(actualResult, fractionalDigits);
            Assert.Equal(expectedResult, actualResult);
        }
        public void CalculateFromText_ShouldThrowExceptionForMathematicalError(
            string invalidEquation, MathematicalError expectedErrorType
            )
        {
            var calculator = new CalculatorModel();
            CalculationParseMathematicalException thrownException =
                Assert.Throws <CalculationParseMathematicalException>(
                    () => calculator.CalculateFromText(invalidEquation)
                    );
            MathematicalError actualErrorType = thrownException.MathematicalErrorType;

            Assert.Equal(expectedErrorType, actualErrorType);
        }
        public void ClearHistory_ShouldHaveNoResultsAfterClearHistory()
        {
            List <EquationCalculation> equations = EquationsToBeStored;
            var calucaltorModel = new CalculatorModel();

            foreach (EquationCalculation data in equations)
            {
                calucaltorModel.CalculateFromText(data.Equation);
            }

            calucaltorModel.ClearHistory();

            Assert.Empty(calucaltorModel.Results);
        }
        public void MaxNumberOfResults_ShouldNotMoreThanMaxNumber()
        {
            List <EquationCalculation> equations = EquationsToBeStored;
            var calucaltorModel = new CalculatorModel();


            foreach (EquationCalculation data in equations)
            {
                calucaltorModel.CalculateFromText(data.Equation);
            }

            calucaltorModel.MaxNumberOfResult = 1;

            Assert.Single <EquationCalculation>(calucaltorModel.Results);

            Assert.Equal("2", calucaltorModel.Results[0].Result);
        }
        public void UsesPointAsDecimalSeperator_ShouldCalculateWithCommas()
        {
            var calculator = new CalculatorModel()
            {
                UsesPointAsDecimalSeperator = false
            };

            double actualResult = calculator.CalculateFromText("2,6 + 2,5");

            Assert.Equal(5.1, actualResult);

            var expectedCalculation = new EquationCalculation("5,1", "2,6 + 2,5");

            EquationCalculation actualCalculation = calculator.Results[0];

            Assert.Equal(
                expectedCalculation.Result, actualCalculation.Result
                );

            Assert.Equal(
                expectedCalculation.Equation, actualCalculation.Equation
                );
        }