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")); }
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: (+, -, *, /)")); }
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()); }
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")); } }
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); } }
public void Positive(string expression, int expectedResult) { var actualResult = CalculatorExecutor.Calculate(expression); actualResult.Should().Be(expectedResult); }