]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/simplepatternformattertest.cpp
ICU-531.30.tar.gz
[apple/icu.git] / icuSources / test / intltest / simplepatternformattertest.cpp
CommitLineData
57a6839d
A
1/*
2*******************************************************************************
3* Copyright (C) 2014, International Business Machines Corporation and *
4* others. All Rights Reserved. *
5*******************************************************************************
6*
7* File SIMPLEPATTERNFORMATTERTEST.CPP
8*
9********************************************************************************
10*/
11#include "cstring.h"
12#include "intltest.h"
13#include "simplepatternformatter.h"
14
15#define LENGTHOF(array) (int32_t)(sizeof(array) / sizeof((array)[0]))
16
17class SimplePatternFormatterTest : public IntlTest {
18public:
19 SimplePatternFormatterTest() {
20 }
21 void TestNoPlaceholders();
22 void TestOnePlaceholder();
23 void TestManyPlaceholders();
24 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
25private:
26};
27
28void SimplePatternFormatterTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
29 TESTCASE_AUTO_BEGIN;
30 TESTCASE_AUTO(TestNoPlaceholders);
31 TESTCASE_AUTO(TestOnePlaceholder);
32 TESTCASE_AUTO(TestManyPlaceholders);
33 TESTCASE_AUTO_END;
34}
35
36void 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;
41 assertEquals(
42 "Evaluate",
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());
47 appendTo.remove();
48 assertEquals(
49 "Evaluate",
50 "This has {} bad {012d placeholders",
51 fmt.format("unused", appendTo, status));
52 assertSuccess("Status", status);
53}
54
55void 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;
61 assertEquals(
62 "Evaluate",
63 "1 meter",
64 fmt.format("1", appendTo, status));
65 assertSuccess("Status", status);
66
67 // assignment
68 SimplePatternFormatter s;
69 s = fmt;
70 appendTo.remove();
71 assertEquals(
72 "Assignment",
73 "1 meter",
74 s.format("1", appendTo, status));
75
76 // Copy constructor
77 SimplePatternFormatter r(fmt);
78 appendTo.remove();
79 assertEquals(
80 "Copy constructor",
81 "1 meter",
82 r.format("1", appendTo, status));
83 assertSuccess("Status", status);
84}
85
86void SimplePatternFormatterTest::TestManyPlaceholders() {
87 UErrorCode status = U_ZERO_ERROR;
88 SimplePatternFormatter fmt;
89 fmt.compile(
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]};
96 int32_t offsets[6];
97 int32_t expectedOffsets[6] = {-1, 22, 18, -1, 35, 27};
98 UnicodeString appendTo("Prefix: ");
99 assertEquals(
100 "Evaluate",
101 "Prefix: Templates frogtommy{0} and leg are out of order.",
102 fmt.format(
103 params,
104 LENGTHOF(params),
105 appendTo,
106 offsets,
107 LENGTHOF(offsets),
108 status));
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]);
113 }
114 }
115 appendTo.remove();
116 fmt.format(
117 params,
118 LENGTHOF(params) - 1,
119 appendTo,
120 offsets,
121 LENGTHOF(offsets),
122 status);
123 if (status != U_ILLEGAL_ARGUMENT_ERROR) {
124 errln("Expected U_ILLEGAL_ARGUMENT_ERROR");
125 }
126 status = U_ZERO_ERROR;
127 offsets[LENGTHOF(offsets) - 1] = 289;
128 appendTo.remove();
129 fmt.format(
130 params,
131 LENGTHOF(params),
132 appendTo,
133 offsets,
134 LENGTHOF(offsets) - 1,
135 status);
136 assertEquals("Offsets buffer length", 289, offsets[LENGTHOF(offsets) - 1]);
137
138 // Test assignment
139 SimplePatternFormatter s;
140 s = fmt;
141 appendTo.remove();
142 assertEquals(
143 "Assignment",
144 "Templates frogtommy{0} and leg are out of order.",
145 s.format(
146 params,
147 LENGTHOF(params),
148 appendTo,
149 NULL,
150 0,
151 status));
152
153 // Copy constructor
154 SimplePatternFormatter r(fmt);
155 appendTo.remove();
156 assertEquals(
157 "Copy constructor",
158 "Templates frogtommy{0} and leg are out of order.",
159 r.format(
160 params,
161 LENGTHOF(params),
162 appendTo,
163 NULL,
164 0,
165 status));
166 r.compile("{0} meter", status);
167 assertEquals("PlaceholderCount", 1, r.getPlaceholderCount());
168 appendTo.remove();
169 assertEquals(
170 "Replace with new compile",
171 "freddy meter",
172 r.format("freddy", appendTo, status));
173 r.compile("{0}, {1}", status);
174 assertEquals("PlaceholderCount", 2, r.getPlaceholderCount());
175 appendTo.remove();
176 assertEquals(
177 "2 arg",
178 "foo, bar",
179 r.format("foo", "bar", appendTo, status));
180 r.compile("{0}, {1} and {2}", status);
181 assertEquals("PlaceholderCount", 3, r.getPlaceholderCount());
182 appendTo.remove();
183 assertEquals(
184 "3 arg",
185 "foo, bar and baz",
186 r.format("foo", "bar", "baz", appendTo, status));
187 assertSuccess("Status", status);
188}
189
190extern IntlTest *createSimplePatternFormatterTest() {
191 return new SimplePatternFormatterTest();
192}