Skip to content

Commit 64dddcf

Browse files
committed
Add test cases
1 parent 99011e5 commit 64dddcf

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

src/test/java/cf/maybelambda/fedora/ConsoleIOHelperTests.java

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package cf.maybelambda.fedora;
22

33
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.ValueSource;
46
import org.mockito.MockedStatic;
57

68
import java.io.ByteArrayInputStream;
9+
import java.io.ByteArrayOutputStream;
710
import java.io.Console;
11+
import java.io.IOException;
12+
import java.io.PrintStream;
813
import java.nio.charset.StandardCharsets;
914
import java.util.List;
1015
import java.util.Scanner;
@@ -15,6 +20,7 @@
1520
import static cf.maybelambda.fedora.ConsoleIOHelper.color;
1621
import static cf.maybelambda.fedora.ConsoleIOHelper.confirm;
1722
import static cf.maybelambda.fedora.ConsoleIOHelper.isANSISupported;
23+
import static cf.maybelambda.fedora.ConsoleIOHelper.printHelp;
1824
import static cf.maybelambda.fedora.ConsoleIOHelper.promptForExclusions;
1925
import static org.junit.jupiter.api.Assertions.assertEquals;
2026
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -85,11 +91,20 @@ void promptForExclusionsHandlesInvalidIndexes() {
8591
assertEquals(List.of("pkg1", "pkg3"), result);
8692
}
8793

88-
@Test
89-
void promptForExclusionsThrowsRuntimeExceptionWhenInvalidInputRead() {
94+
@ParameterizedTest
95+
@ValueSource(strings = {
96+
"1,,3", // multiple consecutive commas
97+
"1,3,", // trailing comma
98+
",1,3", // leading comma
99+
"1a,2", // invalid characters mixed with valid numbers
100+
"1,abc,2", // non-numeric characters
101+
"qwerty", // completely non-numeric input
102+
"!@#, $%^, &*()" // special characters
103+
})
104+
void promptForExclusionsThrowsRuntimeExceptionWhenInvalidInputRead(String input) {
90105
List<String> pkgs = List.of("pkg");
91106
Scanner scanner = mock(Scanner.class);
92-
when(scanner.nextLine()).thenReturn("qwerty");
107+
when(scanner.nextLine()).thenReturn(input);
93108

94109
assertThrows(RuntimeException.class, () -> promptForExclusions(pkgs, scanner));
95110
}
@@ -135,4 +150,44 @@ void isANSISupportedReturnsFalseWhenTermIsNull() {
135150
void isANSISupportedReturnsFalseWhenTermIsDumb() {
136151
assertFalse(isANSISupported("dumb", mock(Console.class)));
137152
}
153+
154+
@Test
155+
void printHelpDisplaysHelpTextSuccessfully() {
156+
try (MockedStatic<ConfigManager> mockedConfig = mockStatic(ConfigManager.class)) {
157+
List<String> helpLines = List.of("Help line 1", "Help line 2", "Help line 3");
158+
mockedConfig.when(ConfigManager::getHelpText).thenReturn(helpLines);
159+
// Capture System.out output
160+
ByteArrayOutputStream capture = new ByteArrayOutputStream();
161+
System.setOut(new PrintStream(capture));
162+
163+
printHelp();
164+
165+
// Reset System.out
166+
System.setOut(System.out);
167+
String output = capture.toString();
168+
169+
assertTrue(output.contains(helpLines.get(0)));
170+
assertTrue(output.contains(helpLines.get(1)));
171+
assertTrue(output.contains(helpLines.get(2)));
172+
}
173+
}
174+
175+
@Test
176+
void printHelpPrintsErrorMessageOnIOException() {
177+
try (MockedStatic<ConfigManager> mockedConfig = mockStatic(ConfigManager.class)) {
178+
mockedConfig.when(ConfigManager::getHelpText).thenThrow(new IOException("File not found"));
179+
// Capture System.err output
180+
ByteArrayOutputStream capture = new ByteArrayOutputStream();
181+
System.setErr(new PrintStream(capture));
182+
183+
printHelp();
184+
185+
// Reset System.err
186+
System.setErr(System.err);
187+
String errorOutput = capture.toString();
188+
189+
assertTrue(errorOutput.contains("Error reading help file"));
190+
assertTrue(errorOutput.contains("File not found"));
191+
}
192+
}
138193
}

0 commit comments

Comments
 (0)