Home | History | Annotate | Download | only in intltest
      1 /********************************************************************
      2  * COPYRIGHT:
      3  * Copyright (c) 1997-2010, International Business Machines Corporation and
      4  * others. All Rights Reserved.
      5  * Copyright (C) 2010 , Yahoo! Inc.
      6  ********************************************************************/
      7 
      8 #include "unicode/utypes.h"
      9 
     10 #if !UCONFIG_NO_FORMATTING
     11 
     12 #include "selfmts.h"
     13 #include "cmemory.h"
     14 #include "unicode/selfmt.h"
     15 #include "stdio.h"
     16 
     17 const UnicodeString SIMPLE_PATTERN = UnicodeString("feminine {feminineVerbValue} other{otherVerbValue}");
     18 #define SELECT_PATTERN_DATA 4
     19 #define SELECT_SYNTAX_DATA 10
     20 #define EXP_FORMAT_RESULT_DATA 12
     21 #define NUM_OF_FORMAT_ARGS 3
     22 
     23 void SelectFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
     24 {
     25     if (exec) logln("TestSuite SelectFormat");
     26     switch (index) {
     27         TESTCASE(0, selectFormatAPITest);
     28         TESTCASE(1, selectFormatUnitTest);
     29         default: name = "";
     30             break;
     31     }
     32 }
     33 
     34 /**
     35  * Unit tests of SelectFormat class.
     36  */
     37 void SelectFormatTest::selectFormatUnitTest(/*char *par*/)
     38 {
     39     UnicodeString patternTestData[SELECT_PATTERN_DATA] = {
     40         UNICODE_STRING_SIMPLE("fem {femValue} other{even}"),
     41         UNICODE_STRING_SIMPLE("other{odd or even}"),
     42         UNICODE_STRING_SIMPLE("odd{The number {0, number, integer} is odd.}other{The number {0, number, integer} is even.}"),
     43         UNICODE_STRING_SIMPLE("odd{The number {1} is odd}other{The number {1} is even}"),
     44     };
     45 
     46     UnicodeString formatArgs[NUM_OF_FORMAT_ARGS] = {
     47         UNICODE_STRING_SIMPLE("fem"),
     48         UNICODE_STRING_SIMPLE("other"),
     49         UNICODE_STRING_SIMPLE("odd")
     50     };
     51 
     52     UnicodeString expFormatResult[][NUM_OF_FORMAT_ARGS] = {
     53         {
     54             UNICODE_STRING_SIMPLE("femValue"),
     55             UNICODE_STRING_SIMPLE("even"),
     56             UNICODE_STRING_SIMPLE("even")
     57         },
     58         {
     59             UNICODE_STRING_SIMPLE("odd or even"),
     60             UNICODE_STRING_SIMPLE("odd or even"),
     61             UNICODE_STRING_SIMPLE("odd or even"),
     62         },
     63         {
     64             UNICODE_STRING_SIMPLE("The number {0, number, integer} is even."),
     65             UNICODE_STRING_SIMPLE("The number {0, number, integer} is even."),
     66             UNICODE_STRING_SIMPLE("The number {0, number, integer} is odd."),
     67         },
     68         {
     69             UNICODE_STRING_SIMPLE("The number {1} is even"),
     70             UNICODE_STRING_SIMPLE("The number {1} is even"),
     71             UNICODE_STRING_SIMPLE("The number {1} is odd"),
     72         }
     73     };
     74 
     75     UnicodeString checkSyntaxData[SELECT_SYNTAX_DATA] = {
     76         UNICODE_STRING_SIMPLE("odd{foo} odd{bar} other{foobar}"),
     77         UNICODE_STRING_SIMPLE("odd{foo} other{bar} other{foobar}"),
     78         UNICODE_STRING_SIMPLE("odd{foo}"),
     79         UNICODE_STRING_SIMPLE("1odd{foo} other{bar}"),
     80         UNICODE_STRING_SIMPLE("odd{foo},other{bar}"),
     81         UNICODE_STRING_SIMPLE("od d{foo} other{bar}"),
     82         UNICODE_STRING_SIMPLE("odd{foo}{foobar}other{foo}"),
     83         UNICODE_STRING_SIMPLE("odd{foo1}other{foo2}}"),
     84         UNICODE_STRING_SIMPLE("odd{foo1}other{{foo2}"),
     85         UNICODE_STRING_SIMPLE("odd{fo{o1}other{foo2}}")
     86     };
     87 
     88     UErrorCode expErrorCodes[SELECT_SYNTAX_DATA]={
     89         U_DUPLICATE_KEYWORD,
     90         U_DUPLICATE_KEYWORD,
     91         U_DEFAULT_KEYWORD_MISSING,
     92         U_PATTERN_SYNTAX_ERROR,
     93         U_PATTERN_SYNTAX_ERROR,
     94         U_PATTERN_SYNTAX_ERROR,
     95         U_PATTERN_SYNTAX_ERROR,
     96         U_PATTERN_SYNTAX_ERROR,
     97         U_PATTERN_SYNTAX_ERROR,
     98         U_DEFAULT_KEYWORD_MISSING
     99     };
    100 
    101     UErrorCode status = U_ZERO_ERROR;
    102     SelectFormat* selFmt = new SelectFormat( SIMPLE_PATTERN , status);
    103     if (U_FAILURE(status)) {
    104         dataerrln("ERROR: SelectFormat Unit Test constructor failed in unit tests.- exitting");
    105         return;
    106     }
    107 
    108     // ======= Test SelectFormat pattern syntax.
    109     logln("SelectFormat Unit Test : Testing SelectFormat pattern syntax.");
    110     for (int32_t i=0; i<SELECT_SYNTAX_DATA; ++i) {
    111         status = U_ZERO_ERROR;
    112         selFmt->applyPattern(checkSyntaxData[i], status);
    113         if( status!= expErrorCodes[i] ){
    114             errln("\nERROR: Unexpected result - SelectFormat Unit Test failed to detect syntax error with pattern: "+checkSyntaxData[i]+" and expected status="+ u_errorName(expErrorCodes[i]) + " and resulted status="+u_errorName(status));
    115         }
    116     }
    117 
    118     delete selFmt;
    119     selFmt = NULL;
    120 
    121     logln("SelectFormat Unit Test : Creating format object for Testing applying various patterns");
    122     status = U_ZERO_ERROR;
    123     selFmt = new SelectFormat( SIMPLE_PATTERN , status);
    124     //SelectFormat* selFmt1 = new SelectFormat( SIMPLE_PATTERN , status);
    125     if (U_FAILURE(status)) {
    126         errln("ERROR: SelectFormat Unit Test constructor failed in unit tests.- exitting");
    127         return;
    128     }
    129 
    130     // ======= Test applying and formatting with various pattern
    131     logln("SelectFormat Unit test: Testing  applyPattern() and format() ...");
    132     UnicodeString result;
    133     FieldPosition ignore(FieldPosition::DONT_CARE);
    134 
    135     for(int32_t i=0; i<SELECT_PATTERN_DATA; ++i) {
    136         status = U_ZERO_ERROR;
    137         selFmt->applyPattern(patternTestData[i], status);
    138         if (U_FAILURE(status)) {
    139             errln("ERROR: SelectFormat Unit Test failed to apply pattern- "+patternTestData[i] );
    140             continue;
    141         }
    142 
    143         //Format with the keyword array
    144         for(int32_t j=0; j<3; j++) {
    145             result.remove();
    146             selFmt->format( formatArgs[j], result , ignore , status);
    147             if (U_FAILURE(status)) {
    148                 errln("ERROR: SelectFormat Unit test failed in format() with argument: "+ formatArgs[j] + " and error is " + u_errorName(status) );
    149             }else{
    150                 if( result != expFormatResult[i][j] ){
    151                     errln("ERROR: SelectFormat Unit test failed in format() with unexpected result\n  with argument: "+ formatArgs[j] + "\n result obtained: " + result + "\n and expected is: " + expFormatResult[i][j] );
    152                 }
    153             }
    154         }
    155     }
    156 
    157     //Test with an invalid keyword
    158     logln("SelectFormat Unit test: Testing  format() with keyword method and with invalid keywords...");
    159     status = U_ZERO_ERROR;
    160     result.remove();
    161     UnicodeString keywords[] = {
    162         "9Keyword-_",       //Starts with a digit
    163         "-Keyword-_",       //Starts with a hyphen
    164         "_Keyword-_",       //Starts with a underscore
    165         "\\u00E9Keyword-_", //Starts with non-ASCII character
    166         "Key*word-_",       //Contains a sepial character not allowed
    167         "*Keyword-_"        //Starts with a sepial character not allowed
    168     };
    169 
    170     delete selFmt;
    171     selFmt = NULL;
    172 
    173     selFmt = new SelectFormat( SIMPLE_PATTERN , status);
    174     for (int32_t i = 0; i< 6; i++ ){
    175         status = U_ZERO_ERROR;
    176         selFmt->format( keywords[i], result , ignore , status);
    177         if (!U_FAILURE(status)) {
    178             errln("ERROR: SelectFormat Unit test failed in format() with keyWord and with an invalid keyword as : "+ keywords[i]);
    179         }
    180     }
    181 
    182     delete selFmt;
    183 }
    184 
    185 /**
    186  * Test various generic API methods of SelectFormat for Basic API usage.
    187  * This is to make sure the API test coverage is 100% .
    188  */
    189 void SelectFormatTest::selectFormatAPITest(/*char *par*/)
    190 {
    191     int numOfConstructors =3;
    192     UErrorCode status[3];
    193     SelectFormat* selFmt[3];
    194 
    195     // ========= Test constructors
    196     logln("SelectFormat API test: Testing SelectFormat constructors ...");
    197     for (int32_t i=0; i< numOfConstructors; ++i) {
    198         status[i] = U_ZERO_ERROR;
    199     }
    200 
    201     selFmt[0]= new SelectFormat(SIMPLE_PATTERN, status[0]);
    202     if ( U_FAILURE(status[0]) ) {
    203         errln("ERROR: SelectFormat API test constructor with pattern and status failed!");
    204         return;
    205     }
    206 
    207     // =========== Test copy constructor
    208     logln("SelectFormat API test: Testing copy constructor and == operator ...");
    209     SelectFormat fmt = *selFmt[0];
    210     SelectFormat* dupPFmt = new SelectFormat(fmt);
    211     if ((*selFmt[0]) != (*dupPFmt)) {
    212         errln("ERROR: SelectFormat API test Failed in copy constructor or == operator!");
    213     }
    214     delete dupPFmt;
    215 
    216     // ======= Test clone && == operator.
    217     logln("SelectFormat API test: Testing clone and == operator ...");
    218     if ( U_SUCCESS(status[0])  ) {
    219         selFmt[1] = (SelectFormat*)selFmt[0]->clone();
    220         if (selFmt[1]!=NULL) {
    221             if ( *selFmt[1] != *selFmt[0] ) {
    222                 errln("ERROR: SelectFormat API test clone test failed!");
    223             }
    224         }
    225     }
    226 
    227     // ======= Test assignment operator && == operator.
    228     logln("SelectFormat API test: Testing assignment operator and == operator ...");
    229     selFmt[2]= new SelectFormat(SIMPLE_PATTERN, status[2]);
    230     if ( U_SUCCESS(status[2]) ) {
    231         *selFmt[1] = *selFmt[2];
    232         if (selFmt[1]!=NULL) {
    233             if ( (*selFmt[1] != *selFmt[2]) ) {
    234                 errln("ERROR: SelectFormat API test assignment operator test failed!");
    235             }
    236         }
    237         delete selFmt[1];
    238     }
    239     else {
    240          errln("ERROR: SelectFormat constructor failed in assignment operator!");
    241     }
    242     delete selFmt[0];
    243     delete selFmt[2];
    244 
    245     // ======= Test getStaticClassID() and getStaticClassID()
    246     logln("SelectFormat API test: Testing getStaticClassID() and getStaticClassID() ...");
    247     UErrorCode status1 = U_ZERO_ERROR;
    248     SelectFormat* selFmt1 = new SelectFormat( SIMPLE_PATTERN , status1);
    249     if( U_FAILURE(status1)) {
    250         errln("ERROR: SelectFormat constructor failed in staticClassID test! Exitting");
    251         return;
    252     }
    253 
    254     logln("Testing getStaticClassID()");
    255     if(selFmt1->getDynamicClassID() !=SelectFormat::getStaticClassID()) {
    256         errln("ERROR: SelectFormat API test getDynamicClassID() didn't return the expected value");
    257     }
    258 
    259     // ======= Test applyPattern() and toPattern()
    260     logln("SelectFormat API test: Testing applyPattern() and toPattern() ...");
    261     UnicodeString pattern = UnicodeString("masculine{masculineVerbValue} other{otherVerbValue}");
    262     status1 = U_ZERO_ERROR;
    263     selFmt1->applyPattern( pattern, status1);
    264     if (U_FAILURE(status1)) {
    265         errln("ERROR: SelectFormat API test failed in applyPattern() with pattern: "+ pattern);
    266     }else{
    267         UnicodeString checkPattern;
    268         selFmt1->toPattern( checkPattern);
    269         if( checkPattern != pattern ){
    270             errln("ERROR: SelectFormat API test failed in toPattern() with unexpected result with pattern: "+ pattern);
    271         }
    272     }
    273 
    274     // ======= Test different format() methods
    275     logln("SelectFormat API test: Testing  format() with keyword method ...");
    276     status1 = U_ZERO_ERROR;
    277     UnicodeString result;
    278     FieldPosition ignore(FieldPosition::DONT_CARE);
    279     UnicodeString keyWord = UnicodeString("masculine");
    280 
    281     selFmt1->format( keyWord, result , ignore , status1);
    282     if (U_FAILURE(status1)) {
    283         errln("ERROR: SelectFormat API test failed in format() with keyWord: "+ keyWord);
    284     }else{
    285         UnicodeString expected=UnicodeString("masculineVerbValue");
    286         if( result != expected ){
    287             errln("ERROR: SelectFormat API test failed in format() with unexpected result with keyWord: "+ keyWord);
    288         }
    289     }
    290 
    291     logln("SelectFormat API test: Testing  format() with Formattable obj method ...");
    292     status1 = U_ZERO_ERROR;
    293     result.remove();
    294     UnicodeString result1;
    295     Formattable testArgs = Formattable("other");
    296     selFmt1->format( testArgs, result1 , ignore , status1);
    297     if (U_FAILURE(status1)) {
    298         errln("ERROR: SelectFormat API test failed in format() with Formattable");
    299     }else{
    300         UnicodeString expected=UnicodeString("otherVerbValue");
    301         if( result1 != expected ){
    302             errln("ERROR: SelectFormat API test failed in format() with unexpected result with Formattable");
    303         }
    304     }
    305 
    306 
    307     delete selFmt1;
    308 }
    309 #endif /* #if !UCONFIG_NO_FORMATTING */
    310