Search in sources :

Example 21 with MessageFormat

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

the class TestMessageFormat method testFormatNamedArguments.

// This tests passing named arguments instead of numbers to format().
@Test
public void testFormatNamedArguments() {
    Map arguments = new HashMap();
    arguments.put("startDate", new Date(871068000000L));
    StringBuffer result = new StringBuffer();
    String formatStr = "On {startDate,date}, it began.";
    String compareStr = "On Aug 8, 1997, it began.";
    MessageFormat msg = new MessageFormat(formatStr);
    FieldPosition fp = new FieldPosition(0);
    try {
        msg.format(arguments.get("startDate"), result, fp);
        errln("*** MSG format without expected error code.");
    } catch (Exception e1) {
    }
    result.setLength(0);
    result = msg.format(arguments, result, fp);
    assertEquals("format", compareStr, result.toString());
}
Also used : MessageFormat(android.icu.text.MessageFormat) HashMap(java.util.HashMap) AttributedString(java.text.AttributedString) FieldPosition(java.text.FieldPosition) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Date(java.util.Date) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 22 with MessageFormat

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

the class TestMessageFormat method testApostropheInPluralAndSelect.

@Test
public void testApostropheInPluralAndSelect() {
    MessageFormat fmt = new MessageFormat("abc_{0,plural,other{#'#'#'{'#''}}_def_{1,select,other{sel'}'ect''}}_xyz", Locale.ENGLISH);
    String expected = "abc_3#3{3'_def_sel}ect'_xyz";
    String result = fmt.format(new Object[] { 3, "x" });
    if (!result.equals(expected)) {
        errln("MessageFormat with apostrophes in plural/select arguments failed:\n" + "Expected " + expected + "\n" + "Got      " + result);
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) AttributedString(java.text.AttributedString) Test(org.junit.Test)

Example 23 with MessageFormat

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

the class TestMessageFormat method testSelectFormat.

/**
 * This tests SelectFormats used inside MessageFormats.
 */
@Test
public void testSelectFormat() {
    String pattern = null;
    MessageFormat msgFmt = null;
    // Create the MessageFormat with simple French pattern
    pattern = "{0} est {1, select, female {all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris.";
    msgFmt = new MessageFormat(pattern);
    assertNotNull("ERROR:Failure in constructing with simple French pattern", msgFmt);
    // Format
    Object[][] testArgs = { { "Kirti", "female" }, { "Victor", "other" }, { "Ash", "unknown" } };
    String[] exp = { "Kirti est all\\u00E9e \\u00E0 Paris.", "Victor est all\\u00E9 \\u00E0 Paris.", "Ash est all\\u00E9 \\u00E0 Paris." };
    for (int i = 0; i < 3; i++) {
        assertEquals("ERROR:Failure in format with simple French Pattern", exp[i], msgFmt.format(testArgs[i]));
    }
    // Create the MessageFormat with Quoted French Pattern
    pattern = "{0} est {1, select, female {all\\u00E9e c''est} other {all\\u00E9 c''est}} \\u00E0 Paris.";
    msgFmt = new MessageFormat(pattern);
    assertNotNull("ERROR:Failure in constructing with quoted French pattern", msgFmt);
    // Format
    Object[][] testArgs1 = { { "Kirti", "female" }, { "Victor", "other" }, { "Ash", "male" } };
    String[] exp1 = { "Kirti est all\\u00E9e c'est \\u00E0 Paris.", "Victor est all\\u00E9 c'est \\u00E0 Paris.", "Ash est all\\u00E9 c'est \\u00E0 Paris." };
    for (int i = 0; i < 3; i++) {
        assertEquals("ERROR:Failure in format with quoted French Pattern", exp1[i], msgFmt.format(testArgs1[i]));
    }
    // Nested patterns with plural, number ,choice ,select format etc.
    // Select Format with embedded number format
    pattern = "{0} est {1, select, female {{2,number,integer} all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris.";
    msgFmt = new MessageFormat(pattern);
    assertNotNull("ERROR:Failure in constructing with nested pattern 1", msgFmt);
    // Format
    Object[][] testArgs3 = { { "Kirti", "female", 6 }, { "Kirti", "female", 100.100 }, { "Kirti", "other", 6 } };
    String[] exp3 = { "Kirti est 6 all\\u00E9e \\u00E0 Paris.", "Kirti est 100 all\\u00E9e \\u00E0 Paris.", "Kirti est all\\u00E9 \\u00E0 Paris." };
    for (int i = 0; i < 3; i++) {
        assertEquals("ERROR:Failure in format with nested Pattern 1", exp3[i], msgFmt.format(testArgs3[i]));
    }
    // Plural format with embedded select format
    pattern = "{0} {1, plural, one {est {2, select, female {all\\u00E9e} other {all\\u00E9}}} other {sont {2, select, female {all\\u00E9es} other {all\\u00E9s}}}} \\u00E0 Paris.";
    msgFmt = new MessageFormat(pattern);
    assertNotNull("ERROR:Failure in constructing with nested pattern 2", msgFmt);
    // Format
    Object[][] testArgs4 = { { "Kirti", 6, "female" }, { "Kirti", 1, "female" }, { "Ash", 1, "other" }, { "Ash", 5, "other" } };
    String[] exp4 = { "Kirti sont all\\u00E9es \\u00E0 Paris.", "Kirti est all\\u00E9e \\u00E0 Paris.", "Ash est all\\u00E9 \\u00E0 Paris.", "Ash sont all\\u00E9s \\u00E0 Paris." };
    for (int i = 0; i < 4; i++) {
        assertEquals("ERROR:Failure in format with nested Pattern 2", exp4[i], msgFmt.format(testArgs4[i]));
    }
    // Select, plural, and number formats heavily nested
    pattern = "{0} und {1, select, female {{2, plural, one {{3, select, female {ihre Freundin} other {ihr Freund}} } other {ihre {2, number, integer} {3, select, female {Freundinnen} other {Freunde}} } }} other{{2, plural, one {{3, select, female {seine Freundin} other {sein Freund}}} other {seine {2, number, integer} {3, select, female {Freundinnen} other {Freunde}}}}} } gingen nach Paris.";
    msgFmt = new MessageFormat(pattern);
    assertNotNull("ERROR:Failure in constructing with nested pattern 3", msgFmt);
    // Format
    Object[][] testArgs5 = { { "Kirti", "other", 1, "other" }, { "Kirti", "other", 6, "other" }, { "Kirti", "other", 1, "female" }, { "Kirti", "other", 3, "female" }, { "Kirti", "female", 1, "female" }, { "Kirti", "female", 5, "female" }, { "Kirti", "female", 1, "other" }, { "Kirti", "female", 5, "other" }, { "Kirti", "mixed", 1, "mixed" }, { "Kirti", "mixed", 1, "other" }, { "Kirti", "female", 1, "mixed" }, { "Kirti", "mixed", 5, "mixed" }, { "Kirti", "mixed", 5, "other" }, { "Kirti", "female", 5, "mixed" } };
    String[] exp5 = { "Kirti und sein Freund gingen nach Paris.", "Kirti und seine 6 Freunde gingen nach Paris.", "Kirti und seine Freundin gingen nach Paris.", "Kirti und seine 3 Freundinnen gingen nach Paris.", "Kirti und ihre Freundin  gingen nach Paris.", "Kirti und ihre 5 Freundinnen  gingen nach Paris.", "Kirti und ihr Freund  gingen nach Paris.", "Kirti und ihre 5 Freunde  gingen nach Paris.", "Kirti und sein Freund gingen nach Paris.", "Kirti und sein Freund gingen nach Paris.", "Kirti und ihr Freund  gingen nach Paris.", "Kirti und seine 5 Freunde gingen nach Paris.", "Kirti und seine 5 Freunde gingen nach Paris.", "Kirti und ihre 5 Freunde  gingen nach Paris." };
    // Format
    for (int i = 0; i < 14; i++) {
        assertEquals("ERROR:Failure in format with nested Pattern 3", exp5[i], msgFmt.format(testArgs5[i]));
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) AttributedString(java.text.AttributedString) Test(org.junit.Test)

Example 24 with MessageFormat

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

the class TestMessageFormat method TestParse.

@Test
public void TestParse() {
    String msgFormatString = "{0} =sep= {1}";
    MessageFormat msg = new MessageFormat(msgFormatString);
    String source = "abc =sep= def";
    try {
        Object[] fmt_arr = msg.parse(source);
        if (fmt_arr.length != 2) {
            errln("*** MSG parse (ustring, count, err) count err.");
        } else {
            // TODO: This if statement seems to be redundant. [tschumann]
            if (fmt_arr.length != 2) {
                errln("*** MSG parse (ustring, parsepos., count) count err.");
            } else {
                assertEquals("parse()[0]", "abc", fmt_arr[0]);
                assertEquals("parse()[1]", "def", fmt_arr[1]);
            }
        }
    } catch (ParseException e1) {
        errln("*** MSG parse (ustring, count, err) error.");
    }
    ParsePosition pp = new ParsePosition(0);
    Object[] fmt_arr = msg.parse(source, pp);
    if (pp.getIndex() == 0 || fmt_arr == null) {
        errln("*** MSG parse (ustring, parsepos., count) error.");
    } else {
        if (fmt_arr.length != 2) {
            errln("*** MSG parse (ustring, parsepos., count) count err.");
        } else {
            assertEquals("parse()[0]", "abc", fmt_arr[0]);
            assertEquals("parse()[1]", "def", fmt_arr[1]);
        }
    }
    pp.setIndex(0);
    Object[] fmta;
    fmta = (Object[]) msg.parseObject(source, pp);
    if (pp.getIndex() == 0) {
        errln("*** MSG parse (ustring, Object, parsepos ) error.");
    } else {
        if (fmta.length != 2) {
            errln("*** MSG parse (ustring, count, err) count err.");
        } else {
            // [tschumann]
            if (fmt_arr.length != 2) {
                errln("*** MSG parse (ustring, parsepos., count) count err.");
            } else {
                // TODO: Don't we want to check fmta? [tschumann]
                assertEquals("parse()[0]", "abc", fmt_arr[0]);
                assertEquals("parse()[1]", "def", fmt_arr[1]);
            }
        }
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) AttributedString(java.text.AttributedString) ParseException(java.text.ParseException) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 25 with MessageFormat

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

the class TestMessageFormat method TestHashCode.

@Test
public void TestHashCode() {
    ULocale save = ULocale.getDefault();
    ULocale.setDefault(ULocale.US);
    MessageFormat x = new MessageFormat("There are {0} files on {1}");
    MessageFormat z = new MessageFormat("There are {0} files on {1}");
    MessageFormat y = null;
    y = (MessageFormat) x.clone();
    if (x.hashCode() != y.hashCode())
        errln("FAIL: identical objects have different hashcodes");
    if (x.hashCode() != z.hashCode())
        errln("FAIL: identical objects have different hashcodes");
    /* These are not errors
        y.setLocale(ULocale.FRENCH);
        if (x.hashCode() == y.hashCode())
            errln("FAIL: different objects have same hashcodes. Locale ignored");

        z.applyPattern("There are {0} files on {1} the disk");
        if (x.hashCode() == z.hashCode())
            errln("FAIL: different objects have same hashcodes. Pattern ignored");
    */
    ULocale.setDefault(save);
}
Also used : ULocale(android.icu.util.ULocale) 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