ICU-21349 calling .usage("") should unset the existing usage

See #1614
This commit is contained in:
younies 2021-03-05 16:15:59 +00:00 committed by Younies Mahmoud
parent 75874eb0eb
commit b1a685a676
3 changed files with 92 additions and 0 deletions

View file

@ -1719,6 +1719,43 @@ void NumberFormatterApiTest::unitUsage() {
30500,
u"350 m");
// Test calling `.usage("")` should unset the existing usage.
// First: without usage
assertFormatSingle(u"Rounding Mode propagates: rounding up",
u"measure-unit/length-centimeter rounding-mode-ceiling",
u"unit/centimeter rounding-mode-ceiling",
NumberFormatter::with()
.unit(MeasureUnit::forIdentifier("centimeter", status))
.roundingMode(UNUM_ROUND_CEILING),
Locale("en-US"), //
3048, //
u"3,048 cm");
// Second: with "road" usage
assertFormatSingle(u"Rounding Mode propagates: rounding up",
u"usage/road measure-unit/length-centimeter rounding-mode-ceiling",
u"usage/road unit/centimeter rounding-mode-ceiling",
NumberFormatter::with()
.unit(MeasureUnit::forIdentifier("centimeter", status))
.usage("road")
.roundingMode(UNUM_ROUND_CEILING),
Locale("en-US"), //
3048, //
u"100 ft");
// Third: with "road" usage, then the usage unsetted by calling .usage("")
assertFormatSingle(u"Rounding Mode propagates: rounding up",
u"measure-unit/length-centimeter rounding-mode-ceiling",
u"unit/centimeter rounding-mode-ceiling",
NumberFormatter::with()
.unit(MeasureUnit::forIdentifier("centimeter", status))
.usage("road")
.roundingMode(UNUM_ROUND_CEILING)
.usage(""), // unset
Locale("en-US"), //
3048, //
u"3,048 cm");
// TODO(icu-units#38): improve unit testing coverage. E.g. add vehicle-fuel
// triggering inversion conversion code. Test with 0 too, to see
// divide-by-zero behaviour.

View file

@ -545,6 +545,10 @@ public abstract class NumberFormatterSettings<T extends NumberFormatterSettings<
* @draft ICU 68
*/
public T usage(String usage) {
if (usage != null && usage.isEmpty()) {
return create(KEY_USAGE, null);
}
return create(KEY_USAGE, usage);
}

View file

@ -1653,6 +1653,57 @@ public class NumberFormatterApiTest extends TestFmwk {
30500,
"350 m");
// Test calling .usage("") or .usage(null) should unset the existing usage.
// First: without usage
assertFormatSingle("Rounding Mode propagates: rounding up",
"measure-unit/length-centimeter rounding-mode-ceiling",
"unit/centimeter rounding-mode-ceiling",
NumberFormatter.with()
.unit(MeasureUnit.forIdentifier("centimeter"))
.roundingMode(RoundingMode.CEILING),
new ULocale("en-US"),
3048,
"3,048 cm");
// Second: with "road" usage
assertFormatSingle("Rounding Mode propagates: rounding up",
"usage/road measure-unit/length-centimeter rounding-mode-ceiling",
"usage/road unit/centimeter rounding-mode-ceiling",
NumberFormatter.with()
.unit(MeasureUnit.forIdentifier("centimeter"))
.usage("road")
.roundingMode(RoundingMode.CEILING),
new ULocale("en-US"),
3048,
"100 ft");
// Third: with "road" usage, then the usage unsetted by calling .usage("")
assertFormatSingle("Rounding Mode propagates: rounding up",
"measure-unit/length-centimeter rounding-mode-ceiling",
"unit/centimeter rounding-mode-ceiling",
NumberFormatter.with()
.unit(MeasureUnit.forIdentifier("centimeter"))
.usage("road")
.roundingMode(RoundingMode.CEILING)
.usage(""), // unset
new ULocale("en-US"),
3048,
"3,048 cm");
// Fourth: with "road" usage, then the usage unsetted by calling .usage(nul)
assertFormatSingle("Rounding Mode propagates: rounding up",
"measure-unit/length-centimeter rounding-mode-ceiling",
"unit/centimeter rounding-mode-ceiling",
NumberFormatter.with()
.unit(MeasureUnit.forIdentifier("centimeter"))
.usage("road")
.roundingMode(RoundingMode.CEILING)
.usage(null), // unset
new ULocale("en-US"),
3048,
"3,048 cm");
// TODO(icu-units#38): improve unit testing coverage. E.g. add
// vehicle-fuel triggering inversion conversion code. Test with 0 too,
// to see divide-by-zero behaviour.