From 6b1112438a545245ec04b9cb7b76476481a5099c Mon Sep 17 00:00:00 2001 From: ljacqu Date: Fri, 7 Oct 2016 23:44:36 +0200 Subject: [PATCH] #293 Fix tests and create consistency test for English help file --- .../command/help/HelpMessagesService.java | 2 +- src/main/resources/messages/help_de.yml | 27 ++++++ src/main/resources/messages/help_en.yml | 19 ++-- .../help/HelpMessagesConsistencyTest.java | 89 +++++++++++++++++++ .../authme/command/help/HelpProviderTest.java | 27 ++++-- 5 files changed, 146 insertions(+), 18 deletions(-) create mode 100644 src/main/resources/messages/help_de.yml create mode 100644 src/test/java/fr/xephi/authme/command/help/HelpMessagesConsistencyTest.java diff --git a/src/main/java/fr/xephi/authme/command/help/HelpMessagesService.java b/src/main/java/fr/xephi/authme/command/help/HelpMessagesService.java index 621c2cc3b..db19aa694 100644 --- a/src/main/java/fr/xephi/authme/command/help/HelpMessagesService.java +++ b/src/main/java/fr/xephi/authme/command/help/HelpMessagesService.java @@ -18,7 +18,7 @@ import java.util.stream.Collectors; */ public class HelpMessagesService implements Reloadable { - private static final String COMMAND_PREFIX = "command."; + private static final String COMMAND_PREFIX = "commands."; private static final String DESCRIPTION_SUFFIX = ".description"; private static final String DETAILED_DESCRIPTION_SUFFIX = ".detailedDescription"; private static final String DEFAULT_PERMISSIONS_PATH = "common.defaultPermissions."; diff --git a/src/main/resources/messages/help_de.yml b/src/main/resources/messages/help_de.yml new file mode 100644 index 000000000..2ae870fc3 --- /dev/null +++ b/src/main/resources/messages/help_de.yml @@ -0,0 +1,27 @@ +common: + description.short: 'Beschreibung' + description.detailed: 'Detaillierte Beschreibung' + usage: 'Gebrauch' + arguments: 'Argumente' + permissions: 'Rechte' + optional: 'Optional' + hasPermission: 'Du hast Berechtigung' + noPermission: 'Keine Berechtigung' + default: 'Default' + alternatives: 'Alternativen' + result: 'Resultat' + commands: 'Kommandos' + defaultPermissions: + notAllowed: 'Kein Recht' + opOnly: 'Nur OP''s' + allowed: 'Allen erlaubt' +commands: + authme.register: + description: 'Registriert einen Benutzer' + detailedDescription: 'Registriert den Benutzer mit dem gegebenen Passwort.' + arg1: + label: 'spieler' + description: 'Name des Spielers' + arg2: + label: 'passwort' + description: 'Passwort' diff --git a/src/main/resources/messages/help_en.yml b/src/main/resources/messages/help_en.yml index 16a458a31..9cb8bb86f 100644 --- a/src/main/resources/messages/help_en.yml +++ b/src/main/resources/messages/help_en.yml @@ -4,7 +4,7 @@ common: usage: 'Usage' arguments: 'Arguments' permissions: 'Permissions' - optional: '(Optional)' + optional: 'Optional' hasPermission: 'You have permission' noPermission: 'No permission' default: 'Default' @@ -15,16 +15,13 @@ common: notAllowed: 'No permission' opOnly: 'OP''s only' allowed: 'Everyone allowed' -command: - register: - description: 'Register with this command' - detailedDescription: 'Use /register ' +commands: authme.register: - description: 'Admin registration' - detailedDescription: 'Used by l33t adminz' + description: 'Register a player' + detailedDescription: 'Register the specified player with the specified password.' arg1: - label: 'n4me' - description: 'The name to save' + label: 'player' + description: 'Player name' arg2: - label: 'passw0rd' - description: 'Password to use yo' + label: 'password' + description: 'Password' diff --git a/src/test/java/fr/xephi/authme/command/help/HelpMessagesConsistencyTest.java b/src/test/java/fr/xephi/authme/command/help/HelpMessagesConsistencyTest.java new file mode 100644 index 000000000..8a9f03891 --- /dev/null +++ b/src/test/java/fr/xephi/authme/command/help/HelpMessagesConsistencyTest.java @@ -0,0 +1,89 @@ +package fr.xephi.authme.command.help; + +import fr.xephi.authme.TestHelper; +import fr.xephi.authme.command.CommandDescription; +import fr.xephi.authme.command.CommandInitializer; +import org.bukkit.configuration.MemorySection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.junit.Test; + +import java.io.File; +import java.util.List; +import java.util.Set; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assert.assertThat; + +/** + * Tests that /messages/help_en.yml contains texts that correspond + * to the texts provided in the CommandDescription. + */ +public class HelpMessagesConsistencyTest { + + private static final File DEFAULT_MESSAGES_FILE = TestHelper.getJarFile("/messages/help_en.yml"); + + @Test + public void shouldHaveIdenticalTexts() { + // given + CommandDescription description = getAuthMeRegisterDescription(); + FileConfiguration configuration = YamlConfiguration.loadConfiguration(DEFAULT_MESSAGES_FILE); + final String path = "commands.authme.register."; + + // when / then + assertThat(configuration.get(path + "description"), equalTo(description.getDescription())); + assertThat(configuration.get(path + "detailedDescription"), equalTo(description.getDetailedDescription())); + assertThat(configuration.get(path + "arg1.label"), equalTo(description.getArguments().get(0).getName())); + assertThat(configuration.get(path + "arg1.description"), equalTo(description.getArguments().get(0).getDescription())); + assertThat(configuration.get(path + "arg2.label"), equalTo(description.getArguments().get(1).getName())); + assertThat(configuration.get(path + "arg2.description"), equalTo(description.getArguments().get(1).getDescription())); + } + + /** + * Since CommandInitializer contains all descriptions for commands in English, the help_en.yml file + * only contains an entry for one command as to provide an example. + */ + @Test + public void shouldOnlyHaveDescriptionForOneCommand() { + // given + FileConfiguration configuration = YamlConfiguration.loadConfiguration(DEFAULT_MESSAGES_FILE); + + // when + Object commands = configuration.get("commands"); + + // then + assertThat(commands, instanceOf(MemorySection.class)); + assertThat(((MemorySection) commands).getKeys(false), contains("authme")); + } + + @Test + public void shouldHaveEntryForEachHelpMessageKey() { + // given + FileConfiguration configuration = YamlConfiguration.loadConfiguration(DEFAULT_MESSAGES_FILE); + + // when / then + for (HelpMessageKey key : HelpMessageKey.values()) { + assertThat("Default configuration has entry for key '" + key + "'", + configuration.contains(key.getKey()), equalTo(true)); + } + } + + /** + * @return the CommandDescription object for the {@code /authme register} command. + */ + private static CommandDescription getAuthMeRegisterDescription() { + Set commands = new CommandInitializer().getCommands(); + + List children = commands.stream() + .filter(command -> command.getLabels().contains("authme")) + .map(CommandDescription::getChildren) + .findFirst().get(); + + return children + .stream() + .filter(child -> child.getLabels().contains("register")) + .findFirst().get(); + } +} diff --git a/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java b/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java index 1308b2407..baca3e4df 100644 --- a/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java +++ b/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java @@ -8,6 +8,7 @@ import fr.xephi.authme.command.FoundCommandResult; import fr.xephi.authme.command.FoundResultStatus; import fr.xephi.authme.command.TestCommandsUtil; import fr.xephi.authme.permission.AdminPermission; +import fr.xephi.authme.permission.DefaultPermission; import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.PluginSettings; @@ -72,8 +73,7 @@ public class HelpProviderTest { @BeforeInjecting public void setInitialSettings() { given(settings.getProperty(PluginSettings.HELP_HEADER)).willReturn(HELP_HEADER); - given(helpMessagesService.buildLocalizedDescription(any(CommandDescription.class))) - .willAnswer(new ReturnsArgumentAt(0)); + setDefaultHelpMessages(helpMessagesService); } @Test @@ -160,9 +160,9 @@ public class HelpProviderTest { assertThat(lines, hasSize(5)); assertThat(removeColors(lines.get(1)), containsString("Permissions:")); assertThat(removeColors(lines.get(2)), - containsString(AdminPermission.UNREGISTER.getNode() + " (You have permission)")); - assertThat(removeColors(lines.get(3)), containsString("Default: OP's only (You have permission)")); - assertThat(removeColors(lines.get(4)), containsString("Result: You have permission")); + containsString(AdminPermission.UNREGISTER.getNode() + " (Has permission)")); + assertThat(removeColors(lines.get(3)), containsString("Default: Op only (Has permission)")); + assertThat(removeColors(lines.get(4)), containsString("Result: Has permission")); } @Test @@ -183,7 +183,7 @@ public class HelpProviderTest { assertThat(removeColors(lines.get(1)), containsString("Permissions:")); assertThat(removeColors(lines.get(2)), containsString(AdminPermission.UNREGISTER.getNode() + " (No permission)")); - assertThat(removeColors(lines.get(3)), containsString("Default: OP's only (No permission)")); + assertThat(removeColors(lines.get(3)), containsString("Default: Op only (No permission)")); assertThat(removeColors(lines.get(4)), containsString("Result: No permission")); } @@ -365,4 +365,19 @@ public class HelpProviderTest { return captor.getAllValues(); } + private static void setDefaultHelpMessages(HelpMessagesService helpMessagesService) { + given(helpMessagesService.buildLocalizedDescription(any(CommandDescription.class))) + .willAnswer(new ReturnsArgumentAt(0)); + for (HelpMessageKey key : HelpMessageKey.values()) { + String text = key.name().replace("_", " ").toLowerCase(); + given(helpMessagesService.getMessage(key)) + .willReturn(text.substring(0, 1).toUpperCase() + text.substring(1)); + } + for (DefaultPermission permission : DefaultPermission.values()) { + String text = permission.name().replace("_", " ").toLowerCase(); + given(helpMessagesService.getMessage(permission)) + .willReturn(text.substring(0, 1).toUpperCase() + text.substring(1)); + } + } + }