2014-01-29 05:08:55 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
* Copyright (C) 2014, International Business Machines Corporation and *
|
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
2014-01-29 22:57:06 +00:00
|
|
|
* File SIMPLEPATTERNFORMATTERTEST.CPP
|
2014-01-29 05:08:55 +00:00
|
|
|
*
|
|
|
|
********************************************************************************
|
|
|
|
*/
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "intltest.h"
|
2014-01-29 22:57:06 +00:00
|
|
|
#include "simplepatternformatter.h"
|
2014-01-29 05:08:55 +00:00
|
|
|
|
2014-01-29 22:57:06 +00:00
|
|
|
class SimplePatternFormatterTest : public IntlTest {
|
2014-01-29 05:08:55 +00:00
|
|
|
public:
|
2014-01-29 22:57:06 +00:00
|
|
|
SimplePatternFormatterTest() {
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
|
|
|
void TestNoPlaceholders();
|
|
|
|
void TestOnePlaceholder();
|
|
|
|
void TestManyPlaceholders();
|
2014-09-11 19:09:54 +00:00
|
|
|
void TestGetPatternWithNoPlaceholders();
|
2014-04-02 21:43:11 +00:00
|
|
|
void TestOptimization();
|
2014-01-29 05:08:55 +00:00
|
|
|
void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
|
2014-01-29 22:57:06 +00:00
|
|
|
void SimplePatternFormatterTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
|
2014-01-29 05:08:55 +00:00
|
|
|
TESTCASE_AUTO_BEGIN;
|
|
|
|
TESTCASE_AUTO(TestNoPlaceholders);
|
|
|
|
TESTCASE_AUTO(TestOnePlaceholder);
|
|
|
|
TESTCASE_AUTO(TestManyPlaceholders);
|
2014-09-11 19:09:54 +00:00
|
|
|
TESTCASE_AUTO(TestGetPatternWithNoPlaceholders);
|
2014-04-02 21:43:11 +00:00
|
|
|
TESTCASE_AUTO(TestOptimization);
|
2014-01-29 05:08:55 +00:00
|
|
|
TESTCASE_AUTO_END;
|
|
|
|
}
|
|
|
|
|
2014-01-29 22:57:06 +00:00
|
|
|
void SimplePatternFormatterTest::TestNoPlaceholders() {
|
2014-01-29 05:08:55 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2014-01-29 22:57:06 +00:00
|
|
|
SimplePatternFormatter fmt("This doesn''t have templates '{0}");
|
|
|
|
assertEquals("PlaceholderCount", 0, fmt.getPlaceholderCount());
|
2014-01-29 05:08:55 +00:00
|
|
|
UnicodeString appendTo;
|
|
|
|
assertEquals(
|
2014-04-02 21:43:11 +00:00
|
|
|
"format",
|
2014-01-29 05:08:55 +00:00
|
|
|
"This doesn't have templates {0}",
|
2014-01-29 23:11:43 +00:00
|
|
|
fmt.format("unused", appendTo, status));
|
2014-01-29 22:57:06 +00:00
|
|
|
fmt.compile("This has {} bad {012d placeholders", status);
|
|
|
|
assertEquals("PlaceholderCount", 0, fmt.getPlaceholderCount());
|
2014-01-30 16:55:02 +00:00
|
|
|
appendTo.remove();
|
2014-01-29 05:08:55 +00:00
|
|
|
assertEquals(
|
2014-04-02 21:43:11 +00:00
|
|
|
"format",
|
2014-01-29 05:08:55 +00:00
|
|
|
"This has {} bad {012d placeholders",
|
2014-01-29 23:11:43 +00:00
|
|
|
fmt.format("unused", appendTo, status));
|
2014-01-29 05:08:55 +00:00
|
|
|
assertSuccess("Status", status);
|
|
|
|
}
|
|
|
|
|
2014-01-29 22:57:06 +00:00
|
|
|
void SimplePatternFormatterTest::TestOnePlaceholder() {
|
2014-01-29 05:08:55 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2014-01-29 22:57:06 +00:00
|
|
|
SimplePatternFormatter fmt;
|
|
|
|
fmt.compile("{0} meter", status);
|
|
|
|
assertEquals("PlaceholderCount", 1, fmt.getPlaceholderCount());
|
2014-01-29 05:08:55 +00:00
|
|
|
UnicodeString appendTo;
|
|
|
|
assertEquals(
|
2014-04-02 21:43:11 +00:00
|
|
|
"format",
|
2014-01-29 05:08:55 +00:00
|
|
|
"1 meter",
|
2014-01-29 23:11:43 +00:00
|
|
|
fmt.format("1", appendTo, status));
|
2014-01-29 05:08:55 +00:00
|
|
|
assertSuccess("Status", status);
|
|
|
|
|
|
|
|
// assignment
|
2014-01-29 22:57:06 +00:00
|
|
|
SimplePatternFormatter s;
|
|
|
|
s = fmt;
|
2014-01-30 16:55:02 +00:00
|
|
|
appendTo.remove();
|
2014-01-29 05:08:55 +00:00
|
|
|
assertEquals(
|
|
|
|
"Assignment",
|
|
|
|
"1 meter",
|
2014-01-29 23:11:43 +00:00
|
|
|
s.format("1", appendTo, status));
|
2014-01-29 05:08:55 +00:00
|
|
|
|
|
|
|
// Copy constructor
|
2014-01-29 22:57:06 +00:00
|
|
|
SimplePatternFormatter r(fmt);
|
2014-01-30 16:55:02 +00:00
|
|
|
appendTo.remove();
|
2014-01-29 05:08:55 +00:00
|
|
|
assertEquals(
|
|
|
|
"Copy constructor",
|
|
|
|
"1 meter",
|
2014-01-29 23:11:43 +00:00
|
|
|
r.format("1", appendTo, status));
|
2014-01-29 05:08:55 +00:00
|
|
|
assertSuccess("Status", status);
|
|
|
|
}
|
|
|
|
|
2014-01-29 22:57:06 +00:00
|
|
|
void SimplePatternFormatterTest::TestManyPlaceholders() {
|
2014-01-29 05:08:55 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2014-01-29 22:57:06 +00:00
|
|
|
SimplePatternFormatter fmt;
|
|
|
|
fmt.compile(
|
2014-01-29 05:08:55 +00:00
|
|
|
"Templates {2}{1}{5} and {4} are out of order.", status);
|
2014-04-02 21:43:11 +00:00
|
|
|
assertSuccess("Status", status);
|
|
|
|
assertFalse("startsWithPlaceholder", fmt.startsWithPlaceholder(2));
|
2014-01-29 22:57:06 +00:00
|
|
|
assertEquals("PlaceholderCount", 6, fmt.getPlaceholderCount());
|
2014-01-29 05:08:55 +00:00
|
|
|
UnicodeString values[] = {
|
|
|
|
"freddy", "tommy", "frog", "billy", "leg", "{0}"};
|
|
|
|
UnicodeString *params[] = {
|
|
|
|
&values[0], &values[1], &values[2], &values[3], &values[4], &values[5]};
|
|
|
|
int32_t offsets[6];
|
2014-01-29 23:11:43 +00:00
|
|
|
int32_t expectedOffsets[6] = {-1, 22, 18, -1, 35, 27};
|
|
|
|
UnicodeString appendTo("Prefix: ");
|
2014-01-29 05:08:55 +00:00
|
|
|
assertEquals(
|
2014-04-02 21:43:11 +00:00
|
|
|
"format",
|
2014-01-29 23:11:43 +00:00
|
|
|
"Prefix: Templates frogtommy{0} and leg are out of order.",
|
2014-01-29 22:57:06 +00:00
|
|
|
fmt.format(
|
2014-01-29 05:08:55 +00:00
|
|
|
params,
|
2014-08-28 22:13:45 +00:00
|
|
|
UPRV_LENGTHOF(params),
|
2014-01-29 05:08:55 +00:00
|
|
|
appendTo,
|
|
|
|
offsets,
|
2014-08-28 22:13:45 +00:00
|
|
|
UPRV_LENGTHOF(offsets),
|
2014-01-29 05:08:55 +00:00
|
|
|
status));
|
|
|
|
assertSuccess("Status", status);
|
2014-08-28 22:13:45 +00:00
|
|
|
for (int32_t i = 0; i < UPRV_LENGTHOF(expectedOffsets); ++i) {
|
2014-01-29 05:08:55 +00:00
|
|
|
if (expectedOffsets[i] != offsets[i]) {
|
|
|
|
errln("Expected %d, got %d", expectedOffsets[i], offsets[i]);
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 16:55:02 +00:00
|
|
|
appendTo.remove();
|
2014-09-11 19:09:54 +00:00
|
|
|
|
|
|
|
// Not having enough placeholder params results in error.
|
2014-01-29 22:57:06 +00:00
|
|
|
fmt.format(
|
2014-01-29 05:08:55 +00:00
|
|
|
params,
|
2014-08-28 22:13:45 +00:00
|
|
|
UPRV_LENGTHOF(params) - 1,
|
2014-01-29 05:08:55 +00:00
|
|
|
appendTo,
|
|
|
|
offsets,
|
2014-08-28 22:13:45 +00:00
|
|
|
UPRV_LENGTHOF(offsets),
|
2014-01-29 05:08:55 +00:00
|
|
|
status);
|
|
|
|
if (status != U_ILLEGAL_ARGUMENT_ERROR) {
|
|
|
|
errln("Expected U_ILLEGAL_ARGUMENT_ERROR");
|
|
|
|
}
|
2014-09-11 19:09:54 +00:00
|
|
|
|
|
|
|
// Ensure we don't write to offsets array beyond its length.
|
2014-01-29 05:08:55 +00:00
|
|
|
status = U_ZERO_ERROR;
|
2014-08-28 22:13:45 +00:00
|
|
|
offsets[UPRV_LENGTHOF(offsets) - 1] = 289;
|
2014-01-30 16:55:02 +00:00
|
|
|
appendTo.remove();
|
2014-01-29 22:57:06 +00:00
|
|
|
fmt.format(
|
2014-01-29 05:08:55 +00:00
|
|
|
params,
|
2014-08-28 22:13:45 +00:00
|
|
|
UPRV_LENGTHOF(params),
|
2014-01-29 05:08:55 +00:00
|
|
|
appendTo,
|
|
|
|
offsets,
|
2014-08-28 22:13:45 +00:00
|
|
|
UPRV_LENGTHOF(offsets) - 1,
|
2014-01-29 05:08:55 +00:00
|
|
|
status);
|
2014-08-28 22:13:45 +00:00
|
|
|
assertEquals("Offsets buffer length", 289, offsets[UPRV_LENGTHOF(offsets) - 1]);
|
2014-01-29 05:08:55 +00:00
|
|
|
|
|
|
|
// Test assignment
|
2014-01-29 22:57:06 +00:00
|
|
|
SimplePatternFormatter s;
|
|
|
|
s = fmt;
|
2014-01-30 16:55:02 +00:00
|
|
|
appendTo.remove();
|
2014-01-29 05:08:55 +00:00
|
|
|
assertEquals(
|
|
|
|
"Assignment",
|
|
|
|
"Templates frogtommy{0} and leg are out of order.",
|
2014-01-29 22:57:06 +00:00
|
|
|
s.format(
|
2014-01-29 05:08:55 +00:00
|
|
|
params,
|
2014-08-28 22:13:45 +00:00
|
|
|
UPRV_LENGTHOF(params),
|
2014-01-29 05:08:55 +00:00
|
|
|
appendTo,
|
2014-01-29 22:57:06 +00:00
|
|
|
NULL,
|
|
|
|
0,
|
2014-01-29 05:08:55 +00:00
|
|
|
status));
|
|
|
|
|
|
|
|
// Copy constructor
|
2014-01-29 22:57:06 +00:00
|
|
|
SimplePatternFormatter r(fmt);
|
2014-01-30 16:55:02 +00:00
|
|
|
appendTo.remove();
|
2014-01-29 05:08:55 +00:00
|
|
|
assertEquals(
|
2014-01-29 22:57:06 +00:00
|
|
|
"Copy constructor",
|
2014-01-29 05:08:55 +00:00
|
|
|
"Templates frogtommy{0} and leg are out of order.",
|
2014-01-29 22:57:06 +00:00
|
|
|
r.format(
|
2014-01-29 05:08:55 +00:00
|
|
|
params,
|
2014-08-28 22:13:45 +00:00
|
|
|
UPRV_LENGTHOF(params),
|
2014-01-29 05:08:55 +00:00
|
|
|
appendTo,
|
2014-01-29 22:57:06 +00:00
|
|
|
NULL,
|
|
|
|
0,
|
2014-01-29 05:08:55 +00:00
|
|
|
status));
|
|
|
|
r.compile("{0} meter", status);
|
|
|
|
assertEquals("PlaceholderCount", 1, r.getPlaceholderCount());
|
2014-01-30 16:55:02 +00:00
|
|
|
appendTo.remove();
|
2014-01-29 05:08:55 +00:00
|
|
|
assertEquals(
|
2014-01-29 22:57:06 +00:00
|
|
|
"Replace with new compile",
|
2014-01-29 05:08:55 +00:00
|
|
|
"freddy meter",
|
2014-01-29 23:11:43 +00:00
|
|
|
r.format("freddy", appendTo, status));
|
|
|
|
r.compile("{0}, {1}", status);
|
|
|
|
assertEquals("PlaceholderCount", 2, r.getPlaceholderCount());
|
2014-01-30 16:55:02 +00:00
|
|
|
appendTo.remove();
|
2014-01-29 23:11:43 +00:00
|
|
|
assertEquals(
|
|
|
|
"2 arg",
|
|
|
|
"foo, bar",
|
|
|
|
r.format("foo", "bar", appendTo, status));
|
|
|
|
r.compile("{0}, {1} and {2}", status);
|
|
|
|
assertEquals("PlaceholderCount", 3, r.getPlaceholderCount());
|
2014-01-30 16:55:02 +00:00
|
|
|
appendTo.remove();
|
2014-01-29 23:11:43 +00:00
|
|
|
assertEquals(
|
|
|
|
"3 arg",
|
|
|
|
"foo, bar and baz",
|
|
|
|
r.format("foo", "bar", "baz", appendTo, status));
|
2014-01-29 05:08:55 +00:00
|
|
|
assertSuccess("Status", status);
|
|
|
|
}
|
|
|
|
|
2014-09-11 19:09:54 +00:00
|
|
|
void SimplePatternFormatterTest::TestGetPatternWithNoPlaceholders() {
|
|
|
|
SimplePatternFormatter fmt("{0} has no {1} placeholders.");
|
|
|
|
assertEquals(
|
|
|
|
"", " has no placeholders.", fmt.getPatternWithNoPlaceholders());
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:43:11 +00:00
|
|
|
void SimplePatternFormatterTest::TestOptimization() {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
SimplePatternFormatter fmt;
|
|
|
|
fmt.compile("{2}, {0}, {1} and {3}", status);
|
|
|
|
assertSuccess("Status", status);
|
|
|
|
assertTrue("startsWithPlaceholder", fmt.startsWithPlaceholder(2));
|
|
|
|
assertFalse("startsWithPlaceholder", fmt.startsWithPlaceholder(0));
|
|
|
|
UnicodeString values[] = {
|
|
|
|
"freddy", "frog", "leg", "by"};
|
|
|
|
UnicodeString *params[] = {
|
|
|
|
&values[0], &values[1], &values[2], &values[3]};
|
|
|
|
int32_t offsets[4];
|
|
|
|
int32_t expectedOffsets[4] = {5, 13, 0, 22};
|
|
|
|
|
|
|
|
// The pattern starts with {2}, so format should append the result of
|
|
|
|
// the rest of the pattern to values[2], the value for {2}.
|
|
|
|
assertEquals(
|
|
|
|
"format",
|
|
|
|
"leg, freddy, frog and by",
|
|
|
|
fmt.format(
|
|
|
|
params,
|
2014-08-28 22:13:45 +00:00
|
|
|
UPRV_LENGTHOF(params),
|
2014-04-02 21:43:11 +00:00
|
|
|
values[2],
|
|
|
|
offsets,
|
2014-08-28 22:13:45 +00:00
|
|
|
UPRV_LENGTHOF(offsets),
|
2014-04-02 21:43:11 +00:00
|
|
|
status));
|
|
|
|
assertSuccess("Status", status);
|
2014-08-28 22:13:45 +00:00
|
|
|
for (int32_t i = 0; i < UPRV_LENGTHOF(expectedOffsets); ++i) {
|
2014-04-02 21:43:11 +00:00
|
|
|
if (expectedOffsets[i] != offsets[i]) {
|
|
|
|
errln("Expected %d, got %d", expectedOffsets[i], offsets[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 22:57:06 +00:00
|
|
|
extern IntlTest *createSimplePatternFormatterTest() {
|
|
|
|
return new SimplePatternFormatterTest();
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|