|
| 1 | +package dev.objz.commandbridge.scripting.bind; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.BeforeAll; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import dev.objz.commandbridge.logging.Log; |
| 13 | + |
| 14 | +class PlaceholderExtractorTest { |
| 15 | + |
| 16 | + @BeforeAll |
| 17 | + static void installLog() { |
| 18 | + try { |
| 19 | + Log.install(LoggerFactory.getLogger("test")); |
| 20 | + } catch (IllegalStateException e) { |
| 21 | + // Log already installed, ignore |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + void testSinglePlaceholder() { |
| 27 | + List<String> result = PlaceholderExtractor.extract("/give ${player} diamond"); |
| 28 | + assertEquals(List.of("player"), result); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void testMultiplePlaceholders() { |
| 33 | + List<String> result = PlaceholderExtractor.extract("${cmd} ${target}"); |
| 34 | + assertEquals(List.of("cmd", "target"), result); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void testNoPlaceholders() { |
| 39 | + List<String> result = PlaceholderExtractor.extract("plain command"); |
| 40 | + assertTrue(result.isEmpty()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testEmptyString() { |
| 45 | + List<String> result = PlaceholderExtractor.extract(""); |
| 46 | + assertTrue(result.isEmpty()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testNullString() { |
| 51 | + List<String> result = PlaceholderExtractor.extract(null); |
| 52 | + assertTrue(result.isEmpty()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testBlankString() { |
| 57 | + List<String> result = PlaceholderExtractor.extract(" "); |
| 58 | + assertTrue(result.isEmpty()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testRepeatedPlaceholder() { |
| 63 | + List<String> result = PlaceholderExtractor.extract("${a} ${a}"); |
| 64 | + assertEquals(List.of("a", "a"), result); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testAdjacentPlaceholders() { |
| 69 | + List<String> result = PlaceholderExtractor.extract("${x}${y}"); |
| 70 | + assertEquals(List.of("x", "y"), result); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testPlaceholderWithWhitespace() { |
| 75 | + List<String> result = PlaceholderExtractor.extract("${ player }"); |
| 76 | + assertEquals(List.of("player"), result); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testBlankPlaceholder() { |
| 81 | + List<String> result = PlaceholderExtractor.extract("${} command"); |
| 82 | + assertTrue(result.isEmpty()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void testPlaceholderWithSpaces() { |
| 87 | + List<String> result = PlaceholderExtractor.extract("${ } command"); |
| 88 | + assertTrue(result.isEmpty()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void testMixedValidAndBlank() { |
| 93 | + List<String> result = PlaceholderExtractor.extract("${valid} ${} ${another}"); |
| 94 | + assertEquals(List.of("valid", "another"), result); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testExtractAllWithMultipleCommands() { |
| 99 | + List<String> commands = List.of( |
| 100 | + "/give ${player} diamond", |
| 101 | + "${cmd} ${target}" |
| 102 | + ); |
| 103 | + List<String> result = PlaceholderExtractor.extractAll(commands); |
| 104 | + assertEquals(List.of("player", "cmd", "target"), result); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void testExtractAllWithEmptyList() { |
| 109 | + List<String> result = PlaceholderExtractor.extractAll(List.of()); |
| 110 | + assertTrue(result.isEmpty()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void testExtractAllWithNullList() { |
| 115 | + List<String> result = PlaceholderExtractor.extractAll(null); |
| 116 | + assertTrue(result.isEmpty()); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testExtractAllWithNullCommand() { |
| 121 | + List<String> commands = new java.util.ArrayList<>(); |
| 122 | + commands.add("/give ${player} diamond"); |
| 123 | + commands.add(null); |
| 124 | + commands.add("${cmd} ${target}"); |
| 125 | + List<String> result = PlaceholderExtractor.extractAll(commands); |
| 126 | + assertEquals(List.of("player", "cmd", "target"), result); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void testExtractAllWithBlankCommand() { |
| 131 | + List<String> commands = List.of( |
| 132 | + "/give ${player} diamond", |
| 133 | + " ", |
| 134 | + "${cmd} ${target}" |
| 135 | + ); |
| 136 | + List<String> result = PlaceholderExtractor.extractAll(commands); |
| 137 | + assertEquals(List.of("player", "cmd", "target"), result); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void testExtractAllWithDuplicates() { |
| 142 | + List<String> commands = List.of( |
| 143 | + "${a} ${a}", |
| 144 | + "${a} ${b}" |
| 145 | + ); |
| 146 | + List<String> result = PlaceholderExtractor.extractAll(commands); |
| 147 | + assertEquals(List.of("a", "a", "a", "b"), result); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void testComplexCommand() { |
| 152 | + List<String> result = PlaceholderExtractor.extract( |
| 153 | + "execute as ${player} at @s run give @s ${item} ${amount}" |
| 154 | + ); |
| 155 | + assertEquals(List.of("player", "item", "amount"), result); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void testPlaceholderWithSpecialChars() { |
| 160 | + List<String> result = PlaceholderExtractor.extract("${player_name}"); |
| 161 | + assertEquals(List.of("player_name"), result); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + void testPlaceholderWithNumbers() { |
| 166 | + List<String> result = PlaceholderExtractor.extract("${arg1} ${arg2}"); |
| 167 | + assertEquals(List.of("arg1", "arg2"), result); |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments