-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test that translation files follow the rules
- Loading branch information
1 parent
55d009b
commit 2600894
Showing
2 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/test/java/net/earthcomputer/clientcommands/test/TranslationsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package net.earthcomputer.clientcommands.test; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonObject; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
import org.junit.jupiter.api.DynamicTest; | ||
import org.junit.jupiter.api.TestFactory; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public final class TranslationsTest { | ||
private static final Gson GSON = new Gson(); | ||
|
||
@TestFactory | ||
public Iterable<DynamicTest> testTranslate() throws IOException { | ||
Path langDir = FabricLoader.getInstance().getModContainer("clientcommands").orElseThrow() | ||
.findPath("assets/clientcommands/lang").orElseThrow(); | ||
|
||
JsonObject enUs; | ||
try (BufferedReader reader = Files.newBufferedReader(langDir.resolve("en_us.json"))) { | ||
enUs = GSON.fromJson(reader, JsonObject.class); | ||
} | ||
|
||
List<DynamicTest> tests = new ArrayList<>(); | ||
|
||
try (Stream<Path> langFiles = Files.list(langDir)) { | ||
for (Path langFile : (Iterable<Path>) langFiles::iterator) { | ||
tests.add(DynamicTest.dynamicTest("Check " + langFile.getFileName().toString(), () -> checkLangFile(enUs, langFile))); | ||
} | ||
} | ||
|
||
return tests; | ||
} | ||
|
||
private static void checkLangFile(JsonObject enUs, Path langFile) throws IOException { | ||
String langFileName = langFile.getFileName().toString(); | ||
|
||
JsonObject lang; | ||
try (BufferedReader reader = Files.newBufferedReader(langFile)) { | ||
lang = GSON.fromJson(reader, JsonObject.class); | ||
} | ||
|
||
for (String key : lang.keySet()) { | ||
assertTrue(enUs.has(key), () -> langFileName + " has key " + key + " not present in en_us.json"); | ||
} | ||
for (var entry : lang.entrySet()) { | ||
assertTrue(entry.getValue().isJsonPrimitive() && entry.getValue().getAsJsonPrimitive().isString(), () -> langFileName + " has a non-string value associated with " + entry.getKey()); | ||
} | ||
for (var entry : lang.entrySet()) { | ||
assertTrue(checkPercentS(entry.getValue().getAsString()), () -> langFileName + " has invalid formatting codes in " + entry.getKey() + ". Only %s, %n$s and %% are allowed"); | ||
} | ||
} | ||
|
||
private static final Pattern NON_PERCENT_S_REGEX = Pattern.compile("%(?!(?:\\d+\\$)?s)"); | ||
|
||
private static boolean checkPercentS(String value) { | ||
return !NON_PERCENT_S_REGEX.matcher(value.replace("%%", "")).find(); | ||
} | ||
} |