Skip to content
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

Closed
wants to merge 2 commits into from
Closed

System in can use less home-made code #16

wants to merge 2 commits into from

Conversation

ashleyfrieze
Copy link
Contributor

  • 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());
Copy link
Contributor Author

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);
Copy link
Contributor Author

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);
Copy link
Contributor Author

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
@ashleyfrieze
Copy link
Contributor Author

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 InputStream as Scanner tries to read ahead.

@ashleyfrieze
Copy link
Contributor Author

Further review suggests that this is not a viable solution... worth exploring whether the behaviour in #18 is what's expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant