Skip to content

Commit 37ec49c

Browse files
committed
improve documentation
1 parent d4eed70 commit 37ec49c

File tree

1 file changed

+73
-14
lines changed

1 file changed

+73
-14
lines changed

README.md

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,76 @@ Include the following dependency on your pom file:
1414

1515
## How to use
1616

17-
class TestStdinStdout {
18-
19-
@Test
20-
public void testAskName() {
21-
StdinStdoutHelper helper = new StdinStdoutHelper()
22-
.expectOutput("Enter your name")
23-
.simulateInput("Pedro")
24-
.expectOutput("Your name is Pedro");
25-
26-
helper.start();
27-
main(null);
28-
helper.stop();
29-
}
30-
}
17+
```java
18+
public class Main {
19+
public static void main(String args[]) {
20+
Scanner scanner = new Scanner(System.in);
21+
22+
System.out.print("Enter your name");
23+
String name = scanner.nextLine();
24+
System.out.println("Your name is " + name);
25+
26+
scanner.close();
27+
}
28+
}
29+
30+
31+
class TestStdinStdout {
32+
33+
@Test
34+
public void testAskName() {
35+
StdinStdoutHelper helper = new StdinStdoutHelper()
36+
.expectOutput("Enter your name")
37+
.simulateInput("Pedro")
38+
.expectOutput("Your name is Pedro");
39+
40+
helper.start();
41+
main(null);
42+
helper.stop();
43+
}
44+
}
45+
```
46+
47+
### Contextual information on failure
48+
49+
In highly interactive programs, it can be difficult to understand what happened on certain
50+
test failures. You can pass a `showDetailedErrors` and a `òutputBufferSize` to the
51+
`StdinStdoutHelper` constructor, as in this example:
52+
53+
```java
54+
StdinStdoutHelper helper = new StdinStdoutHelper(true, 20)
55+
.expectOutput("Enter your name")
56+
.simulateInput("Pedro")
57+
.expectOutput("Enter your age")
58+
.simulateInput("45")
59+
.expectOutput("Pedro, you're 45 years old!");
60+
```
61+
This will show the last 20 (or less) lines of the interaction before the failure.
62+
63+
### Ignoring output lines
64+
65+
If the program writes several lines to the stdout that you don't want to test, you can use the function `expectNNumberOfLines`:
66+
```java
67+
StdinStdoutHelper helper = new StdinStdoutHelper()
68+
.expectOutput("Enter the file name")
69+
.simulateInput("data.txt")
70+
.expectOutput("The data contained in the file:")
71+
.expectNNumberOfLines(57) // suppose the file has 57 lines
72+
.expectOutput("Read another file? [Y|N]")
73+
.simulateInput("N");
74+
```
75+
76+
### Dynamic behaviour
77+
78+
Suppose you have a program that prints a random number to the stdout, and you want to test that the printed number is
79+
within a certain range. You can use the `matchOutput` which receives a predicate function. This function receives the
80+
line that was printed to the screen and returns true if it matches the expected value. This is evaluated during the
81+
program's execution.
82+
83+
#### Example in Kotlin
84+
```kotlin
85+
val helper = StdinStdoutHelper()
86+
.expectOutput("Press enter to generate a random number between 1 and 100")
87+
.simulateInput("") // press enter
88+
.matchOutput { it in 1..100 }
89+
```

0 commit comments

Comments
 (0)