Skip to content

Commit

Permalink
Adjust input tester behavior to be more in line with the console
Browse files Browse the repository at this point in the history
  • Loading branch information
MaisiKoleni committed Nov 27, 2020
1 parent 9fa7fab commit 1c5573c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
16 changes: 6 additions & 10 deletions src/main/java/de/tum/in/test/api/io/TestInStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class TestInStream extends InputStream {
@Override
public int read() throws IOException {
if (input == null)
tryLoadNextLine(true);
tryLoadNextLine();
int res = input.read();
if (input.available() == 0)
input = null;
Expand All @@ -28,15 +28,13 @@ public int read() throws IOException {

@Override
public int available() throws IOException {
if (input == null)
tryLoadNextLine(false);
return input == null ? 0 : input.available();
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
if (input == null)
tryLoadNextLine(true);
tryLoadNextLine();
int res = input.read(b, off, len);
if (input.available() == 0)
input = null;
Expand All @@ -47,11 +45,9 @@ void resetInternalState() {
input = null;
}

private void tryLoadNextLine(boolean force) {
if (force || lineProvider.hasNextLine()) {
Line currentLine = lineProvider.getNextLine();
byte[] bytes = currentLine.text().concat(IOTester.LINE_SEPERATOR).getBytes(StandardCharsets.UTF_8);
input = new ByteArrayInputStream(bytes);
}
private void tryLoadNextLine() {
Line currentLine = lineProvider.getNextLine();
byte[] bytes = currentLine.text().concat(IOTester.LINE_SEPERATOR).getBytes(StandardCharsets.UTF_8);
input = new ByteArrayInputStream(bytes);
}
}
4 changes: 2 additions & 2 deletions src/test/java/de/tum/in/test/api/io/IOTesterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void testUninstall() {
@Test
void testReset() {
try (Scanner scanner = new Scanner(System.in)) {
tester.in().addLinesToInput("X");
tester.in().addLinesToInput("X", "Y");

System.out.println("A");
System.err.println("B");
Expand All @@ -36,7 +36,7 @@ void testReset() {
assertThat(tester.err().getLines()).isNotEmpty();
assertThat(scanner.nextLine()).isEqualTo("X");

tester.in().addLinesToInput("Y");
tester.in().addLinesToInput("Z");

tester.reset();

Expand Down

0 comments on commit 1c5573c

Please sign in to comment.