/// <summary> /// Limits of int32 apply, ranging -2,147,483,648 to 2,147,483,647 /// </summary> private static int GetUserNumericalInput() { int value; while (!int.TryParse(Input.GetUserInput(), out value)) { Output.PrintUserOutput("Please Enter a valid numerical value!"); } return(value); }
public void GetUserNumericalInputReturnsIntegerWhenInputIsInteger() { // Arrange IGetInput InputMock = Substitute.For <IGetInput>(); InputMock.GetUserInput().Returns(x => "5"); int expected = 5; Target.Input = InputMock; // Act var result = RunHelper.RunStaticMethod(typeof(CalcConsoleClient), "GetUserNumericalInput", null); // Assert Assert.AreEqual(expected, result); }
public void GetUserNumericalInputPrintsErrorMessageWhenInputIsOtherThanInteger() { // Arrange IGetInput InputMock = Substitute.For <IGetInput>(); IPrintOutput OutputMock = Substitute.For <IPrintOutput>(); // As the console stays in a loop if an invallid call is received, we are forcing a return with a second valid call InputMock.GetUserInput().Returns("h", "3"); Target.Input = InputMock; Target.Output = OutputMock; // Act var result = RunHelper.RunStaticMethod(typeof(CalcConsoleClient), "GetUserNumericalInput", null); // Assert OutputMock.Received().PrintUserOutput("Please Enter a valid numerical value!"); }
public void GetUserOperationInputOnlyAcceptsValidOperations() { // Arrange IGetInput InputMock = Substitute.For <IGetInput>(); IPrintOutput OutputMock = Substitute.For <IPrintOutput>(); // As the console stays in a loop if an invallid call is received, we are forcing a return with a second valid call InputMock.GetUserInput().Returns("hello", "add"); Target.Input = InputMock; Target.Output = OutputMock; // Act var result = RunHelper.RunStaticMethod(typeof(CalcConsoleClient), "GetUserOperationInput", null); // Assert // Failed 4 times, one per invalid input OutputMock.Received(1); OutputMock.Received().PrintUserOutput("Please Enter a valid operation!"); }
public void GetUserNumericalInputPrintsErrorIfIntIsOutOfRange() { // Arrange IGetInput InputMock = Substitute.For <IGetInput>(); IPrintOutput OutputMock = Substitute.For <IPrintOutput>(); // As the console stays in a loop if an invalid call is received, we are forcing a return with a second valid call InputMock.GetUserInput().Returns("2147483999", "4"); Target.Input = InputMock; Target.Output = OutputMock; // Act var result = RunHelper.RunStaticMethod(typeof(CalcConsoleClient), "GetUserNumericalInput", null); // Assert // Failed 4 times, one per invalid input OutputMock.Received(1); OutputMock.Received().PrintUserOutput("Please Enter a valid numerical value!"); }
public void GetUserNumericalInputBlocksExecutionUntilValidValueIsFound() { // Arrange IGetInput InputMock = Substitute.For <IGetInput>(); IPrintOutput OutputMock = Substitute.For <IPrintOutput>(); // As the console stays in a loop if an invallid call is received, we are forcing a return with a second valid call InputMock.GetUserInput().Returns("a", "b", "c", "&", "3"); Target.Input = InputMock; Target.Output = OutputMock; // Act var result = RunHelper.RunStaticMethod(typeof(CalcConsoleClient), "GetUserNumericalInput", null); // Assert // Failed 4 times, one per invalid input OutputMock.Received(4); OutputMock.Received().PrintUserOutput("Please Enter a valid numerical value!"); }