Skip to content

Commit

Permalink
ICU-22781 Add tests for portion unit formatting (Java)
Browse files Browse the repository at this point in the history
See #3386
  • Loading branch information
younies committed Feb 11, 2025
1 parent 84599e1 commit b7b2a14
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7120,4 +7120,63 @@ public TestData(String unitIdentifier, Integer inputValue, UnitWidth width, ULoc
}

}

@Test
public void TestPortionFormat() {
MeasureUnit unit = MeasureUnit.forIdentifier("portion-per-1e9");
LocalizedNumberFormatter formatter = NumberFormatter.withLocale(ULocale.ENGLISH).unit(unit)
.unitWidth(UnitWidth.FULL_NAME);
String formatted1 = formatter.format(1).toString();
assertEquals("1 portion per 1e9", "1 part per billion", formatted1);

String formatted2 = formatter.format(2).toString();
assertEquals("1000 portion per 1e9", "2 parts per billion", formatted2);

String formatted3 = formatter.format(1000000).toString();
assertEquals("1000000 portion per 1e9", "1,000,000 parts per billion", formatted3);

String formatted4 = formatter.format(1000000000).toString();
assertEquals("1000000000 portion per 1e9", "1,000,000,000 parts per billion", formatted4);
}

@Test
public void TestArbitraryPortionFormat() {
class TestData {
String unitIdentifier;
Integer inputValue;

TestData(String unitIdentifier, Integer inputValue) {
this.unitIdentifier = unitIdentifier;
this.inputValue = inputValue;
}
}

TestData[] testData = {
new TestData("portion-per-1e1", 1),
new TestData("portion-per-1e2", 1),
new TestData("portion-per-1e3", 1),
new TestData("portion-per-1e4", 1),
new TestData("portion-per-1e5", 1),
new TestData("portion-per-1e6", 1),
new TestData("portion-per-1e7", 1),
new TestData("portion-per-1e8", 1),
};

for (TestData testCase : testData) {
try {
MeasureUnit unit = MeasureUnit.forIdentifier(testCase.unitIdentifier);
LocalizedNumberFormatter formatter = NumberFormatter.withLocale(ULocale.ENGLISH).unit(unit)
.unitWidth(UnitWidth.FULL_NAME);
formatter.format(testCase.inputValue);
// NOTE: All these test cases should pass, but because CLDR data is missing for
// `portion`, an error is thrown.
// See: CLDR-18274
fail("Expected IllegalArgumentException for unit: " + testCase.unitIdentifier
+ ", input: " + testCase.inputValue);

} catch (Exception e) {
// Expected
}
}
}
}

0 comments on commit b7b2a14

Please sign in to comment.