Search in sources :

Example 51 with MessageFormat

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() + "'");
        }
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) AttributedString(java.text.AttributedString) Test(org.junit.Test)

Example 52 with MessageFormat

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!");
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) Test(org.junit.Test)

Example 53 with MessageFormat

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());
                }
            }
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) MessageFormat(android.icu.text.MessageFormat) AttributedString(java.text.AttributedString) ParseException(java.text.ParseException) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 54 with MessageFormat

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());
}
Also used : MessageFormat(android.icu.text.MessageFormat) ChoiceFormat(java.text.ChoiceFormat) AttributedString(java.text.AttributedString) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Example 55 with MessageFormat

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) {
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) Test(org.junit.Test)

Aggregations

MessageFormat (android.icu.text.MessageFormat)61 Test (org.junit.Test)53 AttributedString (java.text.AttributedString)24 HashMap (java.util.HashMap)15 FieldPosition (java.text.FieldPosition)12 ParsePosition (java.text.ParsePosition)10 Date (java.util.Date)10 ChoiceFormat (java.text.ChoiceFormat)9 ParseException (java.text.ParseException)9 IOException (java.io.IOException)7 Map (java.util.Map)7 TreeMap (java.util.TreeMap)6 SimpleDateFormat (android.icu.text.SimpleDateFormat)5 DateFormat (android.icu.text.DateFormat)4 DecimalFormat (android.icu.text.DecimalFormat)4 NumberFormat (android.icu.text.NumberFormat)4 UFormat (android.icu.text.UFormat)3 ULocale (android.icu.util.ULocale)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3