]> granicus.if.org Git - icu/commitdiff
ICU-13437 Adding error codes to NumberFormatter terminal methods.
authorShane Carr <shane@unicode.org>
Mon, 30 Apr 2018 21:07:35 +0000 (21:07 +0000)
committerShane Carr <shane@unicode.org>
Mon, 30 Apr 2018 21:07:35 +0000 (21:07 +0000)
X-SVN-Rev: 41296

icu4c/source/i18n/number_fluent.cpp
icu4c/source/i18n/unicode/numberformatter.h
icu4c/source/test/intltest/numbertest_api.cpp

index bc43defaa3cf8a687548254700600114a9075550..c92c6bd0be72e3ba75eb620bf1407d923bb9b625 100644 (file)
@@ -732,7 +732,17 @@ int32_t LocalizedNumberFormatter::getCallCount() const {
 
 UnicodeString FormattedNumber::toString() const {
     if (fResults == nullptr) {
-        // TODO: http://bugs.icu-project.org/trac/ticket/13437
+        return {};
+    }
+    return fResults->string.toUnicodeString();
+}
+
+UnicodeString FormattedNumber::toString(UErrorCode& status) const {
+    if (U_FAILURE(status)) {
+        return {};
+    }
+    if (fResults == nullptr) {
+        status = fErrorCode;
         return {};
     }
     return fResults->string.toUnicodeString();
@@ -740,7 +750,18 @@ UnicodeString FormattedNumber::toString() const {
 
 Appendable& FormattedNumber::appendTo(Appendable& appendable) {
     if (fResults == nullptr) {
-        // TODO: http://bugs.icu-project.org/trac/ticket/13437
+        return appendable;
+    }
+    appendable.appendString(fResults->string.chars(), fResults->string.length());
+    return appendable;
+}
+
+Appendable& FormattedNumber::appendTo(Appendable& appendable, UErrorCode& status) {
+    if (U_FAILURE(status)) {
+        return appendable;
+    }
+    if (fResults == nullptr) {
+        status = fErrorCode;
         return appendable;
     }
     appendable.appendString(fResults->string.chars(), fResults->string.length());
@@ -748,7 +769,9 @@ Appendable& FormattedNumber::appendTo(Appendable& appendable) {
 }
 
 void FormattedNumber::populateFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) {
-    if (U_FAILURE(status)) { return; }
+    if (U_FAILURE(status)) {
+        return;
+    }
     if (fResults == nullptr) {
         status = fErrorCode;
         return;
@@ -757,7 +780,9 @@ void FormattedNumber::populateFieldPosition(FieldPosition& fieldPosition, UError
 }
 
 void FormattedNumber::populateFieldPositionIterator(FieldPositionIterator& iterator, UErrorCode& status) {
-    if (U_FAILURE(status)) { return; }
+    if (U_FAILURE(status)) {
+        return;
+    }
     if (fResults == nullptr) {
         status = fErrorCode;
         return;
@@ -766,7 +791,9 @@ void FormattedNumber::populateFieldPositionIterator(FieldPositionIterator& itera
 }
 
 void FormattedNumber::getDecimalQuantity(DecimalQuantity& output, UErrorCode& status) const {
-    if (U_FAILURE(status)) { return; }
+    if (U_FAILURE(status)) {
+        return;
+    }
     if (fResults == nullptr) {
         status = fErrorCode;
         return;
index d09ab365a87ca61fb43994198aa8102c85c30b51..493051eb74f153886b3f6f573376e5cfd143a881 100644 (file)
@@ -2320,21 +2320,48 @@ class U_I18N_API FormattedNumber : public UMemory {
      * Returns a UnicodeString representation of the formatted number.
      *
      * @return a UnicodeString containing the localized number.
-     * @draft ICU 60
+     * @deprecated ICU 62 Use the version of this method with an error code instead.
+ *                    This method was never @stable and will be removed in a future release.
+     *                See http://bugs.icu-project.org/trac/ticket/13746
      */
     UnicodeString toString() const;
 
+    /**
+     * Returns a UnicodeString representation of the formatted number.
+     *
+     * @param status
+     *            Set if an error occurs while formatting the number to the UnicodeString.
+     * @return a UnicodeString containing the localized number.
+     * @draft ICU 62
+     */
+    UnicodeString toString(UErrorCode& status) const;
+
     /**
      * Appends the formatted number to an Appendable.
      *
      * @param appendable
      *            The Appendable to which to append the formatted number string.
      * @return The same Appendable, for chaining.
-     * @draft ICU 60
+     * @deprecated ICU 62 Use the version of this method with an error code instead.
+ *                    This method was never @stable and will be removed in a future release.
+     *                See http://bugs.icu-project.org/trac/ticket/13746
      * @see Appendable
      */
     Appendable &appendTo(Appendable &appendable);
 
+    /**
+     * Appends the formatted number to an Appendable.
+     *
+     * @param appendable
+     *            The Appendable to which to append the formatted number string.
+     * @param status
+     *            Set if an error occurs while formatting the number to the Appendable.
+     * @return The same Appendable, for chaining.
+     * @draft ICU 62
+     * @see Appendable
+     */
+    Appendable &appendTo(Appendable &appendable, UErrorCode& status);
+
     /**
      * Determine the start and end indices of the first occurrence of the given <em>field</em> in the output string.
      * This allows you to determine the locations of the integer part, fraction part, and sign.
index f374d8e3e393fd5613f8376a07d71f2137e14462..65611729952f17825f0e687ff775092a6893bcba 100644 (file)
@@ -2071,24 +2071,81 @@ void NumberFormatterApiTest::errors() {
             Rounder::fixedFraction(
                     -1));
 
-    {
-        UErrorCode status1 = U_ZERO_ERROR;
-        UErrorCode status2 = U_ZERO_ERROR;
-        FormattedNumber fn = lnf.formatInt(1, status1);
-        assertEquals(
-                "Should fail since rounder is not legal", U_NUMBER_ARG_OUTOFBOUNDS_ERROR, status1);
-        FieldPosition fp;
-        fn.populateFieldPosition(fp, status2);
-        assertEquals(
-                "Should fail on terminal method", U_NUMBER_ARG_OUTOFBOUNDS_ERROR, status2);
-    }
+    // formatInt
+    UErrorCode status = U_ZERO_ERROR;
+    FormattedNumber fn = lnf.formatInt(1, status);
+    assertEquals(
+            "Should fail in formatInt method with error code for rounding",
+            U_NUMBER_ARG_OUTOFBOUNDS_ERROR,
+            status);
 
-    {
-        UErrorCode status = U_ZERO_ERROR;
-        lnf.copyErrorTo(status);
-        assertEquals(
-                "Should fail since rounder is not legal", U_NUMBER_ARG_OUTOFBOUNDS_ERROR, status);
-    }
+    // formatDouble
+    status = U_ZERO_ERROR;
+    fn = lnf.formatDouble(1.0, status);
+    assertEquals(
+            "Should fail in formatDouble method with error code for rounding",
+            U_NUMBER_ARG_OUTOFBOUNDS_ERROR,
+            status);
+
+    // formatDecimal (decimal error)
+    status = U_ZERO_ERROR;
+    fn = NumberFormatter::withLocale("en").formatDecimal("1x2", status);
+    assertEquals(
+            "Should fail in formatDecimal method with error code for decimal number syntax",
+            U_DECIMAL_NUMBER_SYNTAX_ERROR,
+            status);
+
+    // formatDecimal (setting error)
+    status = U_ZERO_ERROR;
+    fn = lnf.formatDecimal("1.0", status);
+    assertEquals(
+            "Should fail in formatDecimal method with error code for rounding",
+            U_NUMBER_ARG_OUTOFBOUNDS_ERROR,
+            status);
+
+    // FieldPosition
+    status = U_ZERO_ERROR;
+    FieldPosition fp;
+    fn.populateFieldPosition(fp, status);
+    assertEquals(
+            "Should fail on FieldPosition terminal method with correct error code",
+            U_NUMBER_ARG_OUTOFBOUNDS_ERROR,
+            status);
+
+    // FieldPositionIterator
+    status = U_ZERO_ERROR;
+    FieldPositionIterator fpi;
+    fn.populateFieldPositionIterator(fpi, status);
+    assertEquals(
+            "Should fail on FieldPositoinIterator terminal method with correct error code",
+            U_NUMBER_ARG_OUTOFBOUNDS_ERROR,
+            status);
+
+    // Appendable
+    status = U_ZERO_ERROR;
+    UnicodeString output;
+    UnicodeStringAppendable appendable(output);
+    fn.appendTo(appendable, status);
+    assertEquals(
+            "Should fail on Appendable terminal method with correct error code",
+            U_NUMBER_ARG_OUTOFBOUNDS_ERROR,
+            status);
+
+    // UnicodeString
+    status = U_ZERO_ERROR;
+    output = fn.toString(status);
+    assertEquals(
+            "Should fail on UnicodeString terminal method with correct error code",
+            U_NUMBER_ARG_OUTOFBOUNDS_ERROR,
+            status);
+
+    // CopyErrorTo
+    status = U_ZERO_ERROR;
+    lnf.copyErrorTo(status);
+    assertEquals(
+            "Should fail since rounder is not legal with correct error code",
+            U_NUMBER_ARG_OUTOFBOUNDS_ERROR,
+            status);
 }
 
 void NumberFormatterApiTest::validRanges() {