OLD | NEW |
(Empty) | |
| 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 class SimplePatternFormatterTest : public IntlTest { |
| 16 public: |
| 17 SimplePatternFormatterTest() { |
| 18 } |
| 19 void TestNoPlaceholders(); |
| 20 void TestOnePlaceholder(); |
| 21 void TestManyPlaceholders(); |
| 22 void TestGetPatternWithNoPlaceholders(); |
| 23 void TestOptimization(); |
| 24 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=
0); |
| 25 private: |
| 26 }; |
| 27 |
| 28 void 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(TestGetPatternWithNoPlaceholders); |
| 34 TESTCASE_AUTO(TestOptimization); |
| 35 TESTCASE_AUTO_END; |
| 36 } |
| 37 |
| 38 void SimplePatternFormatterTest::TestNoPlaceholders() { |
| 39 UErrorCode status = U_ZERO_ERROR; |
| 40 SimplePatternFormatter fmt("This doesn''t have templates '{0}"); |
| 41 assertEquals("PlaceholderCount", 0, fmt.getPlaceholderCount()); |
| 42 UnicodeString appendTo; |
| 43 assertEquals( |
| 44 "format", |
| 45 "This doesn't have templates {0}", |
| 46 fmt.format("unused", appendTo, status)); |
| 47 fmt.compile("This has {} bad {012d placeholders", status); |
| 48 assertEquals("PlaceholderCount", 0, fmt.getPlaceholderCount()); |
| 49 appendTo.remove(); |
| 50 assertEquals( |
| 51 "format", |
| 52 "This has {} bad {012d placeholders", |
| 53 fmt.format("unused", appendTo, status)); |
| 54 assertSuccess("Status", status); |
| 55 } |
| 56 |
| 57 void SimplePatternFormatterTest::TestOnePlaceholder() { |
| 58 UErrorCode status = U_ZERO_ERROR; |
| 59 SimplePatternFormatter fmt; |
| 60 fmt.compile("{0} meter", status); |
| 61 assertEquals("PlaceholderCount", 1, fmt.getPlaceholderCount()); |
| 62 UnicodeString appendTo; |
| 63 assertEquals( |
| 64 "format", |
| 65 "1 meter", |
| 66 fmt.format("1", appendTo, status)); |
| 67 assertSuccess("Status", status); |
| 68 |
| 69 // assignment |
| 70 SimplePatternFormatter s; |
| 71 s = fmt; |
| 72 appendTo.remove(); |
| 73 assertEquals( |
| 74 "Assignment", |
| 75 "1 meter", |
| 76 s.format("1", appendTo, status)); |
| 77 |
| 78 // Copy constructor |
| 79 SimplePatternFormatter r(fmt); |
| 80 appendTo.remove(); |
| 81 assertEquals( |
| 82 "Copy constructor", |
| 83 "1 meter", |
| 84 r.format("1", appendTo, status)); |
| 85 assertSuccess("Status", status); |
| 86 } |
| 87 |
| 88 void SimplePatternFormatterTest::TestManyPlaceholders() { |
| 89 UErrorCode status = U_ZERO_ERROR; |
| 90 SimplePatternFormatter fmt; |
| 91 fmt.compile( |
| 92 "Templates {2}{1}{5} and {4} are out of order.", status); |
| 93 assertSuccess("Status", status); |
| 94 assertFalse("startsWithPlaceholder", fmt.startsWithPlaceholder(2)); |
| 95 assertEquals("PlaceholderCount", 6, fmt.getPlaceholderCount()); |
| 96 UnicodeString values[] = { |
| 97 "freddy", "tommy", "frog", "billy", "leg", "{0}"}; |
| 98 UnicodeString *params[] = { |
| 99 &values[0], &values[1], &values[2], &values[3], &values[4], &values[5
]}; |
| 100 int32_t offsets[6]; |
| 101 int32_t expectedOffsets[6] = {-1, 22, 18, -1, 35, 27}; |
| 102 UnicodeString appendTo("Prefix: "); |
| 103 assertEquals( |
| 104 "format", |
| 105 "Prefix: Templates frogtommy{0} and leg are out of order.", |
| 106 fmt.format( |
| 107 params, |
| 108 UPRV_LENGTHOF(params), |
| 109 appendTo, |
| 110 offsets, |
| 111 UPRV_LENGTHOF(offsets), |
| 112 status)); |
| 113 assertSuccess("Status", status); |
| 114 for (int32_t i = 0; i < UPRV_LENGTHOF(expectedOffsets); ++i) { |
| 115 if (expectedOffsets[i] != offsets[i]) { |
| 116 errln("Expected %d, got %d", expectedOffsets[i], offsets[i]); |
| 117 } |
| 118 } |
| 119 appendTo.remove(); |
| 120 |
| 121 // Not having enough placeholder params results in error. |
| 122 fmt.format( |
| 123 params, |
| 124 UPRV_LENGTHOF(params) - 1, |
| 125 appendTo, |
| 126 offsets, |
| 127 UPRV_LENGTHOF(offsets), |
| 128 status); |
| 129 if (status != U_ILLEGAL_ARGUMENT_ERROR) { |
| 130 errln("Expected U_ILLEGAL_ARGUMENT_ERROR"); |
| 131 } |
| 132 |
| 133 // Ensure we don't write to offsets array beyond its length. |
| 134 status = U_ZERO_ERROR; |
| 135 offsets[UPRV_LENGTHOF(offsets) - 1] = 289; |
| 136 appendTo.remove(); |
| 137 fmt.format( |
| 138 params, |
| 139 UPRV_LENGTHOF(params), |
| 140 appendTo, |
| 141 offsets, |
| 142 UPRV_LENGTHOF(offsets) - 1, |
| 143 status); |
| 144 assertEquals("Offsets buffer length", 289, offsets[UPRV_LENGTHOF(offsets) -
1]); |
| 145 |
| 146 // Test assignment |
| 147 SimplePatternFormatter s; |
| 148 s = fmt; |
| 149 appendTo.remove(); |
| 150 assertEquals( |
| 151 "Assignment", |
| 152 "Templates frogtommy{0} and leg are out of order.", |
| 153 s.format( |
| 154 params, |
| 155 UPRV_LENGTHOF(params), |
| 156 appendTo, |
| 157 NULL, |
| 158 0, |
| 159 status)); |
| 160 |
| 161 // Copy constructor |
| 162 SimplePatternFormatter r(fmt); |
| 163 appendTo.remove(); |
| 164 assertEquals( |
| 165 "Copy constructor", |
| 166 "Templates frogtommy{0} and leg are out of order.", |
| 167 r.format( |
| 168 params, |
| 169 UPRV_LENGTHOF(params), |
| 170 appendTo, |
| 171 NULL, |
| 172 0, |
| 173 status)); |
| 174 r.compile("{0} meter", status); |
| 175 assertEquals("PlaceholderCount", 1, r.getPlaceholderCount()); |
| 176 appendTo.remove(); |
| 177 assertEquals( |
| 178 "Replace with new compile", |
| 179 "freddy meter", |
| 180 r.format("freddy", appendTo, status)); |
| 181 r.compile("{0}, {1}", status); |
| 182 assertEquals("PlaceholderCount", 2, r.getPlaceholderCount()); |
| 183 appendTo.remove(); |
| 184 assertEquals( |
| 185 "2 arg", |
| 186 "foo, bar", |
| 187 r.format("foo", "bar", appendTo, status)); |
| 188 r.compile("{0}, {1} and {2}", status); |
| 189 assertEquals("PlaceholderCount", 3, r.getPlaceholderCount()); |
| 190 appendTo.remove(); |
| 191 assertEquals( |
| 192 "3 arg", |
| 193 "foo, bar and baz", |
| 194 r.format("foo", "bar", "baz", appendTo, status)); |
| 195 assertSuccess("Status", status); |
| 196 } |
| 197 |
| 198 void SimplePatternFormatterTest::TestGetPatternWithNoPlaceholders() { |
| 199 SimplePatternFormatter fmt("{0} has no {1} placeholders."); |
| 200 assertEquals( |
| 201 "", " has no placeholders.", fmt.getPatternWithNoPlaceholders()); |
| 202 } |
| 203 |
| 204 void SimplePatternFormatterTest::TestOptimization() { |
| 205 UErrorCode status = U_ZERO_ERROR; |
| 206 SimplePatternFormatter fmt; |
| 207 fmt.compile("{2}, {0}, {1} and {3}", status); |
| 208 assertSuccess("Status", status); |
| 209 assertTrue("startsWithPlaceholder", fmt.startsWithPlaceholder(2)); |
| 210 assertFalse("startsWithPlaceholder", fmt.startsWithPlaceholder(0)); |
| 211 UnicodeString values[] = { |
| 212 "freddy", "frog", "leg", "by"}; |
| 213 UnicodeString *params[] = { |
| 214 &values[0], &values[1], &values[2], &values[3]}; |
| 215 int32_t offsets[4]; |
| 216 int32_t expectedOffsets[4] = {5, 13, 0, 22}; |
| 217 |
| 218 // The pattern starts with {2}, so format should append the result of |
| 219 // the rest of the pattern to values[2], the value for {2}. |
| 220 assertEquals( |
| 221 "format", |
| 222 "leg, freddy, frog and by", |
| 223 fmt.format( |
| 224 params, |
| 225 UPRV_LENGTHOF(params), |
| 226 values[2], |
| 227 offsets, |
| 228 UPRV_LENGTHOF(offsets), |
| 229 status)); |
| 230 assertSuccess("Status", status); |
| 231 for (int32_t i = 0; i < UPRV_LENGTHOF(expectedOffsets); ++i) { |
| 232 if (expectedOffsets[i] != offsets[i]) { |
| 233 errln("Expected %d, got %d", expectedOffsets[i], offsets[i]); |
| 234 } |
| 235 } |
| 236 } |
| 237 |
| 238 extern IntlTest *createSimplePatternFormatterTest() { |
| 239 return new SimplePatternFormatterTest(); |
| 240 } |
OLD | NEW |