|
| 1 | +package cf.maybelambda.fedora; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.mockito.MockedStatic; |
| 5 | + |
| 6 | +import java.io.ByteArrayInputStream; |
| 7 | +import java.io.Console; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Scanner; |
| 11 | + |
| 12 | +import static cf.maybelambda.fedora.ConsoleIOHelper.GREEN; |
| 13 | +import static cf.maybelambda.fedora.ConsoleIOHelper.RESET; |
| 14 | +import static cf.maybelambda.fedora.ConsoleIOHelper.YELLOW; |
| 15 | +import static cf.maybelambda.fedora.ConsoleIOHelper.color; |
| 16 | +import static cf.maybelambda.fedora.ConsoleIOHelper.confirm; |
| 17 | +import static cf.maybelambda.fedora.ConsoleIOHelper.isANSISupported; |
| 18 | +import static cf.maybelambda.fedora.ConsoleIOHelper.promptForExclusions; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | +import static org.mockito.ArgumentMatchers.any; |
| 24 | +import static org.mockito.Mockito.CALLS_REAL_METHODS; |
| 25 | +import static org.mockito.Mockito.mock; |
| 26 | +import static org.mockito.Mockito.mockStatic; |
| 27 | +import static org.mockito.Mockito.when; |
| 28 | + |
| 29 | +public class ConsoleIOHelperTests { |
| 30 | + @Test |
| 31 | + void confirmYesInput() { |
| 32 | + Scanner scanner = new Scanner(new ByteArrayInputStream("y\n".getBytes(StandardCharsets.UTF_8))); |
| 33 | + |
| 34 | + assertTrue(confirm(scanner, "Install packages?")); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void confirmYesFullWord() { |
| 39 | + Scanner scanner = new Scanner(new ByteArrayInputStream("yes\n".getBytes(StandardCharsets.UTF_8))); |
| 40 | + |
| 41 | + assertTrue(confirm(scanner, "Confirm?")); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void confirmNoInput() { |
| 46 | + Scanner scanner = new Scanner(new ByteArrayInputStream("no\n".getBytes(StandardCharsets.UTF_8))); |
| 47 | + |
| 48 | + assertFalse(confirm(scanner, "Confirm?")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void confirmEmptyDefaultsToNo() { |
| 53 | + Scanner scanner = new Scanner(new ByteArrayInputStream("\n".getBytes(StandardCharsets.UTF_8))); |
| 54 | + |
| 55 | + assertFalse(confirm(scanner, "Proceed?")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void promptForExclusionsKeepsAllWhenInputIsEmpty() { |
| 60 | + List<String> pkgs = List.of("nano", "vim", "htop"); |
| 61 | + Scanner scanner = new Scanner(new ByteArrayInputStream("\n".getBytes(StandardCharsets.UTF_8))); |
| 62 | + |
| 63 | + List<String> result = promptForExclusions(pkgs, scanner); |
| 64 | + |
| 65 | + assertEquals(pkgs, result); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void promptForExclusionsRemovesSome() { |
| 70 | + List<String> pkgs = List.of("nano", "vim", "htop", "curl"); |
| 71 | + Scanner scanner = new Scanner(new ByteArrayInputStream("2,4\n".getBytes(StandardCharsets.UTF_8))); |
| 72 | + |
| 73 | + List<String> result = promptForExclusions(pkgs, scanner); |
| 74 | + |
| 75 | + assertEquals(List.of("nano", "htop"), result); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void promptForExclusionsHandlesInvalidIndexes() { |
| 80 | + List<String> pkgs = List.of("pkg1", "pkg2", "pkg3"); |
| 81 | + Scanner scanner = new Scanner(new ByteArrayInputStream("0,5,2\n".getBytes(StandardCharsets.UTF_8))); |
| 82 | + |
| 83 | + List<String> result = promptForExclusions(pkgs, scanner); |
| 84 | + |
| 85 | + assertEquals(List.of("pkg1", "pkg3"), result); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void promptForExclusionsThrowsRuntimeExceptionWhenInvalidInputRead() { |
| 90 | + List<String> pkgs = List.of("pkg"); |
| 91 | + Scanner scanner = mock(Scanner.class); |
| 92 | + when(scanner.nextLine()).thenReturn("qwerty"); |
| 93 | + |
| 94 | + assertThrows(RuntimeException.class, () -> promptForExclusions(pkgs, scanner)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void colorAppliesAnsiWhenisANSISupportedTrue() { |
| 99 | + try (MockedStatic<ConsoleIOHelper> mocked = mockStatic(ConsoleIOHelper.class, CALLS_REAL_METHODS)) { |
| 100 | + mocked.when(() -> isANSISupported(any(), any())).thenReturn(true); |
| 101 | + |
| 102 | + String result = color("Hello", YELLOW); |
| 103 | + |
| 104 | + assertEquals(YELLOW + "Hello" + RESET, result); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void colorReturnsPlainTextWhenisANSISupportedFalse() { |
| 110 | + try (MockedStatic<ConsoleIOHelper> mocked = mockStatic(ConsoleIOHelper.class, CALLS_REAL_METHODS)) { |
| 111 | + mocked.when(() -> isANSISupported(any(), any())).thenReturn(false); |
| 112 | + |
| 113 | + String result = color("World", GREEN); |
| 114 | + |
| 115 | + assertEquals("World", result); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void isANSISupportedReturnsTrueWhenConsolePresentAndTermValid() { |
| 121 | + assertTrue(isANSISupported("xterm-256color", mock(Console.class))); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void isANSISupportedReturnsFalseWhenConsoleIsNull() { |
| 126 | + assertFalse(isANSISupported("xterm-256color", null)); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void isANSISupportedReturnsFalseWhenTermIsNull() { |
| 131 | + assertFalse(isANSISupported(null, mock(Console.class))); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void isANSISupportedReturnsFalseWhenTermIsDumb() { |
| 136 | + assertFalse(isANSISupported("dumb", mock(Console.class))); |
| 137 | + } |
| 138 | +} |
0 commit comments