1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2014, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 * Copyright (C) 2010 , Yahoo! Inc.
8 ********************************************************************/
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
16 #include "unicode/selfmt.h"
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) 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
35 void SelectFormatTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
37 if (exec
) logln("TestSuite SelectFormat");
39 TESTCASE(0, selectFormatAPITest
);
40 TESTCASE(1, selectFormatUnitTest
);
47 * Unit tests of SelectFormat class.
49 void SelectFormatTest::selectFormatUnitTest(/*char *par*/)
51 const UnicodeString
SIMPLE_PATTERN(SIMPLE_PATTERN_STRING
); /* Don't static init this! */
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}"),
60 UnicodeString formatArgs
[NUM_OF_FORMAT_ARGS
] = {
61 UNICODE_STRING_SIMPLE("fem"),
62 UNICODE_STRING_SIMPLE("other"),
63 UNICODE_STRING_SIMPLE("odd")
66 UnicodeString expFormatResult
[][NUM_OF_FORMAT_ARGS
] = {
68 UNICODE_STRING_SIMPLE("femValue"),
69 UNICODE_STRING_SIMPLE("even"),
70 UNICODE_STRING_SIMPLE("even")
73 UNICODE_STRING_SIMPLE("odd or even"),
74 UNICODE_STRING_SIMPLE("odd or even"),
75 UNICODE_STRING_SIMPLE("odd or even"),
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."),
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"),
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}}")
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.- exitting");
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
;
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
]);
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
);
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.- exitting");
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
);
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
] );
157 //Format with the keyword array
158 for(int32_t j
=0; j
<3; j
++) {
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
) );
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
] );
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
;
176 UnicodeString keywords
[] = {
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
)+")");
204 * Test various generic API methods of SelectFormat for Basic API usage.
205 * This is to make sure the API test coverage is 100% .
207 void SelectFormatTest::selectFormatAPITest(/*char *par*/)
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
};
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
;
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]));
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!");
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!");
244 errln("ERROR: SelectFormat API test clone test failed with NULL!");
248 errln("ERROR: could not create [0]: %s\n", u_errorName(status
[0]));
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!");
265 errln("ERROR: SelectFormat constructor failed in assignment operator!");
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! Exitting");
279 logln("Testing getStaticClassID()");
280 if(selFmt1
->getDynamicClassID() !=SelectFormat::getStaticClassID()) {
281 errln("ERROR: SelectFormat API test getDynamicClassID() didn't return the expected value");
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
);
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
);
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");
306 selFmt1
->format( keyWord
, result
, ignore
, status1
);
307 if (U_FAILURE(status1
)) {
308 errln("ERROR: SelectFormat API test failed in format() with keyWord: "+ keyWord
);
310 UnicodeString expected
=UnicodeString("masculineVerbValue");
311 if( result
!= expected
){
312 errln("ERROR: SelectFormat API test failed in format() with unexpected result with keyWord: "+ keyWord
);
316 logln("SelectFormat API test: Testing format() with Formattable obj method ...");
317 status1
= U_ZERO_ERROR
;
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");
325 UnicodeString expected
=UnicodeString("otherVerbValue");
326 if( result1
!= expected
){
327 errln("ERROR: SelectFormat API test failed in format() with unexpected result with Formattable");
334 #endif /* #if !UCONFIG_NO_FORMATTING */