-
Notifications
You must be signed in to change notification settings - Fork 16
System in can use less home-made code #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,18 +37,19 @@ static void checkArrayConstants() { | |
@Test | ||
void provided_text_is_available_from_system_in( | ||
) throws Exception { | ||
AtomicReference<String> firstLineCapture = new AtomicReference<>(); | ||
AtomicReference<String> secondLineCapture = new AtomicReference<>(); | ||
|
||
withTextFromSystemIn( | ||
"first line", | ||
"second line" | ||
).execute(() -> { | ||
Scanner firstScanner = new Scanner(in); | ||
firstScanner.nextLine(); | ||
Scanner secondScanner = new Scanner(in); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first scanner may cache more than just the first line, so the second scanner is unable to pick up the second line. |
||
secondLineCapture.set(secondScanner.nextLine()); | ||
Scanner scanner = new Scanner(in); | ||
firstLineCapture.set(scanner.nextLine()); | ||
secondLineCapture.set(scanner.nextLine()); | ||
}); | ||
|
||
assertThat(firstLineCapture).hasValue("first line"); | ||
assertThat(secondLineCapture).hasValue("second line"); | ||
} | ||
|
||
|
@@ -201,24 +202,24 @@ void system_in_throws_IndexOutOfBoundsException_when_read_is_called_with_oversiz | |
} | ||
|
||
@Test | ||
void system_in_reads_zero_bytes_even_if_mock_should_throw_IOException_on_input_end( | ||
void system_in_reads_no_bytes_even_if_mock_should_throw_IOException_on_input_end( | ||
) throws Exception { | ||
withTextFromSystemIn() | ||
.andExceptionThrownOnInputEnd(DUMMY_IO_EXCEPTION) | ||
.execute(() -> { | ||
int numBytesRead = System.in.read(DUMMY_ARRAY, VALID_OFFSET, 0); | ||
assertThat(numBytesRead).isZero(); | ||
assertThat(numBytesRead).isEqualTo(-1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); | ||
} | ||
|
||
@Test | ||
void system_in_reads_zero_bytes_even_if_mock_should_throw_RuntimeException_on_input_end( | ||
void system_in_reads_no_bytes_even_if_mock_should_throw_RuntimeException_on_input_end( | ||
) throws Exception { | ||
withTextFromSystemIn() | ||
.andExceptionThrownOnInputEnd(DUMMY_RUNTIME_EXCEPTION) | ||
.execute(() -> { | ||
int numBytesRead = System.in.read(DUMMY_ARRAY, VALID_OFFSET, 0); | ||
assertThat(numBytesRead).isZero(); | ||
assertThat(numBytesRead).isEqualTo(-1); | ||
}); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Increase clarity of the second assert