2 *******************************************************************************
3 * Copyright (C) 2014, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 * File SIMPLEPATTERNFORMATTERTEST.CPP
9 ********************************************************************************
13 #include "simplepatternformatter.h"
15 #define LENGTHOF(array) (int32_t)(sizeof(array) / sizeof((array)[0]))
17 class SimplePatternFormatterTest
: public IntlTest
{
19 SimplePatternFormatterTest() {
21 void TestNoPlaceholders();
22 void TestOnePlaceholder();
23 void TestManyPlaceholders();
24 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=0);
28 void SimplePatternFormatterTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) {
30 TESTCASE_AUTO(TestNoPlaceholders
);
31 TESTCASE_AUTO(TestOnePlaceholder
);
32 TESTCASE_AUTO(TestManyPlaceholders
);
36 void SimplePatternFormatterTest::TestNoPlaceholders() {
37 UErrorCode status
= U_ZERO_ERROR
;
38 SimplePatternFormatter
fmt("This doesn''t have templates '{0}");
39 assertEquals("PlaceholderCount", 0, fmt
.getPlaceholderCount());
40 UnicodeString appendTo
;
43 "This doesn't have templates {0}",
44 fmt
.format("unused", appendTo
, status
));
45 fmt
.compile("This has {} bad {012d placeholders", status
);
46 assertEquals("PlaceholderCount", 0, fmt
.getPlaceholderCount());
50 "This has {} bad {012d placeholders",
51 fmt
.format("unused", appendTo
, status
));
52 assertSuccess("Status", status
);
55 void SimplePatternFormatterTest::TestOnePlaceholder() {
56 UErrorCode status
= U_ZERO_ERROR
;
57 SimplePatternFormatter fmt
;
58 fmt
.compile("{0} meter", status
);
59 assertEquals("PlaceholderCount", 1, fmt
.getPlaceholderCount());
60 UnicodeString appendTo
;
64 fmt
.format("1", appendTo
, status
));
65 assertSuccess("Status", status
);
68 SimplePatternFormatter s
;
74 s
.format("1", appendTo
, status
));
77 SimplePatternFormatter
r(fmt
);
82 r
.format("1", appendTo
, status
));
83 assertSuccess("Status", status
);
86 void SimplePatternFormatterTest::TestManyPlaceholders() {
87 UErrorCode status
= U_ZERO_ERROR
;
88 SimplePatternFormatter fmt
;
90 "Templates {2}{1}{5} and {4} are out of order.", status
);
91 assertEquals("PlaceholderCount", 6, fmt
.getPlaceholderCount());
92 UnicodeString values
[] = {
93 "freddy", "tommy", "frog", "billy", "leg", "{0}"};
94 UnicodeString
*params
[] = {
95 &values
[0], &values
[1], &values
[2], &values
[3], &values
[4], &values
[5]};
97 int32_t expectedOffsets
[6] = {-1, 22, 18, -1, 35, 27};
98 UnicodeString
appendTo("Prefix: ");
101 "Prefix: Templates frogtommy{0} and leg are out of order.",
109 assertSuccess("Status", status
);
110 for (int32_t i
= 0; i
< LENGTHOF(expectedOffsets
); ++i
) {
111 if (expectedOffsets
[i
] != offsets
[i
]) {
112 errln("Expected %d, got %d", expectedOffsets
[i
], offsets
[i
]);
118 LENGTHOF(params
) - 1,
123 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
124 errln("Expected U_ILLEGAL_ARGUMENT_ERROR");
126 status
= U_ZERO_ERROR
;
127 offsets
[LENGTHOF(offsets
) - 1] = 289;
134 LENGTHOF(offsets
) - 1,
136 assertEquals("Offsets buffer length", 289, offsets
[LENGTHOF(offsets
) - 1]);
139 SimplePatternFormatter s
;
144 "Templates frogtommy{0} and leg are out of order.",
154 SimplePatternFormatter
r(fmt
);
158 "Templates frogtommy{0} and leg are out of order.",
166 r
.compile("{0} meter", status
);
167 assertEquals("PlaceholderCount", 1, r
.getPlaceholderCount());
170 "Replace with new compile",
172 r
.format("freddy", appendTo
, status
));
173 r
.compile("{0}, {1}", status
);
174 assertEquals("PlaceholderCount", 2, r
.getPlaceholderCount());
179 r
.format("foo", "bar", appendTo
, status
));
180 r
.compile("{0}, {1} and {2}", status
);
181 assertEquals("PlaceholderCount", 3, r
.getPlaceholderCount());
186 r
.format("foo", "bar", "baz", appendTo
, status
));
187 assertSuccess("Status", status
);
190 extern IntlTest
*createSimplePatternFormatterTest() {
191 return new SimplePatternFormatterTest();