Search in sources :

Example 26 with MessageFormat

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

the class TestMessageFormat method TestNullArgs.

// Test case for null arguments.
// Ticket#6361
@Test
public void TestNullArgs() {
    MessageFormat msgfmt = new MessageFormat("{0} - {1}");
    Object[][] TEST_CASES = { { null, "{0} - {1}" }, { new Object[] { null }, "null - {1}" }, { new Object[] { null, null }, "null - null" }, { new Object[] { "one" }, "one - {1}" }, { new Object[] { "one", null }, "one - null" }, { new Object[] { null, "two" }, "null - two" } };
    for (int i = 0; i < TEST_CASES.length; i++) {
        String text = msgfmt.format(TEST_CASES[i][0]);
        if (!text.equals(TEST_CASES[i][1])) {
            errln("FAIL: Returned[" + text + "] Expected[" + TEST_CASES[i][1] + "]");
        }
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) AttributedString(java.text.AttributedString) Test(org.junit.Test)

Example 27 with MessageFormat

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

the class TestMessageFormat method TestCompatibleApostrophe.

// Compare behavior of JDK and ICU's DOUBLE_REQUIRED compatibility mode.
@Test
public void TestCompatibleApostrophe() {
    // Message with choice argument which does not contain another argument.
    // The JDK performs only one apostrophe-quoting pass on this pattern.
    String pattern = "ab{0,choice,0#1'2''3'''4''''.}yz";
    java.text.MessageFormat jdkMsg = new java.text.MessageFormat(pattern, Locale.ENGLISH);
    MessageFormat compMsg = new MessageFormat("", Locale.ENGLISH);
    compMsg.applyPattern(pattern, MessagePattern.ApostropheMode.DOUBLE_REQUIRED);
    assertEquals("wrong value", MessagePattern.ApostropheMode.DOUBLE_REQUIRED, compMsg.getApostropheMode());
    MessageFormat icuMsg = new MessageFormat("", Locale.ENGLISH);
    icuMsg.applyPattern(pattern, MessagePattern.ApostropheMode.DOUBLE_OPTIONAL);
    assertEquals("wrong value", MessagePattern.ApostropheMode.DOUBLE_OPTIONAL, icuMsg.getApostropheMode());
    Object[] zero0 = new Object[] { 0 };
    assertEquals("unexpected JDK MessageFormat apostrophe behavior", "ab12'3'4''.yz", jdkMsg.format(zero0));
    assertEquals("incompatible ICU MessageFormat compatibility-apostrophe behavior", "ab12'3'4''.yz", compMsg.format(zero0));
    assertEquals("unexpected ICU MessageFormat double-apostrophe-optional behavior", "ab1'2'3''4''.yz", icuMsg.format(zero0));
    // Message with choice argument which contains a nested simple argument.
    // The JDK performs two apostrophe-quoting passes.
    pattern = "ab{0,choice,0#1'2''3'''4''''.{0,number,'#x'}}yz";
    jdkMsg.applyPattern(pattern);
    compMsg.applyPattern(pattern);
    icuMsg.applyPattern(pattern);
    assertEquals("unexpected JDK MessageFormat apostrophe behavior", "ab1234'.0xyz", jdkMsg.format(zero0));
    assertEquals("incompatible ICU MessageFormat compatibility-apostrophe behavior", "ab1234'.0xyz", compMsg.format(zero0));
    assertEquals("unexpected ICU MessageFormat double-apostrophe-optional behavior", "ab1'2'3''4''.#x0yz", icuMsg.format(zero0));
    // Message with choice argument which contains a nested choice argument.
    // The JDK fails to parse this pattern.
    // jdkMsg.applyPattern("cd{0,choice,0#ef{0,choice,0#1'2''3'''4''''.}uv}wx");
    // For lack of comparison, we do not test ICU with this pattern.
    // The JDK ChoiceFormat itself always performs one apostrophe-quoting pass.
    ChoiceFormat choice = new ChoiceFormat("0#1'2''3'''4''''.");
    assertEquals("unexpected JDK ChoiceFormat apostrophe behavior", "12'3'4''.", choice.format(0));
    choice.applyPattern("0#1'2''3'''4''''.{0,number,'#x'}");
    assertEquals("unexpected JDK ChoiceFormat apostrophe behavior", "12'3'4''.{0,number,#x}", choice.format(0));
}
Also used : MessageFormat(android.icu.text.MessageFormat) ChoiceFormat(java.text.ChoiceFormat) AttributedString(java.text.AttributedString) Test(org.junit.Test)

Example 28 with MessageFormat

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

the class TestMessageFormat method TestSimpleFormat.

@Test
public void TestSimpleFormat() {
    Object[] testArgs1 = { new Integer(0), "MyDisk" };
    Object[] testArgs2 = { new Integer(1), "MyDisk" };
    Object[] testArgs3 = { new Integer(12), "MyDisk" };
    MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0} file(s).");
    StringBuffer string = new StringBuffer();
    FieldPosition ignore = new FieldPosition(FieldPosition_DONT_CARE);
    form.format(testArgs1, string, ignore);
    assertEquals("format", "The disk \"MyDisk\" contains 0 file(s).", string.toString());
    string.setLength(0);
    form.format(testArgs2, string, ignore);
    assertEquals("format", "The disk \"MyDisk\" contains 1 file(s).", string.toString());
    string.setLength(0);
    form.format(testArgs3, string, ignore);
    assertEquals("format", "The disk \"MyDisk\" contains 12 file(s).", string.toString());
}
Also used : MessageFormat(android.icu.text.MessageFormat) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Example 29 with MessageFormat

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

the class TestMessageFormat method TestTrimArgumentName.

@Test
public void TestTrimArgumentName() {
    // ICU 4.8 allows and ignores white space around argument names and numbers.
    MessageFormat m = new MessageFormat("a { 0 , number , '#,#'#.0 } z", Locale.ENGLISH);
    assertEquals("trim-numbered-arg format() failed", "a  #,#2.0  z", m.format(new Object[] { 2 }));
    m.applyPattern("x { _oOo_ , number , integer } y");
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("_oOo_", new Integer(3));
    StringBuffer result = new StringBuffer();
    assertEquals("trim-named-arg format() failed", "x 3 y", m.format(map, result, new FieldPosition(0)).toString());
}
Also used : MessageFormat(android.icu.text.MessageFormat) HashMap(java.util.HashMap) AttributedString(java.text.AttributedString) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Example 30 with MessageFormat

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

the class TestMessageFormat method TestPattern.

@Test
public // aka PatternTest()
void TestPattern() {
    Object[] testArgs = { new Double(1), new Double(3456), "Disk", new Date(1000000000L) };
    String[] testCases = { "Quotes '', '{', 'a' {0} '{0}'", "Quotes '', '{', 'a' {0,number} '{0}'", "'{'1,number,'#',##} {1,number,'#',##}", "There are {1} files on {2} at {3}.", "On {2}, there are {1} files, with {0,number,currency}.", "'{1,number,percent}', {1,number,percent},", "'{1,date,full}', {1,date,full},", "'{3,date,full}', {3,date,full},", "'{1,number,#,##}' {1,number,#,##}" };
    // ICU 4.8 returns the original pattern (testCases)
    // rather than toPattern() reconstituting a new, equivalent pattern string (testResultPatterns).
    /*String testResultPatterns[] = {
            "Quotes '', '{', a {0} '{'0}",
            "Quotes '', '{', a {0,number} '{'0}",
            "'{'1,number,#,##} {1,number,'#'#,##}",
            "There are {1} files on {2} at {3}.",
            "On {2}, there are {1} files, with {0,number,currency}.",
            "'{'1,number,percent}, {1,number,percent},",
            "'{'1,date,full}, {1,date,full},",
            "'{'3,date,full}, {3,date,full},",
            "'{'1,number,#,##} {1,number,#,##}"
        };*/
    String[] testResultStrings = { "Quotes ', {, 'a' 1 {0}", "Quotes ', {, 'a' 1 {0}", "{1,number,'#',##} #34,56", "There are 3,456 files on Disk at 1/12/70, 5:46 AM.", "On Disk, there are 3,456 files, with $1.00.", "{1,number,percent}, 345,600%,", "{1,date,full}, Wednesday, December 31, 1969,", "{3,date,full}, Monday, January 12, 1970,", "{1,number,#,##} 34,56" };
    for (int i = 0; i < 9; ++i) {
        // it_out << "\nPat in:  " << testCases[i]);
        // String buffer;
        MessageFormat form = null;
        try {
            form = new MessageFormat(testCases[i], Locale.US);
        } catch (IllegalArgumentException e1) {
            errln("MessageFormat for " + testCases[i] + " creation failed.");
            continue;
        }
        // ICU 4.8 returns the original pattern (testCases)
        // rather than toPattern() reconstituting a new, equivalent pattern string (testResultPatterns).
        // assertEquals("\"" + testCases[i] + "\".toPattern()", testResultPatterns[i], form.toPattern());
        assertEquals("\"" + testCases[i] + "\".toPattern()", testCases[i], form.toPattern());
        // Note: An alternative test would be to build MessagePattern objects for
        // both the input and output patterns and compare them, taking SKIP_SYNTAX etc.
        // into account.
        // (Too much trouble...)
        // it_out << "Pat out: " << form.toPattern(buffer));
        StringBuffer result = new StringBuffer();
        FieldPosition fieldpos = new FieldPosition(0);
        form.format(testArgs, result, fieldpos);
        assertEquals("format", testResultStrings[i], result.toString());
    // it_out << "Result:  " << result);
    // /* TODO: Look at this test and see if this is still a valid test */
    // logln("---------------- test parse ----------------");
    // 
    // int count = 4;
    // form.toPattern(buffer);
    // logln("MSG pattern for parse: " + buffer);
    // 
    // int parseCount = 0;
    // Formattable* values = form.parse(result, parseCount, success);
    // if (U_FAILURE(success)) {
    // errln("MessageFormat failed test #5");
    // logln(String("MessageFormat failed test #5 with error code ")+(int)success);
    // } else if (parseCount != count) {
    // errln("MSG count not %d as expected. Got %d", count, parseCount);
    // }
    // UBool failed = FALSE;
    // for (int j = 0; j < parseCount; ++j) {
    // if (values == 0 || testArgs[j] != values[j]) {
    // errln(((String)"MSG testargs[") + j + "]: " + toString(testArgs[j]));
    // errln(((String)"MSG values[") + j + "]  : " + toString(values[j]));
    // failed = TRUE;
    // }
    // }
    // if (failed)
    // errln("MessageFormat failed test #6");
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) AttributedString(java.text.AttributedString) FieldPosition(java.text.FieldPosition) Date(java.util.Date) 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