From acde9b9aab690947550b905909427a78ef94c2cd Mon Sep 17 00:00:00 2001 From: Elango Cheran Date: Fri, 12 Jan 2024 00:35:05 +0000 Subject: [PATCH] Add Likely Subtags component tests to ICU4J executor (#160) --- .../unicode/conformance/Icu4jExecutor.java | 3 + .../likelysubtags/LikelySubtagsInputJson.java | 15 ++++ .../LikelySubtagsOutputJson.java | 19 +++++ .../LikelySubtagsTestOption.java | 8 +++ .../likelysubtags/LikelySubtagsTester.java | 72 +++++++++++++++++++ .../likelysubtags/LikelySubtagsTest.java | 33 +++++++++ run_config.json | 3 +- 7 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsInputJson.java create mode 100644 executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsOutputJson.java create mode 100644 executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsTestOption.java create mode 100644 executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsTester.java create mode 100644 executors/icu4j/73/executor-icu4j/src/test/java/org/unicode/conformance/likelysubtags/LikelySubtagsTest.java diff --git a/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/Icu4jExecutor.java b/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/Icu4jExecutor.java index b2e02c53..4c599eb3 100644 --- a/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/Icu4jExecutor.java +++ b/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/Icu4jExecutor.java @@ -11,6 +11,7 @@ import org.unicode.conformance.testtype.ITestType; import org.unicode.conformance.testtype.collator.CollatorTester; import org.unicode.conformance.testtype.langnames.LangNamesTester; +import org.unicode.conformance.testtype.likelysubtags.LikelySubtagsTester; /** * Hello world! @@ -115,6 +116,8 @@ public static String getTestCaseResponse(String inputLine) throws Exception { testType = CollatorTester.INSTANCE; } else if (testTypeStr.equals("lang_names")) { testType = LangNamesTester.INSTANCE; + } else if (testTypeStr.equals("likely_subtags")) { + testType = LikelySubtagsTester.INSTANCE; } else { io.lacuna.bifurcan.IMap response = parsedInputPersistentMap diff --git a/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsInputJson.java b/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsInputJson.java new file mode 100644 index 00000000..16b6696f --- /dev/null +++ b/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsInputJson.java @@ -0,0 +1,15 @@ +package org.unicode.conformance.testtype.likelysubtags; + +import org.unicode.conformance.testtype.ITestTypeInputJson; + +public class LikelySubtagsInputJson implements ITestTypeInputJson { + + public String test_type; + + public String label; + + public String locale; + + public LikelySubtagsTestOption option; + +} diff --git a/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsOutputJson.java b/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsOutputJson.java new file mode 100644 index 00000000..929e2254 --- /dev/null +++ b/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsOutputJson.java @@ -0,0 +1,19 @@ +package org.unicode.conformance.testtype.likelysubtags; + +import org.unicode.conformance.testtype.ITestTypeOutputJson; + +public class LikelySubtagsOutputJson implements ITestTypeOutputJson { + + public String label; + + public String locale; + + public LikelySubtagsTestOption option; + + public String result; + + public String error; + + public String error_message; + +} diff --git a/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsTestOption.java b/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsTestOption.java new file mode 100644 index 00000000..aba3cca1 --- /dev/null +++ b/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsTestOption.java @@ -0,0 +1,8 @@ +package org.unicode.conformance.testtype.likelysubtags; + +public enum LikelySubtagsTestOption { + maximize, + minimize, + minimizeFavorScript, + minimizeFavorRegion +} diff --git a/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsTester.java b/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsTester.java new file mode 100644 index 00000000..9b487bbe --- /dev/null +++ b/executors/icu4j/73/executor-icu4j/src/main/java/org/unicode/conformance/testtype/likelysubtags/LikelySubtagsTester.java @@ -0,0 +1,72 @@ +package org.unicode.conformance.testtype.likelysubtags; + +import com.ibm.icu.util.ULocale; +import io.lacuna.bifurcan.Map; +import org.unicode.conformance.ExecutorUtils; +import org.unicode.conformance.testtype.ITestType; +import org.unicode.conformance.testtype.ITestTypeInputJson; +import org.unicode.conformance.testtype.ITestTypeOutputJson; + +public class LikelySubtagsTester implements ITestType { + + public static LikelySubtagsTester INSTANCE = new LikelySubtagsTester(); + + @Override + public ITestTypeInputJson inputMapToJson(Map inputMapData) { + LikelySubtagsInputJson result = new LikelySubtagsInputJson(); + + result.test_type = inputMapData.get("test_type", null); + result.label = inputMapData.get("label", null); + result.locale = inputMapData.get("locale", null); + result.option = LikelySubtagsTestOption.valueOf( + inputMapData.get("option", null) + ); + + return result; + } + + @Override + public ITestTypeOutputJson execute(ITestTypeInputJson inputJson) { + LikelySubtagsInputJson input = (LikelySubtagsInputJson) inputJson; + + // partially construct output + LikelySubtagsOutputJson output = new LikelySubtagsOutputJson(); + output.label = input.label; + + try { + output.result = getLikelySubtagString(input); + } catch (Exception e) { + output.error = "error running test"; + output.error = e.getMessage(); + return output; + } + + // If we get here, it's a pass/fail result (supported options and no runtime errors/exceptions) + return output; + } + + @Override + public String formatOutputJson(ITestTypeOutputJson outputJson) { + return ExecutorUtils.GSON.toJson((LikelySubtagsOutputJson) outputJson); + } + + public String getLikelySubtagString(LikelySubtagsInputJson input) { + String localeID = input.locale; + ULocale locale = ULocale.forLanguageTag(localeID); + + LikelySubtagsTestOption option = input.option; + + switch (option) { + case maximize: + return ULocale.addLikelySubtags(locale).toLanguageTag(); + case minimize: + case minimizeFavorRegion: + return ULocale.minimizeSubtags(locale).toLanguageTag(); + case minimizeFavorScript: + throw new UnsupportedOperationException( + "Likely Subtags test option `minimizeFavorScript` not supported"); + default: + throw new UnsupportedOperationException("Likely Subtags test option not supported"); + } + } +} diff --git a/executors/icu4j/73/executor-icu4j/src/test/java/org/unicode/conformance/likelysubtags/LikelySubtagsTest.java b/executors/icu4j/73/executor-icu4j/src/test/java/org/unicode/conformance/likelysubtags/LikelySubtagsTest.java new file mode 100644 index 00000000..7956470c --- /dev/null +++ b/executors/icu4j/73/executor-icu4j/src/test/java/org/unicode/conformance/likelysubtags/LikelySubtagsTest.java @@ -0,0 +1,33 @@ +package org.unicode.conformance.likelysubtags; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.unicode.conformance.testtype.likelysubtags.LikelySubtagsOutputJson; +import org.unicode.conformance.testtype.likelysubtags.LikelySubtagsTester; + +public class LikelySubtagsTest { + + @Test + public void testMinimizeSubtags() { + String testInput = + "{\"test_type\":\"likely_subtags\", \"option\":\"minimize\", \"locale\":\"fr-Latn-FR\", \"label\":\"1\"}"; + + LikelySubtagsOutputJson output = + (LikelySubtagsOutputJson) LikelySubtagsTester.INSTANCE.getStructuredOutputFromInputStr(testInput); + + assertEquals("fr", output.result); + } + + @Test + public void testMaximizeSubtags() { + String testInput = + "{\"test_type\":\"likely_subtags\", \"option\":\"maximize\", \"locale\":\"fr\", \"label\":\"1\"}"; + + LikelySubtagsOutputJson output = + (LikelySubtagsOutputJson) LikelySubtagsTester.INSTANCE.getStructuredOutputFromInputStr(testInput); + + assertEquals("fr-Latn-FR", output.result); + } + +} diff --git a/run_config.json b/run_config.json index 49bb141b..e6f31792 100644 --- a/run_config.json +++ b/run_config.json @@ -251,7 +251,8 @@ "icu_version": "icu73", "exec": "icu4j", "test_type": [ - "lang_names" + "lang_names", + "likely_subtags" ], "per_execution": 10000 }