Search in sources :

Example 6 with MessageFormat

use of android.icu.text.MessageFormat in project j2objc by google.

the class MessageRegressionTest method Test4142938.

/**
 * @bug 4142938
 * MessageFormat handles single quotes in pattern wrong.
 * This is actually a problem in ChoiceFormat; it doesn't
 * understand single quotes.
 */
@Test
public void Test4142938() {
    String pat = "''Vous'' {0,choice,0#n''|1#}avez s\u00E9lectionne\u00E9 " + "{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} " + "personnel{0,choice,0#s|1#|2#s}.";
    MessageFormat mf = new MessageFormat(pat);
    String[] PREFIX = { "'Vous' n'avez s\u00E9lectionne\u00E9 aucun clients personnels.", "'Vous' avez s\u00E9lectionne\u00E9 ", "'Vous' avez s\u00E9lectionne\u00E9 " };
    String[] SUFFIX = { null, " client personnel.", " clients personnels." };
    for (int i = 0; i < 3; i++) {
        String out = mf.format(new Object[] { new Integer(i) });
        if (SUFFIX[i] == null) {
            if (!out.equals(PREFIX[i]))
                errln("" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"");
        } else {
            if (!out.startsWith(PREFIX[i]) || !out.endsWith(SUFFIX[i]))
                errln("" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"...\"" + SUFFIX[i] + "\"");
        }
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) Test(org.junit.Test)

Example 7 with MessageFormat

use of android.icu.text.MessageFormat in project j2objc by google.

the class MessageRegressionTest method Test4116444.

/* @bug 4116444
     * MessageFormat.parse has different behavior in case of null.
     */
@Test
public void Test4116444() {
    String[] patterns = { "", "one", "{0,date,short}" };
    MessageFormat mf = new MessageFormat("");
    for (int i = 0; i < patterns.length; i++) {
        String pattern = patterns[i];
        mf.applyPattern(pattern);
        try {
            Object[] array = mf.parse(null, new ParsePosition(0));
            logln("pattern: \"" + pattern + "\"");
            log(" parsedObjects: ");
            if (array != null) {
                log("{");
                for (int j = 0; j < array.length; j++) {
                    if (array[j] != null)
                        err("\"" + array[j].toString() + "\"");
                    else
                        log("null");
                    if (j < array.length - 1)
                        log(",");
                }
                log("}");
            } else {
                log("null");
            }
            logln("");
        } catch (Exception e) {
            errln("pattern: \"" + pattern + "\"");
            errln("  Exception: " + e.getMessage());
        }
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) IOException(java.io.IOException) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 8 with MessageFormat

use of android.icu.text.MessageFormat in project j2objc by google.

the class MessageRegressionTest method Test4105380.

/* @bug 4105380
     * When using ChoiceFormat, MessageFormat is not good for I18n.
     */
@Test
public void Test4105380() {
    String patternText1 = "The disk \"{1}\" contains {0}.";
    String patternText2 = "There are {0} on the disk \"{1}\"";
    MessageFormat form1 = new MessageFormat(patternText1);
    MessageFormat form2 = new MessageFormat(patternText2);
    double[] filelimits = { 0, 1, 2 };
    String[] filepart = { "no files", "one file", "{0,number} files" };
    ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
    form1.setFormat(1, fileform);
    form2.setFormat(0, fileform);
    Object[] testArgs = { new Long(12373), "MyDisk" };
    logln(form1.format(testArgs));
    logln(form2.format(testArgs));
}
Also used : MessageFormat(android.icu.text.MessageFormat) ChoiceFormat(java.text.ChoiceFormat) Test(org.junit.Test)

Example 9 with MessageFormat

use of android.icu.text.MessageFormat in project j2objc by google.

the class MessageRegressionTest method Test4074764.

/* @bug 4074764
     * Null exception when formatting pattern with MessageFormat
     * with no parameters.
     */
@Test
public void Test4074764() {
    String[] pattern = { "Message without param", "Message with param:{0}", "Longer Message with param {0}" };
    // difference between the two param strings are that
    // in the first one, the param position is within the
    // length of the string without param while it is not so
    // in the other case.
    MessageFormat messageFormatter = new MessageFormat("");
    try {
        // Apply pattern with param and print the result
        messageFormatter.applyPattern(pattern[1]);
        Object[] paramArray = { new String("BUG"), new Date() };
        String tempBuffer = messageFormatter.format(paramArray);
        if (!tempBuffer.equals("Message with param:BUG"))
            errln("MessageFormat with one param test failed.");
        logln("Formatted with one extra param : " + tempBuffer);
        // Apply pattern without param and print the result
        messageFormatter.applyPattern(pattern[0]);
        tempBuffer = messageFormatter.format(null);
        if (!tempBuffer.equals("Message without param"))
            errln("MessageFormat with no param test failed.");
        logln("Formatted with no params : " + tempBuffer);
        tempBuffer = messageFormatter.format(paramArray);
        if (!tempBuffer.equals("Message without param"))
            errln("Formatted with arguments > subsitution failed. result = " + tempBuffer.toString());
        logln("Formatted with extra params : " + tempBuffer);
    // This statement gives an exception while formatting...
    // If we use pattern[1] for the message with param,
    // we get an NullPointerException in MessageFormat.java(617)
    // If we use pattern[2] for the message with param,
    // we get an StringArrayIndexOutOfBoundsException in MessageFormat.java(614)
    // Both are due to maxOffset not being reset to -1
    // in applyPattern() when the pattern does not
    // contain any param.
    } catch (Exception foo) {
        errln("Exception when formatting with no params.");
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) Date(java.util.Date) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with MessageFormat

use of android.icu.text.MessageFormat in project j2objc by google.

the class MessageRegressionTest method Test4118594.

/* @bug 4118594
     * MessageFormat.parse fails for some patterns.
     */
@Test
public void Test4118594() {
    MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
    String forParsing = "x, y, z";
    Object[] objs = mf.parse(forParsing, new ParsePosition(0));
    logln("pattern: \"" + mf.toPattern() + "\"");
    logln("text for parsing: \"" + forParsing + "\"");
    if (!objs[0].toString().equals("z"))
        errln("argument0: \"" + objs[0] + "\"");
    mf.setLocale(Locale.US);
    mf.applyPattern("{0,number,#.##}, {0,number,#.#}");
    Object[] oldobjs = { new Double(3.1415) };
    String result = mf.format(oldobjs);
    logln("pattern: \"" + mf.toPattern() + "\"");
    logln("text for parsing: \"" + result + "\"");
    // result now equals "3.14, 3.1"
    if (!result.equals("3.14, 3.1"))
        errln("result = " + result);
    Object[] newobjs = mf.parse(result, new ParsePosition(0));
    // newobjs now equals {new Double(3.1)}
    if (// was (Double) [alan]
    ((Number) newobjs[0]).doubleValue() != 3.1)
        errln("newobjs[0] = " + newobjs[0]);
}
Also used : MessageFormat(android.icu.text.MessageFormat) ParsePosition(java.text.ParsePosition) 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