|
1 | 1 | package cf.maybelambda.fedora; |
2 | 2 |
|
3 | 3 | import org.junit.jupiter.api.Test; |
| 4 | +import org.junit.jupiter.params.ParameterizedTest; |
| 5 | +import org.junit.jupiter.params.provider.ValueSource; |
4 | 6 | import org.mockito.MockedStatic; |
5 | 7 |
|
6 | 8 | import java.io.ByteArrayInputStream; |
| 9 | +import java.io.ByteArrayOutputStream; |
7 | 10 | import java.io.Console; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.PrintStream; |
8 | 13 | import java.nio.charset.StandardCharsets; |
9 | 14 | import java.util.List; |
10 | 15 | import java.util.Scanner; |
|
15 | 20 | import static cf.maybelambda.fedora.ConsoleIOHelper.color; |
16 | 21 | import static cf.maybelambda.fedora.ConsoleIOHelper.confirm; |
17 | 22 | import static cf.maybelambda.fedora.ConsoleIOHelper.isANSISupported; |
| 23 | +import static cf.maybelambda.fedora.ConsoleIOHelper.printHelp; |
18 | 24 | import static cf.maybelambda.fedora.ConsoleIOHelper.promptForExclusions; |
19 | 25 | import static org.junit.jupiter.api.Assertions.assertEquals; |
20 | 26 | import static org.junit.jupiter.api.Assertions.assertFalse; |
@@ -85,11 +91,20 @@ void promptForExclusionsHandlesInvalidIndexes() { |
85 | 91 | assertEquals(List.of("pkg1", "pkg3"), result); |
86 | 92 | } |
87 | 93 |
|
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) { |
90 | 105 | List<String> pkgs = List.of("pkg"); |
91 | 106 | Scanner scanner = mock(Scanner.class); |
92 | | - when(scanner.nextLine()).thenReturn("qwerty"); |
| 107 | + when(scanner.nextLine()).thenReturn(input); |
93 | 108 |
|
94 | 109 | assertThrows(RuntimeException.class, () -> promptForExclusions(pkgs, scanner)); |
95 | 110 | } |
@@ -135,4 +150,44 @@ void isANSISupportedReturnsFalseWhenTermIsNull() { |
135 | 150 | void isANSISupportedReturnsFalseWhenTermIsDumb() { |
136 | 151 | assertFalse(isANSISupported("dumb", mock(Console.class))); |
137 | 152 | } |
| 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 | + } |
138 | 193 | } |
0 commit comments