Change message file consistency test to show all errors

This commit is contained in:
ljacqu 2016-03-29 19:07:18 +02:00
parent 55c24b8e64
commit 95f5951587

View File

@ -7,37 +7,55 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.fail;
import static org.junit.Assert.assertThat;
/** /**
* Tests that the project's default language file contains a text for all message keys. * Tests that the project's default language file contains a text for all message keys.
* <p>
* Translators can change the file name in {@link #MESSAGES_FILE} to validate their translation.
*/ */
public class MessagesFileConsistencyTest { public class MessagesFileConsistencyTest {
private static final String DEFAULT_MESSAGES_FILE = "/messages/messages_en.yml"; private static final String MESSAGES_FILE = "/messages/messages_en.yml";
@Test @Test
public void shouldHaveAllMessages() { public void shouldHaveAllMessages() {
File file = TestHelper.getJarFile(DEFAULT_MESSAGES_FILE); File file = TestHelper.getJarFile(MESSAGES_FILE);
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file); FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
List<String> errors = new ArrayList<>();
for (MessageKey messageKey : MessageKey.values()) { for (MessageKey messageKey : MessageKey.values()) {
verifyHasMessage(messageKey, configuration); validateMessage(messageKey, configuration, errors);
}
if (!errors.isEmpty()) {
fail("Validation errors in " + MESSAGES_FILE + ":\n- "
+ StringUtils.join("\n- ", errors));
} }
} }
private static void verifyHasMessage(MessageKey messageKey, FileConfiguration configuration) { private static void validateMessage(MessageKey messageKey, FileConfiguration configuration, List<String> errors) {
final String key = messageKey.getKey(); final String key = messageKey.getKey();
final String message = configuration.getString(key); final String message = configuration.getString(key);
assertThat("Default messages file should have message for key '" + key + "'", if (StringUtils.isEmpty(message)) {
StringUtils.isEmpty(message), equalTo(false)); errors.add("Messages file should have message for key '" + key + "'");
return;
}
List<String> missingTags = new ArrayList<>();
for (String tag : messageKey.getTags()) { for (String tag : messageKey.getTags()) {
assertThat("The message for key '" + key + "' contains the tag '" + tag + "' in the default messages file", if (!message.contains(tag)) {
message.contains(tag), equalTo(true)); missingTags.add(tag);
}
}
if (!missingTags.isEmpty()) {
String pluralS = missingTags.size() > 1 ? "s" : "";
errors.add(String.format("Message with key '%s' missing tag%s: %s", key, pluralS,
StringUtils.join(", ", missingTags)));
} }
} }
} }