Exemple #1
0
        public void ShouldThrowParseExceptionForWrongOperator()
        {
            //Given
            ONPCalculator calculator = new ONPCalculator(ArtmeticOperator.Add(), ArtmeticOperator.Sub());
            //When - Then
            var ex = Assert.Throws <ParseException>(() => calculator.Perform("5 4 *"));

            Assert.That(ex.Message, Is.EqualTo("Operator wasn't found."));
        }
Exemple #2
0
        public void ShouldThrowParseExceptionForWrongSyntax()
        {
            //Given
            ONPCalculator calculator = new ONPCalculator(ArtmeticOperator.Add(), ArtmeticOperator.Sub());
            //When - Then
            var ex = Assert.Throws <ParseException>(() => calculator.Perform("5 +"));

            Assert.That(ex.Message, Is.EqualTo("Invalid expression."));
        }
Exemple #3
0
        public void ShouldCalculateComplexExpression()
        {
            //Given
            ONPCalculator calculator = new ONPCalculator(ArtmeticOperator.Add(), ArtmeticOperator.Sub());
            //When
            double result = calculator.Perform("4 5 + 2 - 1 +");

            //Then
            Assert.That(result, Is.EqualTo(8));
        }
Exemple #4
0
        public void ShouldParseStringWithManySpaces()
        {
            //Given
            ONPCalculator calculator = new ONPCalculator(ArtmeticOperator.Add(), ArtmeticOperator.Sub());
            //When
            double result = calculator.Perform("4    5    +");

            //Then
            Assert.That(result, Is.EqualTo(9));
        }
Exemple #5
0
        public void ShouldAddTwoNumbers(double number1, double number2, string @operator, double expectedResult)
        {
            //Given
            ONPCalculator calculator = new ONPCalculator(ArtmeticOperator.Add(), ArtmeticOperator.Sub());
            //When
            double result = calculator.Perform($"{number1} {number2} {@operator}");

            //Then
            Assert.That(result, Is.EqualTo(expectedResult));
        }
Exemple #6
0
        public void ShouldCalculateAfterException()
        {
            //Given
            ONPCalculator calculator = new ONPCalculator(ArtmeticOperator.Add(), ArtmeticOperator.Sub());

            //When
            Assert.Throws <ParseException>(() => calculator.Perform("4 *"));
            double result = calculator.Perform("4 2 +");

            //Then
            Assert.That(result, Is.EqualTo(6));
        }
Exemple #7
0
        public void ShouldCalculateMoreThanOnce()
        {
            //Given
            ONPCalculator calculator = new ONPCalculator(ArtmeticOperator.Add(), ArtmeticOperator.Sub());
            //When
            double firstCalculation  = calculator.Perform("7 3 +");
            double secondCalculation = calculator.Perform("7 3 -");

            //Then
            Assert.That(firstCalculation, Is.EqualTo(10));
            Assert.That(secondCalculation, Is.EqualTo(4));
        }