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