1 /********************************************************************
3 * Copyright (c) 1997-2011, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 * Copyright (C) 2010 , Yahoo! Inc.
6 ********************************************************************/
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/selfmt.h"
16 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
18 #define SIMPLE_PATTERN_STRING "feminine {feminineVerbValue} other{otherVerbValue}"
21 #define SELECT_PATTERN_DATA 4
22 #define SELECT_SYNTAX_DATA 10
23 #define EXP_FORMAT_RESULT_DATA 12
24 #define NUM_OF_FORMAT_ARGS 3
26 #define VERBOSE_INT(x) {logln("%s:%d: int %s=%d\n", __FILE__, __LINE__, #x, (x));}
27 #define VERBOSE_USTRING(text) {logln("%s:%d: UnicodeString %s(%d) = ", __FILE__, __LINE__, #text, text.length()); logln(UnicodeString(" \"")+text+UnicodeString("\";"));}
30 void SelectFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
32 if (exec) logln("TestSuite SelectFormat");
34 TESTCASE(0, selectFormatAPITest);
35 TESTCASE(1, selectFormatUnitTest);
42 * Unit tests of SelectFormat class.
44 void SelectFormatTest::selectFormatUnitTest(/*char *par*/)
46 const UnicodeString SIMPLE_PATTERN(SIMPLE_PATTERN_STRING); /* Don't static init this! */
48 UnicodeString patternTestData[SELECT_PATTERN_DATA] = {
49 UNICODE_STRING_SIMPLE("fem {femValue} other{even}"),
50 UNICODE_STRING_SIMPLE("other{odd or even}"),
51 UNICODE_STRING_SIMPLE("odd{The number {0, number, integer} is odd.}other{The number {0, number, integer} is even.}"),
52 UNICODE_STRING_SIMPLE("odd{The number {1} is odd}other{The number {1} is even}"),
55 UnicodeString formatArgs[NUM_OF_FORMAT_ARGS] = {
56 UNICODE_STRING_SIMPLE("fem"),
57 UNICODE_STRING_SIMPLE("other"),
58 UNICODE_STRING_SIMPLE("odd")
61 UnicodeString expFormatResult[][NUM_OF_FORMAT_ARGS] = {
63 UNICODE_STRING_SIMPLE("femValue"),
64 UNICODE_STRING_SIMPLE("even"),
65 UNICODE_STRING_SIMPLE("even")
68 UNICODE_STRING_SIMPLE("odd or even"),
69 UNICODE_STRING_SIMPLE("odd or even"),
70 UNICODE_STRING_SIMPLE("odd or even"),
73 UNICODE_STRING_SIMPLE("The number {0, number, integer} is even."),
74 UNICODE_STRING_SIMPLE("The number {0, number, integer} is even."),
75 UNICODE_STRING_SIMPLE("The number {0, number, integer} is odd."),
78 UNICODE_STRING_SIMPLE("The number {1} is even"),
79 UNICODE_STRING_SIMPLE("The number {1} is even"),
80 UNICODE_STRING_SIMPLE("The number {1} is odd"),
84 UnicodeString checkSyntaxData[SELECT_SYNTAX_DATA] = {
85 UNICODE_STRING_SIMPLE("odd{foo}"),
86 UNICODE_STRING_SIMPLE("*odd{foo} other{bar}"),
87 UNICODE_STRING_SIMPLE("odd{foo},other{bar}"),
88 UNICODE_STRING_SIMPLE("od d{foo} other{bar}"),
89 UNICODE_STRING_SIMPLE("odd{foo}{foobar}other{foo}"),
90 UNICODE_STRING_SIMPLE("odd{foo1}other{foo2}}"),
91 UNICODE_STRING_SIMPLE("odd{foo1}other{{foo2}"),
92 UNICODE_STRING_SIMPLE("odd{fo{o1}other{foo2}}")
95 UErrorCode status = U_ZERO_ERROR;
96 VERBOSE_USTRING(SIMPLE_PATTERN);
97 SelectFormat* selFmt = new SelectFormat( SIMPLE_PATTERN , status);
98 if (U_FAILURE(status)) {
99 dataerrln("ERROR: SelectFormat Unit Test constructor failed in unit tests.- exitting");
103 // ======= Test SelectFormat pattern syntax.
104 logln("SelectFormat Unit Test : Testing SelectFormat pattern syntax.");
105 for (int32_t i=0; i<SELECT_SYNTAX_DATA; ++i) {
106 status = U_ZERO_ERROR;
108 VERBOSE_USTRING(checkSyntaxData[i]);
109 selFmt->applyPattern(checkSyntaxData[i], status);
110 if (U_SUCCESS(status)){
111 errln("\nERROR: Unexpected result - SelectFormat Unit Test failed to detect syntax error with pattern: "+checkSyntaxData[i]);
115 // ICU 4.8 does not check for duplicate keywords any more.
116 status = U_ZERO_ERROR;
117 selFmt->applyPattern("odd{foo} odd{bar} other{foobar}", status);
118 FieldPosition format_ignore(FieldPosition::DONT_CARE);
119 UnicodeString format_result;
120 selFmt->format(UnicodeString("odd"), format_result, format_ignore, status);
121 assertEquals("should use first occurrence of the 'odd' keyword", "foo", format_result);
122 format_result.remove();
123 selFmt->applyPattern("odd{foo} other{bar} other{foobar}", status);
124 selFmt->format(UnicodeString("other"), format_result, format_ignore, status);
125 assertEquals("should use first occurrence of the 'other' keyword", "bar", format_result);
130 logln("SelectFormat Unit Test : Creating format object for Testing applying various patterns");
131 status = U_ZERO_ERROR;
132 selFmt = new SelectFormat( SIMPLE_PATTERN , status);
133 //SelectFormat* selFmt1 = new SelectFormat( SIMPLE_PATTERN , status);
134 if (U_FAILURE(status)) {
135 errln("ERROR: SelectFormat Unit Test constructor failed in unit tests.- exitting");
139 // ======= Test applying and formatting with various pattern
140 logln("SelectFormat Unit test: Testing applyPattern() and format() ...");
141 UnicodeString result;
142 FieldPosition ignore(FieldPosition::DONT_CARE);
144 for(int32_t i=0; i<SELECT_PATTERN_DATA; ++i) {
145 status = U_ZERO_ERROR;
146 selFmt->applyPattern(patternTestData[i], status);
147 if (U_FAILURE(status)) {
148 errln("ERROR: SelectFormat Unit Test failed to apply pattern- "+patternTestData[i] );
152 //Format with the keyword array
153 for(int32_t j=0; j<3; j++) {
155 selFmt->format( formatArgs[j], result , ignore , status);
156 if (U_FAILURE(status)) {
157 errln("ERROR: SelectFormat Unit test failed in format() with argument: "+ formatArgs[j] + " and error is " + u_errorName(status) );
159 if( result != expFormatResult[i][j] ){
160 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] );
166 //Test with an invalid keyword
167 // one which contains Pattern_Syntax or Pattern_White_Space.
168 logln("SelectFormat Unit test: Testing format() with keyword method and with invalid keywords...");
169 status = U_ZERO_ERROR;
171 UnicodeString keywords[] = {
185 selFmt = new SelectFormat( SIMPLE_PATTERN , status);
186 for (int32_t i = 0; i < LENGTHOF(keywords); i++ ){
187 status = U_ZERO_ERROR;
188 selFmt->format( keywords[i], result , ignore , status);
189 if (!U_FAILURE(status)) {
190 errln("ERROR: SelectFormat Unit test failed in format() with keyWord and with an invalid keyword as : "+
191 keywords[i]+" ("+u_errorName(status)+")");
199 * Test various generic API methods of SelectFormat for Basic API usage.
200 * This is to make sure the API test coverage is 100% .
202 void SelectFormatTest::selectFormatAPITest(/*char *par*/)
204 const UnicodeString SIMPLE_PATTERN(SIMPLE_PATTERN_STRING); /* Don't static init this! */
205 int numOfConstructors =3;
206 UErrorCode status[3];
207 SelectFormat* selFmt[3] = { NULL, NULL, NULL };
209 // ========= Test constructors
210 logln("SelectFormat API test: Testing SelectFormat constructors ...");
211 for (int32_t i=0; i< numOfConstructors; ++i) {
212 status[i] = U_ZERO_ERROR;
215 selFmt[0]= new SelectFormat(SIMPLE_PATTERN, status[0]);
216 if ( U_FAILURE(status[0]) ) {
217 errln("ERROR: SelectFormat API test constructor with pattern and status failed! with %s\n", u_errorName(status[0]));
221 // =========== Test copy constructor
222 logln("SelectFormat API test: Testing copy constructor and == operator ...");
223 SelectFormat fmt = *selFmt[0];
224 SelectFormat* dupPFmt = new SelectFormat(fmt);
225 if ((*selFmt[0]) != (*dupPFmt)) {
226 errln("ERROR: SelectFormat API test Failed in copy constructor or == operator!");
230 // ======= Test clone && == operator.
231 logln("SelectFormat API test: Testing clone and == operator ...");
232 if ( U_SUCCESS(status[0]) ) {
233 selFmt[1] = (SelectFormat*)selFmt[0]->clone();
234 if (selFmt[1]!=NULL) {
235 if ( *selFmt[1] != *selFmt[0] ) {
236 errln("ERROR: SelectFormat API test clone test failed!");
239 errln("ERROR: SelectFormat API test clone test failed with NULL!");
243 errln("ERROR: could not create [0]: %s\n", u_errorName(status[0]));
247 // ======= Test assignment operator && == operator.
248 logln("SelectFormat API test: Testing assignment operator and == operator ...");
249 selFmt[2]= new SelectFormat(SIMPLE_PATTERN, status[2]);
250 if ( U_SUCCESS(status[2]) ) {
251 *selFmt[1] = *selFmt[2];
252 if (selFmt[1]!=NULL) {
253 if ( (*selFmt[1] != *selFmt[2]) ) {
254 errln("ERROR: SelectFormat API test assignment operator test failed!");
260 errln("ERROR: SelectFormat constructor failed in assignment operator!");
265 // ======= Test getStaticClassID() and getStaticClassID()
266 logln("SelectFormat API test: Testing getStaticClassID() and getStaticClassID() ...");
267 UErrorCode status1 = U_ZERO_ERROR;
268 SelectFormat* selFmt1 = new SelectFormat( SIMPLE_PATTERN , status1);
269 if( U_FAILURE(status1)) {
270 errln("ERROR: SelectFormat constructor failed in staticClassID test! Exitting");
274 logln("Testing getStaticClassID()");
275 if(selFmt1->getDynamicClassID() !=SelectFormat::getStaticClassID()) {
276 errln("ERROR: SelectFormat API test getDynamicClassID() didn't return the expected value");
279 // ======= Test applyPattern() and toPattern()
280 logln("SelectFormat API test: Testing applyPattern() and toPattern() ...");
281 UnicodeString pattern = UnicodeString("masculine{masculineVerbValue} other{otherVerbValue}");
282 status1 = U_ZERO_ERROR;
283 selFmt1->applyPattern( pattern, status1);
284 if (U_FAILURE(status1)) {
285 errln("ERROR: SelectFormat API test failed in applyPattern() with pattern: "+ pattern);
287 UnicodeString checkPattern;
288 selFmt1->toPattern( checkPattern);
289 if( checkPattern != pattern ){
290 errln("ERROR: SelectFormat API test failed in toPattern() with unexpected result with pattern: "+ pattern);
294 // ======= Test different format() methods
295 logln("SelectFormat API test: Testing format() with keyword method ...");
296 status1 = U_ZERO_ERROR;
297 UnicodeString result;
298 FieldPosition ignore(FieldPosition::DONT_CARE);
299 UnicodeString keyWord = UnicodeString("masculine");
301 selFmt1->format( keyWord, result , ignore , status1);
302 if (U_FAILURE(status1)) {
303 errln("ERROR: SelectFormat API test failed in format() with keyWord: "+ keyWord);
305 UnicodeString expected=UnicodeString("masculineVerbValue");
306 if( result != expected ){
307 errln("ERROR: SelectFormat API test failed in format() with unexpected result with keyWord: "+ keyWord);
311 logln("SelectFormat API test: Testing format() with Formattable obj method ...");
312 status1 = U_ZERO_ERROR;
314 UnicodeString result1;
315 Formattable testArgs = Formattable("other");
316 selFmt1->format( testArgs, result1 , ignore , status1);
317 if (U_FAILURE(status1)) {
318 errln("ERROR: SelectFormat API test failed in format() with Formattable");
320 UnicodeString expected=UnicodeString("otherVerbValue");
321 if( result1 != expected ){
322 errln("ERROR: SelectFormat API test failed in format() with unexpected result with Formattable");
329 #endif /* #if !UCONFIG_NO_FORMATTING */