Skip to content

Commit

Permalink
refactor(TextExceptionTest): reconstruct the localized message
Browse files Browse the repository at this point in the history
Instead of checking for whether the JVM is in English and then checking for a static string (which probably changed in the meantime), reconstruct the translated error manually and compare the results. This makes the test environment agnostic.

Signed-off-by: TTtie <[email protected]>
  • Loading branch information
TTtie committed Nov 23, 2023
1 parent 7363626 commit 5d32ef5
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions util/src/test/java/tc/oc/pgm/util/text/TextExceptionTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package tc.oc.pgm.util.text;

import static net.kyori.adventure.text.Component.translatable;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static tc.oc.pgm.util.text.TextTranslations.translate;

import java.util.Locale;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.junit.jupiter.api.Test;

public final class TextExceptionTest {

@Test
void testGetLocalizedMessage() {
final Throwable cause = new IllegalStateException();
Expand All @@ -16,10 +18,12 @@ void testGetLocalizedMessage() {
assertEquals(cause, error.getCause(), "error cause is wrong");
assertEquals("error.unknown", error.getMessage(), "error key is wrong");

// This test can only be run when the JVM has its language set to English or Root
final Locale jvmLocale = Locale.getDefault();
assumeTrue(jvmLocale == Locale.ROOT || jvmLocale == Locale.US, "jvm locale is not english");
assertEquals(
"An unknown error occurred, please see console for details.", error.getLocalizedMessage());
final Component unknownErrorTranslatable = translatable().key("error.unknown").build();
// accessing tc.oc.pgm.util.Audience crashes the test due to Bukkit not being available
final Component translatedError = translate(unknownErrorTranslatable, Audience.empty());
final String serializedError =
PlainTextComponentSerializer.plainText().serialize(translatedError);

assertEquals(serializedError, error.getLocalizedMessage(), "localized message is wrong");
}
}

0 comments on commit 5d32ef5

Please sign in to comment.