-
Notifications
You must be signed in to change notification settings - Fork 14
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
System in can use less home-made code #16
System in can use less home-made code #16
Conversation
ashleyfrieze
commented
Nov 13, 2020
- Surprising behaviour with Scanner suggests home-made code acted unusually
- Surprising behaviour with Scanner suggests home-made code acted unusually
@@ -220,7 +220,7 @@ void Scanner_reads_text_from_System_in( | |||
withTextFromSystemIn("first line", "second line") | |||
.execute(() -> { | |||
Scanner scanner = new Scanner(System.in); | |||
scanner.nextLine(); | |||
assertEquals("first line", scanner.nextLine()); |
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
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 comment
The 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.
) 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 comment
The reason will be displayed to describe this comment to others. Learn more.
read
should return -1
at the end of the stream.
- Surprising behaviour with Scanner suggests home-made code acted unusually - Interface for `read` was broken - should return `-1` at stream end
I've encountered some surprises with this implementation. I think the home made stream was there to solve a problem that was not covered by the original unit tests for SystemLambda. I've put the missing tests into #18 I'd like a little clarification, please. What's the intended behaviour? Some of the tests in #18 will likely fail if we use more conventional implementations of |
Further review suggests that this is not a viable solution... worth exploring whether the behaviour in #18 is what's expected. |