|
| 1 | +package tfmc.justin.activity.config; |
| 2 | + |
| 3 | +import org.bukkit.configuration.ConfigurationSection; |
| 4 | +import org.bukkit.configuration.file.YamlConfiguration; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import tfmc.justin.activity.utils.Utils; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.regex.Matcher; |
| 12 | +import java.util.regex.Pattern; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | + |
| 18 | +// ==================================== |
| 19 | +// The shipped default messages.yml/config.yml now mix inline #rrggbb hex |
| 20 | +// with legacy &-codes (see the diff that introduced fillEmptySlots and the |
| 21 | +// hex accents). YamlConfiguration.loadConfiguration works headless (pure |
| 22 | +// SnakeYAML under the hood, no live server needed) - these tests just load |
| 23 | +// the real resources exactly as the plugin ships them and check the hex |
| 24 | +// strings survived as values, not as swallowed YAML comments, and that every |
| 25 | +// placeholder callers substitute into is still present. |
| 26 | +// ==================================== |
| 27 | +class DefaultResourcesTest { |
| 28 | + |
| 29 | + private static YamlConfiguration load(String resource) { |
| 30 | + File file = new File("src/main/resources/" + resource); |
| 31 | + assertTrue(file.exists(), resource + " should exist"); |
| 32 | + return YamlConfiguration.loadConfiguration(file); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void everyGuiMessageIsNonEmpty() { |
| 37 | + YamlConfiguration messages = load("messages.yml"); |
| 38 | + ConfigurationSection gui = messages.getConfigurationSection("gui"); |
| 39 | + |
| 40 | + assertTrue(gui != null && !gui.getKeys(false).isEmpty()); |
| 41 | + for (String key : gui.getKeys(false)) { |
| 42 | + String value = gui.getString(key); |
| 43 | + assertFalse(value == null || value.isBlank(), "gui." + key + " should not be blank"); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void guiMessagesKeepTheirPlaceholders() { |
| 49 | + YamlConfiguration messages = load("messages.yml"); |
| 50 | + |
| 51 | + assertTrue(messages.getString("gui.bar-name").contains("%points%")); |
| 52 | + assertTrue(messages.getString("gui.bar-name").contains("%max%")); |
| 53 | + assertTrue(messages.getString("gui.reward-click").contains("%count%")); |
| 54 | + assertTrue(messages.getString("gui.activity-lore-points").contains("%points%")); |
| 55 | + assertTrue(messages.getString("gui.activity-lore-points").contains("%every%")); |
| 56 | + assertTrue(messages.getString("gui.activity-lore-today").contains("%today%")); |
| 57 | + assertTrue(messages.getString("gui.activity-lore-today-capped").contains("%today%")); |
| 58 | + assertTrue(messages.getString("gui.activity-lore-today-capped").contains("%cap%")); |
| 59 | + assertTrue(messages.getString("gui.reward-lore-claimed").contains("%claimed%")); |
| 60 | + assertTrue(messages.getString("gui.reward-lore-claimed").contains("%total%")); |
| 61 | + } |
| 62 | + |
| 63 | + private static final Pattern HEX_MARKER = Pattern.compile("#[0-9a-fA-F]{6}"); |
| 64 | + |
| 65 | + private static void assertEveryHexMarkerIsValid(String value, String where) { |
| 66 | + int index = 0; |
| 67 | + while ((index = value.indexOf('#', index)) != -1) { |
| 68 | + Matcher m = HEX_MARKER.matcher(value); |
| 69 | + assertTrue(m.find(index) && m.start() == index, |
| 70 | + where + ": '#' at index " + index + " is not followed by exactly 6 hex digits: " + value); |
| 71 | + index++; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void everyHexMarkerInGuiAndConfigIsSixDigits() { |
| 77 | + YamlConfiguration messages = load("messages.yml"); |
| 78 | + ConfigurationSection gui = messages.getConfigurationSection("gui"); |
| 79 | + assertTrue(gui != null && !gui.getKeys(false).isEmpty()); |
| 80 | + for (String key : gui.getKeys(false)) { |
| 81 | + String value = gui.getString(key); |
| 82 | + if (value != null) { |
| 83 | + assertEveryHexMarkerIsValid(value, "gui." + key); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + YamlConfiguration config = load("config.yml"); |
| 88 | + ConfigurationSection activities = config.getConfigurationSection("activities"); |
| 89 | + assertTrue(activities != null && !activities.getKeys(false).isEmpty()); |
| 90 | + for (String key : activities.getKeys(false)) { |
| 91 | + String display = activities.getString(key + ".display"); |
| 92 | + if (display != null) { |
| 93 | + assertEveryHexMarkerIsValid(display, "activities." + key + ".display"); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + List<String> rewardDisplay = config.getStringList("rewards.display"); |
| 98 | + for (int i = 0; i < rewardDisplay.size(); i++) { |
| 99 | + assertEveryHexMarkerIsValid(rewardDisplay.get(i), "rewards.display[" + i + "]"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void everyActivityDisplayIsNonEmpty() { |
| 105 | + YamlConfiguration config = load("config.yml"); |
| 106 | + ConfigurationSection activities = config.getConfigurationSection("activities"); |
| 107 | + |
| 108 | + assertTrue(activities != null && !activities.getKeys(false).isEmpty()); |
| 109 | + for (String key : activities.getKeys(false)) { |
| 110 | + String display = activities.getString(key + ".display"); |
| 111 | + assertFalse(display == null || display.isBlank(), "activities." + key + ".display should not be blank"); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void everyRewardDisplayLineIsNonEmpty() { |
| 117 | + YamlConfiguration config = load("config.yml"); |
| 118 | + var display = config.getStringList("rewards.display"); |
| 119 | + |
| 120 | + assertFalse(display.isEmpty()); |
| 121 | + for (String line : display) { |
| 122 | + assertFalse(line == null || line.isBlank(), "rewards.display entries should not be blank"); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void colorizeStripsHexAndLegacyCodes() { |
| 128 | + String result = Utils.colorize("#e6ca40&lX"); |
| 129 | + |
| 130 | + assertFalse(result.contains("#e6ca40"), "hex marker should be translated away: " + result); |
| 131 | + assertFalse(result.contains("&l"), "legacy code should be translated away: " + result); |
| 132 | + } |
| 133 | +} |
0 commit comments