Home | History | Annotate | Download | only in intltest
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html
      3 /*
      4 *******************************************************************************
      5 * Copyright (C) 2014-2016, International Business Machines Corporation and
      6 * others. All Rights Reserved.
      7 *******************************************************************************
      8 *
      9 * File QUANTITYFORMATTERTEST.CPP
     10 *
     11 ********************************************************************************
     12 */
     13 #include "cstring.h"
     14 #include "intltest.h"
     15 #include "quantityformatter.h"
     16 #include "unicode/simpleformatter.h"
     17 #include "unicode/numfmt.h"
     18 #include "unicode/plurrule.h"
     19 
     20 
     21 class QuantityFormatterTest : public IntlTest {
     22 public:
     23     QuantityFormatterTest() {
     24     }
     25     void TestBasic();
     26     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
     27 private:
     28 };
     29 
     30 void QuantityFormatterTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
     31   TESTCASE_AUTO_BEGIN;
     32   TESTCASE_AUTO(TestBasic);
     33   TESTCASE_AUTO_END;
     34 }
     35 
     36 void QuantityFormatterTest::TestBasic() {
     37     UErrorCode status = U_ZERO_ERROR;
     38 #if !UCONFIG_NO_FORMATTING
     39     QuantityFormatter fmt;
     40     assertFalse(
     41             "adding bad variant",
     42             fmt.addIfAbsent("a bad variant", "{0} pounds", status));
     43     assertEquals("adding bad variant status", (int32_t)U_ILLEGAL_ARGUMENT_ERROR, status);
     44     status = U_ZERO_ERROR;
     45     assertFalse(
     46             "Adding bad pattern",
     47             fmt.addIfAbsent("other", "{0} {1} too many placeholders", status));
     48     assertEquals("adding bad pattern status", (int32_t)U_ILLEGAL_ARGUMENT_ERROR, status);
     49     status = U_ZERO_ERROR;
     50     assertFalse("isValid with no patterns", fmt.isValid());
     51     assertTrue(
     52             "Adding good pattern with no placeholders",
     53             fmt.addIfAbsent("zero", "no placeholder", status));
     54     assertTrue(
     55             "Adding good pattern",
     56             fmt.addIfAbsent("other", "{0} pounds", status));
     57     assertTrue("isValid with other", fmt.isValid());
     58     assertTrue(
     59             "Adding good pattern",
     60             fmt.addIfAbsent("one", "{0} pound", status));
     61 
     62     assertEquals(
     63             "getByVariant",
     64             fmt.getByVariant("bad variant")->getTextWithNoArguments(),
     65             " pounds");
     66     assertEquals(
     67             "getByVariant",
     68             fmt.getByVariant("other")->getTextWithNoArguments(),
     69             " pounds");
     70     assertEquals(
     71             "getByVariant",
     72             fmt.getByVariant("one")->getTextWithNoArguments(),
     73             " pound");
     74     assertEquals(
     75             "getByVariant",
     76             fmt.getByVariant("few")->getTextWithNoArguments(),
     77             " pounds");
     78 
     79     // Test copy constructor
     80     {
     81         QuantityFormatter copied(fmt);
     82         assertEquals(
     83                 "copied getByVariant",
     84                 copied.getByVariant("other")->getTextWithNoArguments(),
     85                 " pounds");
     86         assertEquals(
     87                 "copied getByVariant",
     88                 copied.getByVariant("one")->getTextWithNoArguments(),
     89                 " pound");
     90         assertEquals(
     91                 "copied getByVariant",
     92                 copied.getByVariant("few")->getTextWithNoArguments(),
     93                 " pounds");
     94     }
     95 
     96     // Test assignment
     97     {
     98         QuantityFormatter assigned;
     99         assigned = fmt;
    100         assertEquals(
    101                 "assigned getByVariant",
    102                 assigned.getByVariant("other")->getTextWithNoArguments(),
    103                 " pounds");
    104         assertEquals(
    105                 "assigned getByVariant",
    106                 assigned.getByVariant("one")->getTextWithNoArguments(),
    107                 " pound");
    108         assertEquals(
    109                 "assigned getByVariant",
    110                 assigned.getByVariant("few")->getTextWithNoArguments(),
    111                 " pounds");
    112     }
    113 
    114     // Test format.
    115     {
    116         LocalPointer<NumberFormat> numfmt(
    117                 NumberFormat::createInstance(Locale::getEnglish(), status));
    118         LocalPointer<PluralRules> plurrule(
    119                 PluralRules::forLocale("en", status));
    120         FieldPosition pos(FieldPosition::DONT_CARE);
    121         UnicodeString appendTo;
    122         assertEquals(
    123                 "format singular",
    124                 UnicodeString("1 pound"),
    125                 fmt.format(
    126                         1.0,
    127                         *numfmt,
    128                         *plurrule,
    129                         appendTo,
    130                         pos,
    131                         status), TRUE);
    132         appendTo.remove();
    133         assertEquals(
    134                 "format plural",
    135                 UnicodeString("2 pounds"),
    136                 fmt.format(
    137                         2.0,
    138                         *numfmt,
    139                         *plurrule,
    140                         appendTo,
    141                         pos,
    142                         status), TRUE);
    143     }
    144     fmt.reset();
    145     assertFalse("isValid after reset", fmt.isValid());
    146 #endif
    147     assertSuccess("", status);
    148 }
    149 
    150 extern IntlTest *createQuantityFormatterTest() {
    151     return new QuantityFormatterTest();
    152 }
    153