import org.junit.Test; import p20230510_TestConsoleIO.GuessTheNumber; import p20230510_TestConsoleIO.library.MockRandomSingleValue; import p20230510_TestConsoleIO.library.SystemInputOutputTester; import java.io.IOException; public class GuessTheNumberTest { @Test public void testHighsAndLowsTo33() throws IOException { final var systemNumberWeAreTryingToGuess = 33; try (final var mockRandomNumberGenerator = new MockRandomSingleValue(systemNumberWeAreTryingToGuess)) { SystemInputOutputTester .whenCallingThisMethod(() -> { GuessTheNumber.main(null); }) .assertTextOutput("Welcome to the number guessing program.") // .assertTextOutput("I am thinking of a number in the range of 1 to 100, inclusive.") .assertTextOutput("You need to guess this number, with a minimum number of guesses.") .assertTextOutput("Each time you guess, I will tell you if my number is HIGHER or LOWER than your guess.") // .assertTextOutput("What is your guess?") .provideLineInput("50") .assertTextOutput("Your guess of 50 is TOO HIGH.") // .assertTextOutput("What is your guess?") .provideLineInput("25") .assertTextOutput("Your guess of 25 is TOO LOW.") // .assertTextOutput("What is your guess?") .provideLineInput("37") .assertTextOutput("Your guess of 37 is TOO HIGH.") // .assertTextOutput("What is your guess?") .provideLineInput("31") .assertTextOutput("Your guess of 31 is TOO LOW.") // .assertTextOutput("What is your guess?") .provideLineInput("34") .assertTextOutput("Your guess of 34 is TOO HIGH.") // .assertTextOutput("What is your guess?") .provideLineInput("32") .assertTextOutput("Your guess of 32 is TOO LOW.") // .assertTextOutput("What is your guess?") .provideLineInput("33") .assertTextOutput("Your guess of 33 is CORRECT in 7 guesses!") // .assertThatTheMethodReturnsHere(); } } @Test public void testMaximumGuesses() throws IOException { final var systemNumberWeAreTryingToGuess = 100; try (final var mockRandomNumberGenerator = new MockRandomSingleValue(systemNumberWeAreTryingToGuess)) { SystemInputOutputTester .whenCallingThisMethod(() -> { GuessTheNumber.main(null); }) .assertTextOutput("Welcome to the number guessing program.") // .assertTextOutput("I am thinking of a number in the range of 1 to 100, inclusive.") .assertTextOutput("You need to guess this number, with a minimum number of guesses.") .assertTextOutput("Each time you guess, I will tell you if my number is HIGHER or LOWER than your guess.") // .assertTextOutput("What is your guess?") .provideLineInput("50") .assertTextOutput("Your guess of 50 is TOO LOW.") // .assertTextOutput("What is your guess?") .provideLineInput("75") .assertTextOutput("Your guess of 75 is TOO LOW.") // .assertTextOutput("What is your guess?") .provideLineInput("88") .assertTextOutput("Your guess of 88 is TOO LOW.") // .assertTextOutput("What is your guess?") .provideLineInput("94") .assertTextOutput("Your guess of 94 is TOO LOW.") // .assertTextOutput("What is your guess?") .provideLineInput("97") .assertTextOutput("Your guess of 97 is TOO LOW.") // .assertTextOutput("What is your guess?") .provideLineInput("99") .assertTextOutput("Your guess of 99 is TOO LOW.") // .assertTextOutput("What is your guess?") .provideLineInput("100") .assertTextOutput("Your guess of 100 is CORRECT in 7 guesses!") // .assertThatTheMethodReturnsHere(); } } @Test public void testFirstGuessIsCorrect() throws IOException { final var systemNumberWeAreTryingToGuess = 24; try (final var mockRandomNumberGenerator = new MockRandomSingleValue(systemNumberWeAreTryingToGuess)) { SystemInputOutputTester .whenCallingThisMethod(() -> { GuessTheNumber.main(null); }) .assertTextOutput("Welcome to the number guessing program.") // .assertTextOutput("I am thinking of a number in the range of 1 to 100, inclusive.") .assertTextOutput("You need to guess this number, with a minimum number of guesses.") .assertTextOutput("Each time you guess, I will tell you if my number is HIGHER or LOWER than your guess.") // .assertTextOutput("What is your guess?") .provideLineInput("24") .assertTextOutput("Your guess of 24 is CORRECT in one guess!!!") // .assertThatTheMethodReturnsHere(); } } @Test public void testSecondGuessIsCorrect() throws IOException { final var systemNumberWeAreTryingToGuess = 33; try (final var mockRandomNumberGenerator = new MockRandomSingleValue(systemNumberWeAreTryingToGuess)) { SystemInputOutputTester .whenCallingThisMethod(() -> { GuessTheNumber.main(null); }) .assertTextOutput("Welcome to the number guessing program.") // .assertTextOutput("I am thinking of a number in the range of 1 to 100, inclusive.") .assertTextOutput("You need to guess this number, with a minimum number of guesses.") .assertTextOutput("Each time you guess, I will tell you if my number is HIGHER or LOWER than your guess.") // .assertTextOutput("What is your guess?") .provideLineInput("86") .assertTextOutput("Your guess of 86 is TOO HIGH.") // .assertTextOutput("What is your guess?") .provideLineInput("33") .assertTextOutput("Your guess of 33 is CORRECT in 2 guesses!") // .assertThatTheMethodReturnsHere(); } } }