1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
// © 2024 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#ifndef _TESTMESSAGEFORMAT2_UTILS
#define _TESTMESSAGEFORMAT2_UTILS
#include "unicode/utypes.h"
#if !UCONFIG_NO_NORMALIZATION
#if !UCONFIG_NO_FORMATTING
#if !UCONFIG_NO_MF2
#include "unicode/locid.h"
#include "unicode/messageformat2_formattable.h"
#include "unicode/messageformat2.h"
#include "intltest.h"
#include "messageformat2_macros.h"
#include "messageformat2_serializer.h"
U_NAMESPACE_BEGIN namespace message2 {
class TestCase : public UMemory {
private:
/* const */ UnicodeString testName;
/* const */ UnicodeString pattern;
/* const */ Locale locale;
/* const */ std::map<UnicodeString, Formattable> arguments;
/* const */ UErrorCode expectedError;
/* const */ bool arbitraryError = false;
/* const */ bool expectedNoSyntaxError;
/* const */ bool hasExpectedOutput;
/* const */ UnicodeString expected;
/* const */ bool hasLineNumberAndOffset;
/* const */ uint32_t lineNumber;
/* const */ uint32_t offset;
/* const */ bool ignoreError;
// Function registry is not owned by the TestCase object
const MFFunctionRegistry* functionRegistry = nullptr;
public:
const UnicodeString& getPattern() const { return pattern; }
const Locale& getLocale() const { return locale; }
std::map<UnicodeString, Formattable> getArguments() const { return std::move(arguments); }
const UnicodeString& getTestName() const { return testName; }
bool expectSuccess() const {
return (!ignoreError && U_SUCCESS(expectedError) && !arbitraryError);
}
bool expectFailure() const {
return (!ignoreError && U_FAILURE(expectedError));
}
bool expectArbitraryError() const {
return arbitraryError;
}
bool expectNoSyntaxError() const {
return expectedNoSyntaxError;
}
UErrorCode expectedErrorCode() const {
U_ASSERT(!expectSuccess());
return expectedError;
}
bool lineNumberAndOffsetMatch(uint32_t actualLine, uint32_t actualOffset) const {
return (!hasLineNumberAndOffset ||
((actualLine == lineNumber) && actualOffset == offset));
}
bool outputMatches(const UnicodeString& result) const {
return (!hasExpectedOutput || (expected == result));
}
const UnicodeString& expectedOutput() const {
U_ASSERT(hasExpectedOutput);
return expected;
}
uint32_t getLineNumber() const {
U_ASSERT(hasLineNumberAndOffset);
return lineNumber;
}
uint32_t getOffset() const {
U_ASSERT(hasLineNumberAndOffset);
return offset;
}
bool hasCustomRegistry() const { return functionRegistry != nullptr; }
const MFFunctionRegistry* getCustomRegistry() const {
U_ASSERT(hasCustomRegistry());
return functionRegistry;
}
TestCase(const TestCase&);
TestCase& operator=(TestCase&& other) noexcept = default;
virtual ~TestCase();
class Builder : public UObject {
friend class TestCase;
public:
Builder& setName(UnicodeString name) { testName = name; return *this; }
Builder& setPattern(UnicodeString pat) { pattern = pat; return *this; }
Builder& setArgument(const UnicodeString& k, const UnicodeString& val) {
arguments[k] = Formattable(val);
return *this;
}
Builder& setArgument(const UnicodeString& k, const Formattable* val, int32_t count) {
U_ASSERT(val != nullptr);
arguments[k] = Formattable(val, count);
return *this;
}
Builder& setArgument(const UnicodeString& k, double val) {
arguments[k] = Formattable(val);
return *this;
}
Builder& setArgument(const UnicodeString& k, int64_t val) {
arguments[k] = Formattable(val);
return *this;
}
Builder& setDateArgument(const UnicodeString& k, UDate date) {
// This ignores time zones; the data-driven tests represent date/time values
// as a datestamp, so this code suffices to handle those.
// Date/time literal strings would be handled using `setArgument()` with a string
// argument.
DateInfo dateInfo = { date, {} }; // No time zone or calendar name
arguments[k] = Formattable(std::move(dateInfo));
return *this;
}
Builder& setDecimalArgument(const UnicodeString& k, std::string_view decimal, UErrorCode& errorCode) {
THIS_ON_ERROR(errorCode);
arguments[k] = Formattable::forDecimal(decimal, errorCode);
return *this;
}
Builder& setArgument(const UnicodeString& k, const FormattableObject* val) {
U_ASSERT(val != nullptr);
arguments[k] = Formattable(val);
return *this;
}
Builder& clearArguments() {
arguments.clear();
return *this;
}
Builder& setExpected(UnicodeString e) {
hasExpectedOutput = true;
expected = e;
return *this;
}
Builder& clearExpected() {
hasExpectedOutput = false;
return *this;
}
Builder& setExpectedError(UErrorCode errorCode) {
expectedError = U_SUCCESS(errorCode) ? U_ZERO_ERROR : errorCode;
return *this;
}
Builder& setExpectedAnyError() {
arbitraryError = true;
return *this;
}
Builder& setNoSyntaxError() {
expectNoSyntaxError = true;
return *this;
}
Builder& setExpectSuccess() {
return setExpectedError(U_ZERO_ERROR);
}
Builder& setLocale(Locale&& loc) {
locale = loc;
return *this;
}
Builder& setExpectedLineNumberAndOffset(uint32_t line, uint32_t o) {
hasLineNumberAndOffset = true;
lineNumber = line;
offset = o;
return *this;
}
Builder& setIgnoreError() {
ignoreError = true;
return *this;
}
Builder& clearIgnoreError() {
ignoreError = false;
return *this;
}
Builder& setFunctionRegistry(const MFFunctionRegistry* reg) {
U_ASSERT(reg != nullptr);
functionRegistry = reg;
return *this;
}
TestCase build() const {
return TestCase(*this);
}
virtual ~Builder();
private:
UnicodeString testName;
UnicodeString pattern;
Locale locale;
std::map<UnicodeString, Formattable> arguments;
bool hasExpectedOutput;
UnicodeString expected;
UErrorCode expectedError;
bool arbitraryError;
bool expectNoSyntaxError;
bool hasLineNumberAndOffset;
uint32_t lineNumber;
uint32_t offset;
bool ignoreError;
const MFFunctionRegistry* functionRegistry = nullptr; // Not owned
public:
Builder() : pattern(""), locale(Locale::getDefault()), hasExpectedOutput(false), expected(""), expectedError(U_ZERO_ERROR), arbitraryError(false), expectNoSyntaxError(false), hasLineNumberAndOffset(false), ignoreError(false) {}
};
private:
TestCase(const Builder& builder) :
testName(builder.testName),
pattern(builder.pattern),
locale(builder.locale),
arguments(builder.arguments),
expectedError(builder.expectedError),
arbitraryError(builder.arbitraryError),
expectedNoSyntaxError(builder.expectNoSyntaxError),
hasExpectedOutput(builder.hasExpectedOutput),
expected(builder.expected),
hasLineNumberAndOffset(builder.hasLineNumberAndOffset),
lineNumber(builder.hasLineNumberAndOffset ? builder.lineNumber : 0),
offset(builder.hasLineNumberAndOffset ? builder.offset : 0),
ignoreError(builder.ignoreError),
functionRegistry(builder.functionRegistry) {
// If an error is not expected, then the expected
// output should be present
U_ASSERT(expectFailure() || expectNoSyntaxError() || hasExpectedOutput);
}
}; // class TestCase
class TestUtils {
public:
// Runs a single test case
static void runTestCase(IntlTest& tmsg,
const TestCase& testCase,
IcuTestErrorCode& errorCode) {
CHECK_ERROR(errorCode);
UParseError parseError;
MessageFormatter::Builder mfBuilder(errorCode);
mfBuilder.setPattern(testCase.getPattern(), parseError, errorCode).setLocale(testCase.getLocale());
if (testCase.hasCustomRegistry()) {
mfBuilder.setFunctionRegistry(*testCase.getCustomRegistry());
}
// Initially, set error behavior to strict.
// We'll re-run to check for errors.
mfBuilder.setErrorHandlingBehavior(MessageFormatter::U_MF_STRICT);
MessageFormatter mf = mfBuilder.build(errorCode);
UnicodeString result;
// Builder should fail if a syntax error was expected
if (!testCase.expectSuccess() && testCase.expectedErrorCode() == U_MF_SYNTAX_ERROR) {
if (errorCode != testCase.expectedErrorCode()) {
failExpectedFailure(tmsg, testCase, errorCode);
}
errorCode.reset();
return;
}
if (U_SUCCESS(errorCode)) {
result = mf.formatToString(MessageArguments(testCase.getArguments(), errorCode), errorCode);
}
const UnicodeString& in = mf.getNormalizedPattern();
UnicodeString out;
if (!roundTrip(in, mf.getDataModel(), out)
// For now, don't round-trip messages with these errors,
// since duplicate options are dropped
&& (testCase.expectSuccess() ||
(testCase.expectedErrorCode() != U_MF_DUPLICATE_OPTION_NAME_ERROR))) {
failRoundTrip(tmsg, testCase, in, out);
}
if (testCase.expectNoSyntaxError()) {
if (errorCode == U_MF_SYNTAX_ERROR) {
failSyntaxError(tmsg, testCase);
}
errorCode.reset();
return;
}
if (testCase.expectSuccess() && U_FAILURE(errorCode)) {
failExpectedSuccess(tmsg, testCase, errorCode, parseError.line, parseError.offset);
return;
}
if (testCase.expectArbitraryError() && U_SUCCESS(errorCode)) {
failExpectedArbitraryError(tmsg, testCase);
}
if (testCase.expectFailure() && errorCode != testCase.expectedErrorCode()) {
failExpectedFailure(tmsg, testCase, errorCode);
return;
}
if (!testCase.lineNumberAndOffsetMatch(parseError.line, parseError.offset)) {
failWrongOffset(tmsg, testCase, parseError.line, parseError.offset);
}
if (U_FAILURE(errorCode) && !testCase.expectSuccess()
&& testCase.expectedErrorCode() != U_MF_SYNTAX_ERROR) {
// Re-run the formatter if there was an error,
// in order to get best-effort output
errorCode.reset();
mfBuilder.setErrorHandlingBehavior(MessageFormatter::U_MF_BEST_EFFORT);
mf = mfBuilder.build(errorCode);
if (U_SUCCESS(errorCode)) {
result = mf.formatToString(MessageArguments(testCase.getArguments(), errorCode), errorCode);
}
if (U_FAILURE(errorCode)) {
// Must be a non-MF2 error code
U_ASSERT(!(errorCode >= U_MF_UNRESOLVED_VARIABLE_ERROR
&& errorCode <= U_FMT_PARSE_ERROR_LIMIT));
}
// Re-run the formatter
result = mf.formatToString(MessageArguments(testCase.getArguments(), errorCode), errorCode);
}
if (!testCase.outputMatches(result)) {
failWrongOutput(tmsg, testCase, result);
return;
}
errorCode.reset();
}
static bool roundTrip(const UnicodeString& normalizedInput, const MFDataModel& dataModel, UnicodeString& result) {
Serializer(dataModel, result).serialize();
return (normalizedInput == result);
}
static void failSyntaxError(IntlTest& tmsg, const TestCase& testCase) {
tmsg.dataerrln(testCase.getTestName());
tmsg.logln(testCase.getTestName() + " failed test with pattern: " + testCase.getPattern() + " and error code U_MF_SYNTAX_ERROR; expected no syntax error");
}
static void failExpectedSuccess(IntlTest& tmsg, const TestCase& testCase, IcuTestErrorCode& errorCode, int32_t line, int32_t offset) {
tmsg.dataerrln(testCase.getTestName());
tmsg.logln(testCase.getTestName() + " failed test with pattern: " + testCase.getPattern() + " and error code " + UnicodeString(u_errorName(errorCode)));
tmsg.dataerrln("line = %d offset = %d", line, offset);
errorCode.reset();
}
static void failExpectedFailure(IntlTest& tmsg, const TestCase& testCase, IcuTestErrorCode& errorCode) {
tmsg.dataerrln(testCase.getTestName());
tmsg.errln(testCase.getTestName() + " failed test with wrong error code; pattern: " + testCase.getPattern() + " and error code " + UnicodeString(u_errorName(errorCode)) + " and expected error code: " + UnicodeString(u_errorName(testCase.expectedErrorCode())));
errorCode.reset();
}
static void failExpectedArbitraryError(IntlTest& tmsg, const TestCase& testCase) {
tmsg.dataerrln(testCase.getTestName());
tmsg.errln(testCase.getTestName() + " succeeded although any error was expected; pattern: " + testCase.getPattern());
}
static void failWrongOutput(IntlTest& tmsg, const TestCase& testCase, const UnicodeString& result) {
tmsg.dataerrln(testCase.getTestName());
tmsg.logln(testCase.getTestName() + " failed test with wrong output; pattern: " + testCase.getPattern() + " and expected output = " + testCase.expectedOutput() + " and actual output = " + result);
}
static void failRoundTrip(IntlTest& tmsg, const TestCase& testCase, const UnicodeString& in, const UnicodeString& output) {
tmsg.dataerrln(testCase.getTestName());
tmsg.logln(testCase.getTestName() + " failed test with wrong output; normalized input = " + in + " serialized data model = " + output);
}
static void failWrongOffset(IntlTest& tmsg, const TestCase& testCase, uint32_t actualLine, uint32_t actualOffset) {
tmsg.dataerrln("Test failed with wrong line or character offset in parse error; expected (line %d, offset %d), got (line %d, offset %d)", testCase.getLineNumber(), testCase.getOffset(),
actualLine, actualOffset);
tmsg.logln(UnicodeString(testCase.getTestName()) + " pattern = " + testCase.getPattern() + " - failed by returning the wrong line number or offset in the parse error");
}
}; // class TestUtils
} // namespace message2
U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_MF2 */
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif /* #if !UCONFIG_NO_NORMALIZATION */
#endif
|