use of android.icu.text.MessageFormat in project j2objc by google.
the class TestMessageFormat method testPluralFormatToPattern.
// Test toPattern when there is a PluralFormat
@Test
public void testPluralFormatToPattern() {
String[] patterns = { "Beware of vicious {0, plural, one {hamster} other {hamsters}}.", "{0, plural, one {{0, number,C''''est #,##0.0# fichier}} other {Ce sont # fichiers}} dans la liste.", "{0, plural, one {C''est # fichier} other {Ce sont # fichiers}} dans la liste." };
for (int i = 0; i < patterns.length; ++i) {
String pattern = patterns[i];
MessageFormat mf = new MessageFormat(pattern);
MessageFormat mf2 = new MessageFormat(mf.toPattern());
if (!mf.equals(mf2)) {
errln("message formats not equal for pattern:\n*** '" + pattern + "'\n*** '" + mf.toPattern() + "'");
}
}
}
use of android.icu.text.MessageFormat in project j2objc by google.
the class TestMessageFormat method TestClone.
// ---------------------------------
// API Tests
// ---------------------------------
@Test
public void TestClone() {
MessageFormat x = new MessageFormat("There are {0} files on {1}");
MessageFormat z = new MessageFormat("There are {0} files on {1} created");
MessageFormat y = null;
y = (MessageFormat) x.clone();
if (x.equals(y) && !x.equals(z) && !y.equals(z))
logln("First test (operator ==): Passed!");
else {
errln("First test (operator ==): Failed!");
}
if ((x.equals(y) && y.equals(x)) && (!x.equals(z) && !z.equals(x)) && (!y.equals(z) && !z.equals(y)))
logln("Second test (equals): Passed!");
else {
errln("Second test (equals): Failed!");
}
}
use of android.icu.text.MessageFormat in project j2objc by google.
the class TestMessageFormat method TestRBNF.
// test RBNF extensions to message format
@Test
public void TestRBNF() {
// WARNING: this depends on the RBNF formats for en_US
Locale locale = Locale.US;
String[] values = { // do not always parse, so do not include them
"0", "1", "12", "100", "123", "1001", "123,456", "-17" };
String[] formats = { "There are {0,spellout} files to search.", "There are {0,spellout,%simplified} files to search.", "The bogus spellout {0,spellout,%BOGUS} files behaves like the default.", // TODO fix bug, ordinal does not parse
"This is the {0,ordinal} file to search.", "Searching this file will take {0,duration} to complete.", "Searching this file will take {0,duration,%with-words} to complete." };
final NumberFormat numFmt = NumberFormat.getInstance(locale);
Object[] args = new Object[1];
Number num = null;
for (int i = 0; i < formats.length; ++i) {
MessageFormat fmt = new MessageFormat(formats[i], locale);
logln("Testing format pattern: '" + formats[i] + "'");
for (int j = 0; j < values.length; ++j) {
try {
num = numFmt.parse(values[j]);
} catch (Exception e) {
throw new IllegalStateException("failed to parse test argument");
}
args[0] = num;
String result = fmt.format(args);
logln("value: " + num + " --> " + result);
if (i != 3) {
// TODO: fix this, for now skip ordinal parsing (format string at index 3)
try {
Object[] parsedArgs = fmt.parse(result);
if (parsedArgs.length != 1) {
errln("parse returned " + parsedArgs.length + " args");
} else if (!parsedArgs[0].equals(num)) {
errln("parsed argument " + parsedArgs[0] + " != " + num);
}
} catch (Exception e) {
errln("parse of '" + result + " returned exception: " + e.getMessage());
}
}
}
}
}
use of android.icu.text.MessageFormat in project j2objc by google.
the class TestMessageFormat method TestMsgFormatChoice.
@Test
public void TestMsgFormatChoice() {
MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
double[] filelimits = { 0, 1, 2 };
String[] filepart = { "no files", "one file", "{0,number} files" };
ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
// NOT zero, see below
form.setFormat(1, fileform);
FieldPosition ignore = new FieldPosition(FieldPosition_DONT_CARE);
StringBuffer string = new StringBuffer();
Object[] testArgs1 = { new Integer(0), "MyDisk" };
form.format(testArgs1, string, ignore);
assertEquals("format#1", "The disk \"MyDisk\" contains no files.", string.toString());
string.setLength(0);
Object[] testArgs2 = { new Integer(1), "MyDisk" };
form.format(testArgs2, string, ignore);
assertEquals("format#2", "The disk \"MyDisk\" contains one file.", string.toString());
string.setLength(0);
Object[] testArgs3 = { new Integer(1273), "MyDisk" };
form.format(testArgs3, string, ignore);
assertEquals("format#3", "The disk \"MyDisk\" contains 1,273 files.", string.toString());
}
use of android.icu.text.MessageFormat in project j2objc by google.
the class TestMessageFormat method testNamedArguments.
@Test
public void testNamedArguments() {
// ICU 4.8 allows mixing named and numbered arguments.
assertTrue("has some named arguments", new MessageFormat("Number of files in folder {0}: {numfiles}").usesNamedArguments());
assertTrue("has some named arguments", new MessageFormat("Number of files in folder {folder}: {1}").usesNamedArguments());
// Test named arguments.
MessageFormat mf = new MessageFormat("Number of files in folder {folder}: {numfiles}");
if (!mf.usesNamedArguments()) {
errln("message format 1 should have used named arguments");
}
mf = new MessageFormat("Wavelength: {\u028EValue\uFF14}");
if (!mf.usesNamedArguments()) {
errln("message format 2 should have used named arguments");
}
// Modified: ICU 4.8 allows all characters except for Pattern_White_Space and Pattern_Syntax.
try {
new MessageFormat("Wavelength: {^\u028EValue\uFF14}");
errln("Creating a MessageFormat with invalid argument names " + "should throw an IllegalArgumentException but did not!");
} catch (IllegalArgumentException e) {
}
try {
new MessageFormat("Wavelength: {\uFE45\u028EValue}");
errln("Creating a MessageFormat with invalid argument names " + "should throw an IllegalArgumentException but did not!");
} catch (IllegalArgumentException e) {
}
// Modified: ICU 4.8 allows all characters except for Pattern_White_Space and Pattern_Syntax.
try {
new MessageFormat("Wavelength: {Value@\uFF14}");
errln("Creating a MessageFormat with invalid argument names " + "should throw an IllegalArgumentException but did not!");
} catch (IllegalArgumentException e) {
}
try {
new MessageFormat("Wavelength: {Value(\uFF14)}");
errln("Creating a MessageFormat with invalid argument names " + "should throw an IllegalArgumentException but did not!");
} catch (IllegalArgumentException e) {
}
}
Aggregations