1 #include <muleunit/test.h>
2 #include <common/Format.h>
7 using namespace muleunit
;
9 // Note: These tests have been written in accordance with the
10 // capabilities of printf found in printf(3).
12 #define ELEMENTS(x) (sizeof(x)/sizeof(x[0]))
13 #define MIN(x) std::numeric_limits<x>::min()
14 #define MAX(x) std::numeric_limits<x>::max()
18 // Less is valid for the string type, so we need a cut
19 // down test for that.
20 void testFormat(const wxString
& str
, const wxChar
* value
) {
21 for (int p
= -1; p
< 5; ++p
) {
29 format
+= wxString::Format(wxT(".%d"), p
);
34 wxString reference
= wxString::Format(format
, value
);
35 wxString actual
= CFormat(format
) % value
;
37 ASSERT_EQUALS_M(reference
, actual
, format
+ wxT(": '") + reference
+ wxT("' != '") + actual
+ wxT("'"));
43 TEST(Format
, ConstructorAndGetString
)
46 // Null should be valid
48 ASSERT_TRUE(fmt1
.IsReady());
49 ASSERT_EQUALS(wxT(""), fmt1
.GetString());
53 // Empty string should be valid
54 CFormat
fmt2(wxT(""));
55 ASSERT_TRUE(fmt2
.IsReady());
56 ASSERT_EQUALS(wxT(""), fmt2
.GetString());
60 // Simple string with no format fields
61 CFormat
fmt3(wxT("a b c"));
62 ASSERT_TRUE(fmt3
.IsReady());
63 ASSERT_EQUALS(wxT("a b c"), fmt3
.GetString());
67 // Simple string with one format field
68 CFormat
fmt4(wxT("a %i c"));
69 ASSERT_FALSE(fmt4
.IsReady());
70 ASSERT_RAISES(CAssertFailureException
, fmt4
.GetString());
73 ASSERT_EQUALS(wxT("a %i c"), fmt4
.GetString());
78 TEST(Format
, SetStringAndGetString
)
81 ASSERT_TRUE(format
.IsReady());
82 ASSERT_EQUALS(wxT(""), format
.GetString());
84 // Empty string should be valid
85 format
= CFormat(wxT(""));
86 ASSERT_TRUE(format
.IsReady());
87 ASSERT_EQUALS(wxT(""), format
.GetString());
89 // Simple string with no format fields
90 format
= CFormat(wxT("a b c"));
91 ASSERT_TRUE(format
.IsReady());
92 ASSERT_EQUALS(wxT("a b c"), format
.GetString());
94 // Simple string with one format field
95 format
= CFormat(wxT("a %i c"));
96 ASSERT_FALSE(format
.IsReady());
97 ASSERT_RAISES(CAssertFailureException
, format
.GetString());
100 ASSERT_EQUALS(wxT("a %i c"), format
.GetString());
105 //! Implementation for the Standard type test
106 #define STANDARD_TEST(cformat, wxformat, value) \
108 wxString reference = wxString::Format(wxString(wxT("%")) + wxformat, value); \
109 wxString actual = CFormat(wxString(wxT("%")) + cformat) % value; \
110 ASSERT_EQUALS_M(reference, actual, wxString(wxT("%")) << wxformat \
111 << wxT(" vs. %") << cformat << wxT(": '") + reference + wxT("' != '") + actual + wxT("'")); \
116 //! Test the two boundries and a middle value of the specificed format
117 #define STANDARD_TYPE_TESTS(cformat, wxformat, type) \
118 STANDARD_TEST(cformat, wxformat, MIN(type)); \
119 STANDARD_TEST(cformat, wxformat, (type)(MAX(type) / 2)); \
120 STANDARD_TEST(cformat, wxformat, MAX(type)); \
122 TEST(Format, InjectwxChar)
124 STANDARD_TYPE_TESTS(wxT("c"), wxT("c"), wxChar
);
128 //! All length specifiers are supported and should yield the same result
129 const wxChar
* int_lengths
[] =
138 //! All signed types are supported, and should yield the same result
139 const wxChar
* sint_types
[] =
147 //! All unsigned types are supported, and should yield the same result
148 const wxChar
* uint_types
[] =
158 TEST(Format
, InjectInteger
)
160 const wxChar
** sint_entry
= sint_types
;
162 while (*sint_entry
) {
163 const wxChar
** len_entry
= int_lengths
;
166 wxString entry
= wxString() << *len_entry
<< *sint_entry
;
168 STANDARD_TYPE_TESTS(entry
, wxString() << wxT("h") << *sint_entry
, signed short);
169 STANDARD_TYPE_TESTS(entry
, wxString() << wxT("") << *sint_entry
, signed int);
170 STANDARD_TYPE_TESTS(entry
, wxString() << wxT("l") << *sint_entry
, signed long);
171 STANDARD_TYPE_TESTS(entry
, wxString() << wxLongLongFmtSpec
<< *sint_entry
, signed long long);
180 const wxChar
** uint_entry
= uint_types
;
181 while (*uint_entry
) {
182 const wxChar
** len_entry
= int_lengths
;
185 wxString entry
= wxString() << *len_entry
<< *uint_entry
;
187 STANDARD_TYPE_TESTS(entry
, wxString() << wxT("h") << *uint_entry
, unsigned short);
188 STANDARD_TYPE_TESTS(entry
, wxString() << wxT("") << *uint_entry
, unsigned int);
189 STANDARD_TYPE_TESTS(entry
, wxString() << wxT("l") << *uint_entry
, unsigned long);
190 STANDARD_TYPE_TESTS(entry
, wxString() << wxLongLongFmtSpec
<< *uint_entry
, unsigned long long);
200 TEST(Format
, InjectFloatAndDouble
)
202 STANDARD_TYPE_TESTS(wxT("e"), wxT("e"), float);
203 STANDARD_TYPE_TESTS(wxT("E"), wxT("E"), float);
204 STANDARD_TYPE_TESTS(wxT("f"), wxT("f"), float);
205 STANDARD_TYPE_TESTS(wxT("F"), wxT("F"), float);
206 STANDARD_TYPE_TESTS(wxT("g"), wxT("g"), float);
207 STANDARD_TYPE_TESTS(wxT("G"), wxT("G"), float);
209 STANDARD_TYPE_TESTS(wxT("e"), wxT("e"), double);
210 STANDARD_TYPE_TESTS(wxT("E"), wxT("E"), double);
211 STANDARD_TYPE_TESTS(wxT("f"), wxT("f"), double);
212 STANDARD_TYPE_TESTS(wxT("F"), wxT("F"), double);
213 STANDARD_TYPE_TESTS(wxT("g"), wxT("g"), double);
214 STANDARD_TYPE_TESTS(wxT("G"), wxT("G"), double);
218 TEST(Format
, InjectString
)
220 testFormat(wxT("s"), wxT(""));
221 testFormat(wxT("s"), wxT("abc"));
225 TEST(Format
, InjectNULLString
)
227 for (int p
= -1; p
< 5; ++p
) {
228 wxString format
= wxT("%");
233 format
+= wxString::Format(wxT(".%d"), p
);
238 wxString actual
= CFormat(format
) % (const wxChar
*)NULL
;
240 ASSERT_TRUE_M(actual
.IsEmpty(), wxT("Expected empty string, got '") + actual
+ wxT("'"));
245 TEST(Format
, MultipleFields
)
248 CFormat
fmt1(wxT("%d _ %u _ %i"));
250 ASSERT_TRUE(fmt1
.IsReady());
251 ASSERT_EQUALS(wxT("-1 _ 2 _ -4"), fmt1
.GetString());
255 CFormat
fmt2(wxT("%d _ %u _ %i"));
256 ASSERT_FALSE(fmt2
.IsReady());
258 ASSERT_FALSE(fmt2
.IsReady());
260 ASSERT_FALSE(fmt2
.IsReady());
262 ASSERT_TRUE(fmt2
.IsReady());
263 ASSERT_EQUALS(wxT("-1 _ 2 _ -4"), fmt2
.GetString());
267 // Test grouped fields
268 CFormat
fmt3(wxT("%d%u%i"));
270 ASSERT_EQUALS(wxT("-12-4"), fmt3
.GetString());
275 TEST(Format
, EscapedPercentageSign
)
278 CFormat
fmt1(wxT("%%"));
279 ASSERT_EQUALS(wxT("%"), fmt1
.GetString());
283 CFormat
fmt2(wxT("-- %% --"));
284 ASSERT_EQUALS(wxT("-- % --"), fmt2
.GetString());
288 CFormat
fmt3(wxT("%d _ %% _ %i"));
290 ASSERT_TRUE(fmt3
.IsReady());
291 ASSERT_EQUALS(wxT("1 _ % _ 2"), fmt3
.GetString());
295 CFormat
fmt4(wxT("%% _ %% _ %%"));
296 ASSERT_EQUALS(wxT("% _ % _ %"), fmt4
.GetString());
301 ///////////////////////////////////////////////////////////
302 // The following checks for invalid operations
304 TEST(Format
, MalformedFields
)
307 // Incomplete format string
308 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%")));
309 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT(" -- %")));
312 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%q")) % 1);
313 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT(" -- %q")) % 1.0f
);
314 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT(" -- %q -- ")) % wxT("1"));
316 // Invalid string length
317 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%.qs")) % wxT(""));
318 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%.-10s")) % wxT(""));
324 ASSERT_EQUALS(wxT("%"), CFormat(wxT("%")));
325 ASSERT_EQUALS(wxT(" -- %"), CFormat(wxT(" -- %")));
328 ASSERT_EQUALS(wxT("%q"), CFormat(wxT("%q")) % 1);
329 ASSERT_EQUALS(wxT(" -- %q"), CFormat(wxT(" -- %q")) % 1.0f
);
330 ASSERT_EQUALS(wxT(" -- %q -- "), CFormat(wxT(" -- %q -- ")) % wxT("1"));
332 // Invalid string length
333 ASSERT_EQUALS(wxT("%.qs"), CFormat(wxT("%.qs")) % wxT(""));
334 ASSERT_EQUALS(wxT("%.-10s"), CFormat(wxT("%.-10s")) % wxT(""));
336 // Wrong and right arguments
337 ASSERT_EQUALS(wxT("%s -- 17"), CFormat(wxT("%s -- %i")) % 1 % 17);
342 TEST(Format
, NotReady
)
344 CFormat
fmt(wxT("-- %s - %d"));
345 ASSERT_FALSE(fmt
.IsReady());
346 ASSERT_RAISES(CAssertFailureException
, fmt
.GetString());
350 ASSERT_EQUALS(wxT("-- %s - %d"), fmt
);
355 ASSERT_FALSE(fmt
.IsReady());
356 ASSERT_RAISES(CAssertFailureException
, fmt
.GetString());
360 ASSERT_EQUALS(wxT("-- foo - %d"), fmt
);
365 ASSERT_TRUE(fmt
.IsReady());
366 ASSERT_EQUALS(wxT("-- foo - 42"), fmt
.GetString());
370 TEST(Format
, WrongTypes
)
373 // Entirely wrong types:
374 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%s")) % 1);
375 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%s")) % 1.0f
);
376 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%s")) % wxT('1'));
378 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%f")) % 1);
379 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%f")) % wxT('1'));
380 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%f")) % wxT("1"));
382 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%d")) % 1.0f
);
383 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%d")) % wxT("1"));
389 ASSERT_EQUALS(wxT("-- %s -- 42 --"), CFormat(wxT("-- %s -- %u --")) % 1 % 42);
390 ASSERT_EQUALS(wxT("-- %f -- 42 --"), CFormat(wxT("-- %f -- %u --")) % 1 % 42);
391 ASSERT_EQUALS(wxT("-- %d -- 42 --"), CFormat(wxT("-- %d -- %u --")) % 1.0f
% 42);
396 TEST(Format
, NotSupported
)
399 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%*d")) % 1);
400 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%*s")) % wxT(""));
401 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%p")) % wxT(""));
402 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%n")) % wxT(""));
408 ASSERT_EQUALS(wxT("%*d"), CFormat(wxT("%*d")) % 1);
409 ASSERT_EQUALS(wxT("%*s"), CFormat(wxT("%*s")) % wxT(""));
410 ASSERT_EQUALS(wxT("%p"), CFormat(wxT("%p")) % wxT(""));
411 ASSERT_EQUALS(wxT("%n"), CFormat(wxT("%n")) % wxT(""));
416 TEST(Format
, Overfeeding
)
418 CFormat
fmt(wxT("%d - %d"));
419 ASSERT_FALSE(fmt
.IsReady());
421 ASSERT_TRUE(fmt
.IsReady());
422 ASSERT_EQUALS(wxT("1 - 2"), fmt
.GetString());
424 ASSERT_RAISES(CAssertFailureException
, fmt
% 1);
425 ASSERT_TRUE(fmt
.IsReady());
426 ASSERT_EQUALS(wxT("1 - 2"), fmt
.GetString());
428 ASSERT_RAISES(CAssertFailureException
, fmt
% 1.0f
);
429 ASSERT_TRUE(fmt
.IsReady());
430 ASSERT_EQUALS(wxT("1 - 2"), fmt
.GetString());
432 ASSERT_RAISES(CAssertFailureException
, fmt
% wxT("1"));
433 ASSERT_TRUE(fmt
.IsReady());
434 ASSERT_EQUALS(wxT("1 - 2"), fmt
.GetString());
438 TEST(Format
, 64bValues
)
441 CFormat
fmt(wxT("%lli - %lli"));
442 fmt
% MIN(sint64
) % MAX(sint64
);
443 ASSERT_EQUALS(wxT("-9223372036854775808 - 9223372036854775807"), fmt
.GetString());
447 CFormat
fmt(wxT("%llu - %llu"));
448 fmt
% MIN(uint64
) % MAX(uint64
);
449 ASSERT_EQUALS(wxT("0 - 18446744073709551615"), fmt
.GetString());
454 class CTestPrintable
: public CPrintable
457 CTestPrintable(int value
)
462 virtual wxString
GetPrintableString() const
464 return wxString::Format(wxT("%i"), m_value
);
471 TEST(Format
, Printable
)
474 CFormat
fmt(wxT("%s"));
475 fmt
% CTestPrintable(10);
476 ASSERT_EQUALS(wxT("10"), fmt
.GetString());
480 CFormat
fmt(wxT("%s"));
481 fmt
% CTestPrintable(-10);
482 ASSERT_EQUALS(wxT("-10"), fmt
.GetString());