18d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath/*
28d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath *******************************************************************************
38d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath * Copyright (C) 2014, International Business Machines Corporation and
48d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath * others. All Rights Reserved.
58d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath *******************************************************************************
68d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath */
78d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamathpackage com.android.messageformat;
88d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath
98d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamathimport java.util.Date;
108d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamathimport java.util.Locale;
118d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamathimport junit.framework.TestCase;
128d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath
138d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamathpublic class SimpleMessageFormatTest extends TestCase {
148d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath    public void testBasic() {
158d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("one simple argument", "Going to Germany and back",
168d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(
178d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                        Locale.US, "Going to {place} and back", "place", "Germany"));
188d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath    }
198d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath
208d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath    public void testSelect() {
218d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        String msg = "{gender,select,female{her book}male{his book}other{their book}}";
228d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("select female", "her book",
238d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(Locale.US, msg, "gender", "female"));
248d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("select male", "his book",
258d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(Locale.US, msg, "gender", "male"));
268d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("select neutral", "their book",
278d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(Locale.US, msg, "gender", "unknown"));
288d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath    }
298d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath
308d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath    public void testPlural() {
318d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        // Using Serbian, see
328d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        // http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html
338d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        Locale sr = new Locale("sr");
348d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        String msg =
358d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                "{num,plural,offset:1 =1{only {name}}=2{{name} and one other}" +
368d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                        "one{{name} and #-one others}few{{name} and #-few others}" +
378d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                        "other{{name} and #... others}}";
388d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("plural 1", "only Peter",
398d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(sr, msg, "num", 1, "name", "Peter"));
408d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("plural 2", "Paul and one other",
418d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(sr, msg, "num", 2, "name", "Paul"));
428d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("plural 22", "Mary and 21-one others",
438d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(sr, msg, "num", 22, "name", "Mary"));
448d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("plural 33", "John and 32-few others",
458d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(sr, msg, "num", 33, "name", "John"));
468d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("plural 6", "Yoko and 5... others",
478d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(sr, msg, "num", 6, "name", "Yoko"));
488d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath    }
498d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath
508d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath    public void testSelectAndPlural() {
518d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        Locale ja = Locale.JAPANESE;  // always "other"
528d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        String msg =
538d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                "{gender,select,female{" +
548d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                        "{num,plural,=1{her book}other{her # books}}" +
558d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                        "}male{" +
568d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                        "{num,plural,=1{his book}other{his # books}}" +
578d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                        "}other{" +
588d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                        "{num,plural,=1{their book}other{their # books}}" +
598d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                        "}}";
608d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("female 1", "her book",
618d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(ja, msg, "gender", "female", "num", 1));
628d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("male 2", "his 2 books",
638d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(ja, msg, "gender", "male", "num", 2));
648d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("unknown 3000", "their 3,000 books",
658d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(ja, msg, "gender", "?", "num", 3000));
668d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath    }
678d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath
688d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath    public void testSelectOrdinal() {
698d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        Locale en = Locale.ENGLISH;
708d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        String msg =
718d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                "{num,selectordinal,one{#st floor}two{#nd floor}few{#rd floor}other{#th floor}}";
728d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("91", "91st floor",
738d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(en, msg, "num", 91));
748d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("22", "22nd floor",
758d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(en, msg, "num", 22));
768d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("33", "33rd floor",
778d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(en, msg, "num", 33));
788d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath        assertEquals("11", "11th floor",
798d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath                MessageFormat.formatNamedArgs(en, msg, "num", 11));
808d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath    }
818d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath}
828d05787d6a4b5762d790ccd2a9ed9dc8885986efNarayan Kamath
83