-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathApplicationTest.java
More file actions
110 lines (103 loc) · 3.82 KB
/
ApplicationTest.java
File metadata and controls
110 lines (103 loc) · 3.82 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com
* SPDX-License-Identifier: MIT
*/
package org.eolang.aoi;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Path;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import picocli.CommandLine;
/**
* Tests for {@link Application}.
*
* @since 0.0.5
* @todo #152:15min Enable help flag tests after adding support for the short `-h` flag.
* Once implemented, remove @Disabled from the following tests:
* {@link ApplicationTest#applicationPrintsHelpMessageWhenHelpFlagIsPassed},
* {@link ApplicationTest#applicationExitsWithZeroCodeWhenHelpFlagIsPassed}
*/
final class ApplicationTest {
@Test
void applicationExitsWithZeroCodeOnSuccessfulExecution(
@TempDir final Path input,
@TempDir final Path output
) {
final CommandLine cmd = new CommandLine(new Application());
final int exit = cmd.execute(input.toString(), output.toString());
MatcherAssert.assertThat(
"Application did not exit with a zero code on successful execution",
exit,
Matchers.is(0)
);
}
@Test
void applicationPrintsNothingToStderrOnSuccessfulExecution(
@TempDir final Path input,
@TempDir final Path output
) {
final CommandLine cmd = new CommandLine(new Application());
final StringWriter stderr = new StringWriter();
cmd.setErr(new PrintWriter(stderr));
cmd.execute(input.toString(), output.toString());
MatcherAssert.assertThat(
"Application printed to stderr during a successful execution",
stderr.toString(),
Matchers.emptyString()
);
}
@Disabled
@ParameterizedTest
@ValueSource(strings = {"--help", "-h"})
void applicationPrintsHelpMessageWhenHelpFlagIsPassed(final String flag) {
final CommandLine cmd = new CommandLine(new Application());
final StringWriter stdout = new StringWriter();
cmd.setOut(new PrintWriter(stdout));
cmd.execute(flag);
MatcherAssert.assertThat(
"Application did not print the help message to stdout for the %s flag".formatted(flag),
stdout.toString(),
Matchers.containsString("Usage:")
);
}
@Disabled
@ParameterizedTest
@ValueSource(strings = {"--help", "-h"})
void applicationExitsWithZeroCodeWhenHelpFlagIsPassed(final String flag) {
final CommandLine cmd = new CommandLine(new Application());
final int exit = cmd.execute(flag);
MatcherAssert.assertThat(
"Application did not exit with a zero code when the %s flag was used".formatted(flag),
exit,
Matchers.is(0)
);
}
@Test
void applicationExitsWithNonZeroCodeWhenNoArgumentsAreProvided() {
final CommandLine cmd = new CommandLine(new Application());
cmd.setErr(new PrintWriter(new StringWriter()));
final int exit = cmd.execute();
MatcherAssert.assertThat(
"Application did not exit with a non-zero code when required arguments were missing",
exit,
Matchers.not(0)
);
}
@Test
void applicationExitsWithNonZeroCodeWhenOneArgumentIsMissing() {
final CommandLine cmd = new CommandLine(new Application());
cmd.setErr(new PrintWriter(new StringWriter()));
final int exit = cmd.execute("input/dir");
MatcherAssert.assertThat(
"Application did not exit with a non-zero code when one argument was missing",
exit,
Matchers.not(0)
);
}
}