aboutsummaryrefslogtreecommitdiff
path: root/tests/src/com/android/messageformat/SimpleMessageFormatTest.java
blob: e132b14a1fd3600a0124ea94c1d2707f837fa61e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 *******************************************************************************
 * Copyright (C) 2014, International Business Machines Corporation and
 * others. All Rights Reserved.
 *******************************************************************************
 */
package com.android.messageformat;

import java.util.Date;
import java.util.Locale;
import junit.framework.TestCase;

public class SimpleMessageFormatTest extends TestCase {
    public void testBasic() {
        assertEquals("one simple argument", "Going to Germany and back",
                MessageFormat.formatNamedArgs(
                        Locale.US, "Going to {place} and back", "place", "Germany"));
    }

    public void testSelect() {
        String msg = "{gender,select,female{her book}male{his book}other{their book}}";
        assertEquals("select female", "her book",
                MessageFormat.formatNamedArgs(Locale.US, msg, "gender", "female"));
        assertEquals("select male", "his book",
                MessageFormat.formatNamedArgs(Locale.US, msg, "gender", "male"));
        assertEquals("select neutral", "their book",
                MessageFormat.formatNamedArgs(Locale.US, msg, "gender", "unknown"));
    }

    public void testPlural() {
        // Using Serbian, see
        // http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html
        Locale sr = new Locale("sr");
        String msg =
                "{num,plural,offset:1 =1{only {name}}=2{{name} and one other}" +
                        "one{{name} and #-one others}few{{name} and #-few others}" +
                        "other{{name} and #... others}}";
        assertEquals("plural 1", "only Peter",
                MessageFormat.formatNamedArgs(sr, msg, "num", 1, "name", "Peter"));
        assertEquals("plural 2", "Paul and one other",
                MessageFormat.formatNamedArgs(sr, msg, "num", 2, "name", "Paul"));
        assertEquals("plural 22", "Mary and 21-one others",
                MessageFormat.formatNamedArgs(sr, msg, "num", 22, "name", "Mary"));
        assertEquals("plural 33", "John and 32-few others",
                MessageFormat.formatNamedArgs(sr, msg, "num", 33, "name", "John"));
        assertEquals("plural 6", "Yoko and 5... others",
                MessageFormat.formatNamedArgs(sr, msg, "num", 6, "name", "Yoko"));
    }

    public void testSelectAndPlural() {
        Locale ja = Locale.JAPANESE;  // always "other"
        String msg =
                "{gender,select,female{" +
                        "{num,plural,=1{her book}other{her # books}}" +
                        "}male{" +
                        "{num,plural,=1{his book}other{his # books}}" +
                        "}other{" +
                        "{num,plural,=1{their book}other{their # books}}" +
                        "}}";
        assertEquals("female 1", "her book",
                MessageFormat.formatNamedArgs(ja, msg, "gender", "female", "num", 1));
        assertEquals("male 2", "his 2 books",
                MessageFormat.formatNamedArgs(ja, msg, "gender", "male", "num", 2));
        assertEquals("unknown 3000", "their 3,000 books",
                MessageFormat.formatNamedArgs(ja, msg, "gender", "?", "num", 3000));
    }

    public void testSelectOrdinal() {
        Locale en = Locale.ENGLISH;
        String msg =
                "{num,selectordinal,one{#st floor}two{#nd floor}few{#rd floor}other{#th floor}}";
        assertEquals("91", "91st floor",
                MessageFormat.formatNamedArgs(en, msg, "num", 91));
        assertEquals("22", "22nd floor",
                MessageFormat.formatNamedArgs(en, msg, "num", 22));
        assertEquals("33", "33rd floor",
                MessageFormat.formatNamedArgs(en, msg, "num", 33));
        assertEquals("11", "11th floor",
                MessageFormat.formatNamedArgs(en, msg, "num", 11));
    }
}