1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ******************************************************************************* 6 * Copyright (C) 2012-2013, Google, International Business Machines Corporation and 7 * others. All Rights Reserved. 8 ******************************************************************************* 9 */ 10 package android.icu.dev.test.format; 11 12 import java.util.ArrayList; 13 import java.util.Locale; 14 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 import org.junit.runners.JUnit4; 18 19 import android.icu.dev.test.TestFmwk; 20 import android.icu.text.ListFormatter; 21 import android.icu.util.ULocale; 22 import android.icu.testsharding.MainTestShard; 23 24 @MainTestShard 25 @RunWith(JUnit4.class) 26 public class ListFormatterTest extends TestFmwk { 27 String[] HardcodedTestData = { 28 "", 29 "A", 30 "A and B", 31 "A; B, and C", 32 "A; B, C, and D", 33 "A; B, C, D, and E" 34 }; 35 36 @Test 37 public void TestBasic() { 38 ListFormatter formatter = new ListFormatter("{0} and {1}", "{0}; {1}", "{0}, {1}", "{0}, and {1}"); 39 checkData(formatter, HardcodedTestData); 40 } 41 42 String[] EnglishTestData = { 43 "", 44 "A", 45 "A and B", 46 "A, B, and C", 47 "A, B, C, and D", 48 "A, B, C, D, and E" 49 }; 50 51 @Test 52 public void TestEnglish() { 53 checkData(ListFormatter.getInstance(ULocale.ENGLISH), EnglishTestData); 54 checkData(ListFormatter.getInstance(ULocale.US), EnglishTestData); 55 // Redundant tests for code coverage. 56 checkData(ListFormatter.getInstance(Locale.ENGLISH), EnglishTestData); 57 if (isDefaultLocaleEnglishLike()) { 58 checkData(ListFormatter.getInstance(), EnglishTestData); 59 } 60 } 61 62 // Tests resource loading and inheritance when region sublocale 63 // has only partial data for the listPattern element (overriding 64 // some of the parent data). #12994 65 String[] EnglishGBTestData = { 66 "", 67 "A", 68 "A and B", 69 "A, B and C", 70 "A, B, C and D", 71 "A, B, C, D and E" 72 }; 73 74 @Test 75 public void TestEnglishGB() { 76 checkData(ListFormatter.getInstance(new ULocale("en_GB")), EnglishGBTestData); 77 } 78 79 // Tests resource loading and inheritance when region sublocale 80 // has only partial data for the listPattern element (overriding 81 // some of the parent data). #12994 82 String[] ChineseTradHKTestData = { 83 "", 84 "A", 85 "A\u53CAB", 86 "A\u3001B\u53CAC", 87 "A\u3001B\u3001C\u53CAD", 88 "A\u3001B\u3001C\u3001D\u53CAE" 89 }; 90 91 @Test 92 public void TestChineseTradHK() { 93 checkData(ListFormatter.getInstance(new ULocale("zh_Hant_HK")), ChineseTradHKTestData); 94 } 95 96 String[] JapaneseTestData = { 97 "", 98 "A", 99 "AB", 100 "ABC", 101 "ABCD", 102 "ABCDE" 103 }; 104 105 @Test 106 public void TestJapanese() { 107 checkData(ListFormatter.getInstance(ULocale.JAPANESE), JapaneseTestData); 108 } 109 110 String[] outOfOrderTestData = { 111 "", 112 "A", 113 "B after A", 114 "C in the last after B after the first A", 115 "D in the last after C after B after the first A", 116 "E in the last after D after C after B after the first A" 117 }; 118 @Test 119 public void TestPatternOutOfOrder() { 120 ListFormatter formatter = new ListFormatter("{1} after {0}", "{1} after the first {0}", "{1} after {0}", 121 "{1} in the last after {0}"); 122 checkData(formatter, outOfOrderTestData); 123 } 124 125 String[] RootTestData = { 126 "", 127 "A", 128 "A, B", 129 "A, B, C", 130 "A, B, C, D", 131 "A, B, C, D, E" 132 }; 133 134 @Test 135 public void TestSpecial() { 136 checkData(ListFormatter.getInstance(ULocale.ROOT), RootTestData); 137 if (isDefaultLocaleEnglishLike()) { 138 checkData(ListFormatter.getInstance(new ULocale("xxx")), EnglishTestData); 139 } 140 } 141 142 public void checkData(ListFormatter listFormat, String[] strings) { 143 assertEquals("0", strings[0], listFormat.format()); 144 assertEquals("1", strings[1], listFormat.format("A")); 145 assertEquals("2", strings[2], listFormat.format("A", "B")); 146 assertEquals("3", strings[3], listFormat.format("A", "B", "C")); 147 assertEquals("4", strings[4], listFormat.format("A", "B", "C", "D")); 148 assertEquals("5", strings[5], listFormat.format("A", "B", "C", "D", "E")); 149 } 150 151 @Test 152 public void TestFromList() { 153 ListFormatter listFormatter = ListFormatter.getInstance(ULocale.ENGLISH); 154 ArrayList<String> list = new ArrayList<String>(); 155 list.add("A"); 156 list.add("B"); 157 list.add("C"); 158 assertEquals("list", "A, B, and C", listFormatter.format(list)); 159 } 160 161 @Test 162 public void TestCreatePatternForNumItems() { 163 ListFormatter listFormatter = ListFormatter.getInstance(ULocale.ENGLISH); 164 assertEquals( 165 "createPatternForNumItems", 166 "{0}, {1}, and {2}", 167 listFormatter.getPatternForNumItems(3)); 168 } 169 170 @Test 171 public void TestGetPatternForNumItemsException() { 172 ListFormatter listFormatter = ListFormatter.getInstance(ULocale.ENGLISH); 173 try { 174 listFormatter.getPatternForNumItems(0); 175 fail("IllegalArgumentException expected."); 176 } catch (IllegalArgumentException expected) { 177 // expected. 178 } 179 } 180 181 @Test 182 public void TestGetLocale() { 183 assertEquals( 184 "getLocale", ULocale.ENGLISH, ListFormatter.getInstance(ULocale.ENGLISH).getLocale()); 185 } 186 187 @Test 188 public void Test9946() { 189 ListFormatter listFormatter = ListFormatter.getInstance(ULocale.ENGLISH); 190 assertEquals("bug 9946", "{0}, {1}, and {2}", listFormatter.format("{0}", "{1}", "{2}")); 191 } 192 193 private boolean isDefaultLocaleEnglishLike() { 194 ULocale defaultLocale = ULocale.getDefault(ULocale.Category.FORMAT); 195 return defaultLocale.equals(ULocale.ENGLISH) || defaultLocale.equals(ULocale.US); 196 } 197 } 198