#293 Fix tests and create consistency test for English help file

This commit is contained in:
ljacqu 2016-10-07 23:44:36 +02:00
parent 26716b0f79
commit 6b1112438a
5 changed files with 146 additions and 18 deletions

View File

@ -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.";

View File

@ -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'

View File

@ -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 <pw> <pw>'
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'

View File

@ -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<CommandDescription> commands = new CommandInitializer().getCommands();
List<CommandDescription> 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();
}
}

View File

@ -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));
}
}
}