Skip to content

Commit

Permalink
Merge branch 'main' of github.com:unicode-org/conformance
Browse files Browse the repository at this point in the history
  • Loading branch information
sven-oly committed Jan 17, 2024
2 parents 3c1068a + acde9b9 commit 8ca0ed9
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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<String,String> response =
parsedInputPersistentMap
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.unicode.conformance.testtype.likelysubtags;

public enum LikelySubtagsTestOption {
maximize,
minimize,
minimizeFavorScript,
minimizeFavorRegion
}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
3 changes: 2 additions & 1 deletion run_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@
"icu_version": "icu73",
"exec": "icu4j",
"test_type": [
"lang_names"
"lang_names",
"likely_subtags"
],
"per_execution": 10000
}
Expand Down

0 comments on commit 8ca0ed9

Please sign in to comment.