]> granicus.if.org Git - icu/commitdiff
ICU-21349 calling .usage("") should unset the existing usage
authoryounies <younies@chromium.org>
Fri, 5 Mar 2021 16:15:59 +0000 (16:15 +0000)
committerYounies Mahmoud <younies@chromium.org>
Fri, 5 Mar 2021 23:37:06 +0000 (00:37 +0100)
See #1614

icu4c/source/test/intltest/numbertest_api.cpp
icu4j/main/classes/core/src/com/ibm/icu/number/NumberFormatterSettings.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java

index 2540a1a5056dd1fc4c6d82aa9f65f89c34aeba83..0da4d2ddee5e530b39a1a0a4a29d65963fb0d71c 100644 (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.
index 40ba691952acbb0269916f434476a7a4bbad3601..5549bb465569c430c90b8cef9ff7a45680628497 100644 (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);
     }
 
index a50c282074acddb3640370afacb2f549d1f19105..85670b2ef141ee706d0715d0d887b182be5fb8a9 100644 (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.