forked from unicode-org/conformance
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main'
- Loading branch information
Showing
8 changed files
with
294 additions
and
4 deletions.
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
22 changes: 22 additions & 0 deletions
22
.../unicode/conformance/testtype/relativedatetimeformat/RelativeDateTimeFormatInputJson.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,22 @@ | ||
package org.unicode.conformance.testtype.relativedatetimeformat; | ||
|
||
import org.unicode.conformance.testtype.ITestTypeInputJson; | ||
|
||
public class RelativeDateTimeFormatInputJson implements ITestTypeInputJson { | ||
|
||
public String test_type; | ||
|
||
public String label; | ||
|
||
public String locale; | ||
|
||
public String numberingSystem; | ||
|
||
public String count; | ||
|
||
public RelativeDateTimeFormatUnits unit; | ||
|
||
public RelativeDateTimeFormatStyle style; // E.g., SHORT | ||
|
||
public double quantity; | ||
} |
16 changes: 16 additions & 0 deletions
16
...unicode/conformance/testtype/relativedatetimeformat/RelativeDateTimeFormatOutputJson.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,16 @@ | ||
package org.unicode.conformance.testtype.relativedatetimeformat; | ||
|
||
import org.unicode.conformance.testtype.ITestTypeOutputJson; | ||
|
||
public class RelativeDateTimeFormatOutputJson implements ITestTypeOutputJson { | ||
|
||
public String test_type; | ||
|
||
public String label; | ||
|
||
public String result; | ||
|
||
public String error; | ||
|
||
public String error_message; | ||
} |
17 changes: 17 additions & 0 deletions
17
.../org/unicode/conformance/testtype/relativedatetimeformat/RelativeDateTimeFormatStyle.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,17 @@ | ||
package org.unicode.conformance.testtype.relativedatetimeformat; | ||
|
||
public enum RelativeDateTimeFormatStyle { | ||
LONG, | ||
NARROW, | ||
SHORT; | ||
|
||
public static org.unicode.conformance.testtype.relativedatetimeformat.RelativeDateTimeFormatStyle DEFAULT = LONG; | ||
|
||
public static org.unicode.conformance.testtype.relativedatetimeformat.RelativeDateTimeFormatStyle getFromString(String s) { | ||
try { | ||
return org.unicode.conformance.testtype.relativedatetimeformat.RelativeDateTimeFormatStyle.valueOf(s.toUpperCase()); | ||
} catch (Exception e){ | ||
return DEFAULT; | ||
} | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
...org/unicode/conformance/testtype/relativedatetimeformat/RelativeDateTimeFormatTester.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,124 @@ | ||
package org.unicode.conformance.testtype.relativedatetimeformat; | ||
|
||
import com.ibm.icu.text.DisplayContext; | ||
import com.ibm.icu.text.NumberFormat; | ||
import com.ibm.icu.text.RelativeDateTimeFormatter; | ||
import com.ibm.icu.text.RelativeDateTimeFormatter.Style; | ||
import com.ibm.icu.util.ULocale; | ||
|
||
import io.lacuna.bifurcan.IMap; | ||
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 RelativeDateTimeFormatTester implements ITestType { | ||
|
||
public static RelativeDateTimeFormatTester INSTANCE = new RelativeDateTimeFormatTester(); | ||
|
||
@Override | ||
public ITestTypeInputJson inputMapToJson(Map<String, Object> inputMapData) { | ||
RelativeDateTimeFormatInputJson result = new RelativeDateTimeFormatInputJson(); | ||
|
||
result.label = (String) inputMapData.get("label", null); | ||
result.locale = (String) inputMapData.get("locale", null); | ||
result.count = (String) inputMapData.get("count", "0"); | ||
result.quantity = Double.parseDouble(result.count); | ||
|
||
result.numberingSystem = (String) inputMapData.get("numbering_system", null); | ||
|
||
java.util.Map<String,Object> inputOptions = | ||
(java.util.Map<String,Object>) inputMapData.get("options", null); | ||
|
||
result.style = RelativeDateTimeFormatStyle.getFromString( | ||
"" + inputOptions.get("style") | ||
); | ||
|
||
String unitInput = (String) inputMapData.get("unit", "0"); | ||
result.unit = RelativeDateTimeFormatUnits.getFromString( | ||
unitInput); | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
public ITestTypeOutputJson execute(ITestTypeInputJson inputJson) { | ||
RelativeDateTimeFormatInputJson input = (RelativeDateTimeFormatInputJson) inputJson; | ||
|
||
// partially construct output | ||
RelativeDateTimeFormatOutputJson output = (RelativeDateTimeFormatOutputJson) getDefaultOutputJson(); | ||
output.label = input.label; | ||
|
||
try { | ||
output.result = getRelativeDateTimeFormatResultString(input); | ||
} catch (Exception e) { | ||
output.error = e.getMessage(); | ||
output.error_message = 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 ITestTypeOutputJson getDefaultOutputJson() { | ||
return new RelativeDateTimeFormatOutputJson(); | ||
} | ||
|
||
public IMap<String, Object> convertOutputToMap(ITestTypeOutputJson outputJson) { | ||
RelativeDateTimeFormatOutputJson output = (RelativeDateTimeFormatOutputJson) outputJson; | ||
return new io.lacuna.bifurcan.Map<String,Object>() | ||
.put("label", output.label) | ||
.put("result", output.result); | ||
} | ||
|
||
public String formatOutputJson(ITestTypeOutputJson outputJson) { | ||
return ExecutorUtils.GSON.toJson(outputJson); | ||
} | ||
public String getRelativeDateTimeFormatResultString(RelativeDateTimeFormatInputJson input) { | ||
ULocale locale = ULocale.forLanguageTag(input.locale); | ||
Style style; | ||
RelativeDateTimeFormatter.RelativeDateTimeUnit unit; | ||
|
||
switch (input.unit) { | ||
default: | ||
case DAY: unit = RelativeDateTimeFormatter.RelativeDateTimeUnit.DAY; | ||
break; | ||
case HOUR: unit = RelativeDateTimeFormatter.RelativeDateTimeUnit.HOUR; | ||
break; | ||
case MINUTE: unit = RelativeDateTimeFormatter.RelativeDateTimeUnit.MINUTE; | ||
break; | ||
case MONTH: unit = RelativeDateTimeFormatter.RelativeDateTimeUnit.MONTH; | ||
break; | ||
case QUARTER: unit = RelativeDateTimeFormatter.RelativeDateTimeUnit.QUARTER; | ||
break; | ||
case SECOND: unit = RelativeDateTimeFormatter.RelativeDateTimeUnit.SECOND; | ||
break; | ||
case WEEK: unit = RelativeDateTimeFormatter.RelativeDateTimeUnit.WEEK; | ||
break; | ||
case YEAR: unit = RelativeDateTimeFormatter.RelativeDateTimeUnit.YEAR; | ||
break; | ||
} | ||
|
||
switch (input.style) { | ||
case NARROW: style = RelativeDateTimeFormatter.Style.NARROW; | ||
break; | ||
case SHORT: style = RelativeDateTimeFormatter.Style.SHORT; | ||
break; | ||
default: | ||
case LONG: style = RelativeDateTimeFormatter.Style.LONG; | ||
break; | ||
} | ||
|
||
NumberFormat nf = null; | ||
DisplayContext dc = DisplayContext.CAPITALIZATION_NONE; | ||
|
||
RelativeDateTimeFormatter rdtf = | ||
RelativeDateTimeFormatter.getInstance(locale, nf, style, dc); | ||
|
||
return rdtf.format(input.quantity, unit); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
.../org/unicode/conformance/testtype/relativedatetimeformat/RelativeDateTimeFormatUnits.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,22 @@ | ||
package org.unicode.conformance.testtype.relativedatetimeformat; | ||
|
||
public enum RelativeDateTimeFormatUnits { | ||
DAY, | ||
HOUR, | ||
MINUTE, | ||
MONTH, | ||
QUARTER, | ||
SECOND, | ||
WEEK, | ||
YEAR; | ||
|
||
public static org.unicode.conformance.testtype.relativedatetimeformat.RelativeDateTimeFormatUnits DEFAULT = DAY; | ||
|
||
public static org.unicode.conformance.testtype.relativedatetimeformat.RelativeDateTimeFormatUnits getFromString(String s) { | ||
try { | ||
return org.unicode.conformance.testtype.relativedatetimeformat.RelativeDateTimeFormatUnits.valueOf(s.toUpperCase()); | ||
} catch (Exception e){ | ||
return DEFAULT; | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
.../test/java/org/unicode/conformance/relativedatetimeformat/RelativeDateTimeFormatTest.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,88 @@ | ||
package org.unicode.conformance.relativedatetimeformat; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.unicode.conformance.testtype.relativedatetimeformat.RelativeDateTimeFormatOutputJson; | ||
import org.unicode.conformance.testtype.relativedatetimeformat.RelativeDateTimeFormatTester; | ||
|
||
public class RelativeDateTimeFormatTest { | ||
|
||
@Test | ||
public void testEn100SecondsAgo() { | ||
String testInput = | ||
"\t{\"unit\":\"second\",\"count\":\"-100\",\"locale\":\"en-US\",\"options\":{},\"hexhash\":\"ab5dfa48d57aac79202e8e4dfd12b729b8e4a74a\",\"label\":\"0\"}"; | ||
|
||
RelativeDateTimeFormatOutputJson output = | ||
(RelativeDateTimeFormatOutputJson) RelativeDateTimeFormatTester.INSTANCE.getStructuredOutputFromInputStr( | ||
testInput); | ||
|
||
assertEquals("100 seconds ago", output.result); | ||
} | ||
|
||
@Test | ||
public void testEnIn100Sec() { | ||
String testInput = | ||
"\t{\"unit\":\"second\",\"count\":\"100\",\"locale\":\"en-US\",\"options\":{\"style\":\"short\"},\"hexhash\":\"ab5dfa48d57aac79202e8e4dfd12b729b8e4a74a\",\"label\":\"0\"}"; | ||
|
||
RelativeDateTimeFormatOutputJson output = | ||
(RelativeDateTimeFormatOutputJson) RelativeDateTimeFormatTester.INSTANCE.getStructuredOutputFromInputStr( | ||
testInput); | ||
|
||
assertEquals("in 100 sec.", output.result); | ||
} | ||
|
||
@Test | ||
public void testEnIn100Seconds() { | ||
String testInput = | ||
"\t{\"unit\":\"second\",\"count\":\"100\",\"locale\":\"en-US\",\"options\":{\"style\":\"long\"},\"hexhash\":\"ab5dfa48d57aac79202e8e4dfd12b729b8e4a74a\",\"label\":\"0\"}"; | ||
|
||
RelativeDateTimeFormatOutputJson output = | ||
(RelativeDateTimeFormatOutputJson) RelativeDateTimeFormatTester.INSTANCE.getStructuredOutputFromInputStr( | ||
testInput); | ||
|
||
assertEquals("in 100 seconds", output.result); | ||
} | ||
|
||
@Test | ||
public void testEn100SecAgo() { | ||
String testInput = | ||
"\t{\"unit\":\"second\",\"count\":\"-100\",\"locale\":\"en-US\",\"options\":{\"style\":\"short\"},\"hexhash\":\"ab5dfa48d57aac79202e8e4dfd12b729b8e4a74a\",\"label\":\"0\"}"; | ||
|
||
RelativeDateTimeFormatOutputJson output = | ||
(RelativeDateTimeFormatOutputJson) RelativeDateTimeFormatTester.INSTANCE.getStructuredOutputFromInputStr( | ||
testInput); | ||
|
||
assertEquals("100 sec. ago", output.result); | ||
} | ||
|
||
@Test | ||
public void testEn100SAgo() { | ||
String testInput = | ||
"\t{\"unit\":\"second\",\"count\":\"-100\",\"locale\":\"en-US\",\"options\":{\"style\":\"narrow\"},\"hexhash\":\"a57aac792\",\"label\":\"0\"}"; | ||
|
||
RelativeDateTimeFormatOutputJson output = | ||
(RelativeDateTimeFormatOutputJson) RelativeDateTimeFormatTester.INSTANCE.getStructuredOutputFromInputStr( | ||
testInput); | ||
|
||
assertEquals("100s ago", output.result); | ||
} | ||
|
||
@Ignore | ||
// This doesn't yet handle non-ASCII numbering systems | ||
// https://github.com/unicode-org/conformance/issues/261 | ||
@Test | ||
public void testAdlamIn1Year() { | ||
// Adlam string in output | ||
// | ||
String testInput = | ||
"\t{\"unit\":\"second\",\"count\":\"-100\",\"locale\":\"en-US\",\"options\":{\"style\":\"narrow\"},\"numberingSystem\":\"adlm\",\"hexhash\":\"79202e8e\",\"label\":\"0\"}"; | ||
|
||
RelativeDateTimeFormatOutputJson output = | ||
(RelativeDateTimeFormatOutputJson) RelativeDateTimeFormatTester.INSTANCE.getStructuredOutputFromInputStr( | ||
testInput); | ||
|
||
assertEquals("in 𞥑𞥐y", output.result); | ||
} | ||
} |
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