-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleOutputExampleTest.java
More file actions
55 lines (46 loc) · 1.3 KB
/
ConsoleOutputExampleTest.java
File metadata and controls
55 lines (46 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package example;
import app.zoftwhere.bolt.Runner;
import app.zoftwhere.bolt.api.RunnerInterface.RunConsoleArgued;
import java.io.PrintStream;
import org.junit.jupiter.api.Test;
/**
* This class is the test class for {@link example.ConsoleOutputExample}.
*
* @author Osmund
* @version 11.2.0
* @since 1.0.0
*/
class ConsoleOutputExampleTest {
private final Runner runner = new Runner();
/** Not Thread Safe * */
private static RunConsoleArgued redirect(final ThrowingConsumer<String[]> program) {
return (arguments, inputStream, outputStream) -> {
var systemIn = System.in;
var systemOutput = System.out;
var systemError = System.err;
System.setIn(inputStream);
System.setOut(new PrintStream(outputStream));
System.setErr(new PrintStream(outputStream));
try {
program.accept(arguments);
} finally {
System.setIn(systemIn);
System.setOut(systemOutput);
System.setErr(systemError);
}
};
}
@Test
void testCase() {
// Not Thread Safe!
runner
.runConsole(redirect(ConsoleOutputExample::main))
.argument()
.input("World")
.expected("Hello World!", "")
.assertSuccess();
}
private interface ThrowingConsumer<T> {
void accept(T value) throws Exception;
}
}