+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2016, International Business Machines Corporation and
#include "unicode/messagepattern.h"
#include "unicode/selfmt.h"
#include "unicode/gregocal.h"
+#include "unicode/strenum.h"
#include <stdio.h>
void
TESTCASE_AUTO(TestSelectOrdinal);
TESTCASE_AUTO(TestDecimals);
TESTCASE_AUTO(TestArgIsPrefixOfAnother);
+ TESTCASE_AUTO(TestMessageFormatNumberSkeleton);
TESTCASE_AUTO_END;
}
continue;
}
Formattable result;
- FieldPosition pos(0);
+ FieldPosition pos(FieldPosition::DONT_CARE);
buffer.remove();
form->format(myNumber, buffer, pos);
success = U_ZERO_ERROR;
"1.0<=Arg<2.0",
"2.0<-Arg"};
ChoiceFormat *cf = new ChoiceFormat(limit, formats, 3);
- FieldPosition status(0);
+ FieldPosition status(FieldPosition::DONT_CARE);
UnicodeString toAppendTo;
cf->format((int32_t)1, toAppendTo, status);
if (toAppendTo != "1.0<=Arg<2.0") {
}
#if 0
-#if defined(_DEBUG) && U_IOSTREAM_SOURCE >= 199711
+#if defined(_DEBUG)
//----------------------------------------------------
// console I/O
//----------------------------------------------------
}
return stream;
}
-#endif /* defined(_DEBUG) && U_IOSTREAM_SOURCE >= 199711 */
+#endif /* defined(_DEBUG) */
#endif
void TestMessageFormat::PatternTest()
//it_out << "Pat out: " << form->toPattern(buffer));
UnicodeString result;
int32_t count = 4;
- FieldPosition fieldpos(0);
+ FieldPosition fieldpos(FieldPosition::DONT_CARE);
form->format(testArgs, count, result, fieldpos, success);
if (U_FAILURE(success)) {
dataerrln("MessageFormat failed test #3 - %s", u_errorName(success));
UnicodeString abc("abc");
UnicodeString def("def");
Formattable testArgs1[] = { abc, def };
- FieldPosition fieldpos(0);
+ FieldPosition fieldpos(FieldPosition::DONT_CARE);
assertEquals("format",
"There are abc files on def",
form->format(testArgs1, 2, buffer2, fieldpos, success));
err = U_ZERO_ERROR;
//Create the MessageFormat with Plural format with embedded select format(nested pattern)
MessageFormat* msgFmt5 = internalCreate(t5.unescape(), Locale("fr"),err,(char*)"From TestMessageFormat::TestSelectFormat create t5");
+ // with no data the above should fail but it seems to construct an invalid MessageFormat with no reported error. See #13079
if (!U_FAILURE(err)) {
//Arguments
Formattable testArgs10[] = {"Kirti",(int32_t)6,"female"};
MessageFormat msg( formatStr, err);
result = "";
- FieldPosition pos(0);
+ FieldPosition pos(FieldPosition::DONT_CARE);
result = msg.format(
arguments,
3,
err = U_ZERO_ERROR;
MessageFormat msg( formatStr, err);
- FieldPosition fp(0);
+ FieldPosition fp(FieldPosition::DONT_CARE);
result = "";
fp = 0;
UnicodeString formatStr("Hello World on {0,date,full}", "");
UnicodeString resultStr(" ", "");
UnicodeString result;
- FieldPosition fp(0);
+ FieldPosition fp(FieldPosition::DONT_CARE);
UDate d = Calendar::getNow();
const Formattable fargs( d, Formattable::kIsDate );
if (U_FAILURE(ec)) {
errln((UnicodeString)"Failed to parse test argument " + values[j]);
} else {
- FieldPosition fp(0);
+ FieldPosition fp(FieldPosition::DONT_CARE);
UnicodeString result;
fmt->format(args, 1, result, fp, ec);
logln((UnicodeString)"value: " + toString(args[0]) + " --> " + result + UnicodeString(" ec: ") + u_errorName(ec));
}
Formattable zero0[] = { (int32_t)0 };
- FieldPosition fieldpos(0);
+ FieldPosition fieldpos(FieldPosition::DONT_CARE);
UnicodeString buffer1, buffer2;
assertEquals("incompatible ICU MessageFormat compatibility-apostrophe behavior",
"ab12'3'4''.yz",
void TestMessageFormat::testGetFormatNames() {
IcuTestErrorCode errorCode(*this, "testGetFormatNames");
MessageFormat msgfmt("Hello, {alice,number} {oops,date,full} {zip,spellout} World.", Locale::getRoot(), errorCode);
- if(errorCode.logDataIfFailureAndReset("MessageFormat() failed")) {
+ if(errorCode.errDataIfFailureAndReset("MessageFormat() failed")) {
return;
}
LocalPointer<StringEnumeration> names(msgfmt.getFormatNames(errorCode));
- if(errorCode.logIfFailureAndReset("msgfmt.getFormatNames() failed")) {
+ if(errorCode.errIfFailureAndReset("msgfmt.getFormatNames() failed")) {
return;
}
const UnicodeString *name;
// ICU 4.8 allows and ignores white space around argument names and numbers.
IcuTestErrorCode errorCode(*this, "TestTrimArgumentName");
MessageFormat m("a { 0 , number , '#,#'#.0 } z", Locale::getEnglish(), errorCode);
- if (errorCode.logDataIfFailureAndReset("Unable to instantiate MessageFormat")) {
+ if (errorCode.errDataIfFailureAndReset("Unable to instantiate MessageFormat")) {
return;
}
Formattable args[1] = { (int32_t)2 };
- FieldPosition ignore(0);
+ FieldPosition ignore(FieldPosition::DONT_CARE);
UnicodeString result;
assertEquals("trim-numbered-arg format() failed", "a #,#2.0 z",
m.format(args, 1, result, ignore, errorCode));
"{0,plural,one{1 file}other{# files}}, "
"{0,selectordinal,one{#st file}two{#nd file}few{#rd file}other{#th file}}",
Locale::getEnglish(), errorCode);
- if (errorCode.logDataIfFailureAndReset("Unable to instantiate MessageFormat")) {
+ if (errorCode.errDataIfFailureAndReset("Unable to instantiate MessageFormat")) {
return;
}
Formattable args[1] = { (int32_t)21 };
- FieldPosition ignore(0);
+ FieldPosition ignore(FieldPosition::DONT_CARE);
UnicodeString result;
assertEquals("plural-and-ordinal format(21) failed", "21 files, 21st file",
m.format(args, 1, result, ignore, errorCode), TRUE);
assertEquals("plural-and-ordinal format(3) failed", "3 files, 3rd file",
m.format(args, 1, result.remove(), ignore, errorCode), TRUE);
- errorCode.logDataIfFailureAndReset("");
+ errorCode.errDataIfFailureAndReset("");
}
void TestMessageFormat::TestDecimals() {
assertEquals("aa aaa", "AB ABC", mf3.format(argNames + 1, args + 1, 2, result.remove(), errorCode));
}
+void TestMessageFormat::TestMessageFormatNumberSkeleton() {
+ IcuTestErrorCode status(*this, "TestMessageFormatNumberSkeleton");
+
+ static const struct TestCase {
+ const char16_t* messagePattern;
+ const char* localeName;
+ double arg;
+ const char16_t* expected;
+ } cases[] = {
+ { u"{0,number,::percent}", "en", 50, u"50%" },
+ { u"{0,number,::percent scale/100}", "en", 0.5, u"50%" },
+ { u"{0,number, :: percent scale/100 }", "en", 0.5, u"50%" },
+ { u"{0,number,::currency/USD}", "en", 23, u"$23.00" },
+ { u"{0,number,::precision-integer}", "en", 514.23, u"514" },
+ { u"{0,number,::.000}", "en", 514.23, u"514.230" },
+ { u"{0,number,::.}", "en", 514.23, u"514" },
+ { u"{0,number,::}", "fr", 514.23, u"514,23" },
+ { u"Cost: {0,number,::currency/EUR}.", "en", 4.3, u"Cost: €4.30." },
+ { u"{0,number,'::'0.00}", "en", 50, u"::50.00" }, // pattern literal
+ };
+
+ for (auto& cas : cases) {
+ status.setScope(cas.messagePattern);
+ MessageFormat msgf(cas.messagePattern, cas.localeName, status);
+ UnicodeString sb;
+ FieldPosition fpos(0);
+ Formattable argsArray[] = {{cas.arg}};
+ Formattable args(argsArray, 1);
+ msgf.format(args, sb, status);
+
+ assertEquals(cas.messagePattern, cas.expected, sb);
+ }
+}
+
#endif /* #if !UCONFIG_NO_FORMATTING */