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) 2014-2016, International Business Machines Corporation and 7 * others. All Rights Reserved. 8 ******************************************************************************* 9 */ 10 package android.icu.dev.test.util; 11 12 import org.junit.Test; 13 import org.junit.runner.RunWith; 14 import org.junit.runners.JUnit4; 15 16 import android.icu.dev.test.TestFmwk; 17 import android.icu.text.MessageFormat; 18 import android.icu.text.SimpleFormatter; 19 import android.icu.util.ULocale; 20 import android.icu.testsharding.MainTestShard; 21 22 @MainTestShard 23 @RunWith(JUnit4.class) 24 public class SimpleFormatterTest extends TestFmwk { 25 26 /** 27 * Constructor 28 */ 29 public SimpleFormatterTest() 30 { 31 } 32 33 // public methods ----------------------------------------------- 34 35 @Test 36 public void TestWithNoArguments() { 37 SimpleFormatter fmt = SimpleFormatter.compile("This doesn''t have templates '{0}"); 38 assertEquals( 39 "getArgumentLimit", 40 0, 41 fmt.getArgumentLimit()); 42 assertEquals( 43 "format", 44 "This doesn't have templates {0}", 45 fmt.format("unused")); 46 assertEquals( 47 "format with values=null", 48 "This doesn't have templates {0}", 49 fmt.format((CharSequence[])null)); 50 assertEquals( 51 "toString", 52 "This doesn't have templates {0}", 53 fmt.toString()); 54 int[] offsets = new int[1]; 55 assertEquals( 56 "formatAndAppend", 57 "This doesn't have templates {0}", 58 fmt.formatAndAppend(new StringBuilder(), offsets).toString()); 59 assertEquals( 60 "offsets[0]", 61 -1, 62 offsets[0]); 63 assertEquals( 64 "formatAndAppend with values=null", 65 "This doesn't have templates {0}", 66 fmt.formatAndAppend(new StringBuilder(), null, (CharSequence[])null).toString()); 67 assertEquals( 68 "formatAndReplace with values=null", 69 "This doesn't have templates {0}", 70 fmt.formatAndReplace(new StringBuilder(), null, (CharSequence[])null).toString()); 71 } 72 73 @Test 74 public void TestSyntaxErrors() { 75 try { 76 SimpleFormatter.compile("{}"); 77 fail("Syntax error did not yield an exception."); 78 } catch (IllegalArgumentException expected) { 79 } 80 try { 81 SimpleFormatter.compile("{12d"); 82 fail("Syntax error did not yield an exception."); 83 } catch (IllegalArgumentException expected) { 84 } 85 } 86 87 @Test 88 public void TestOneArgument() { 89 assertEquals("TestOneArgument", 90 "1 meter", 91 SimpleFormatter.compile("{0} meter").format("1")); 92 } 93 94 @Test 95 public void TestBigArgument() { 96 SimpleFormatter fmt = SimpleFormatter.compile("a{20}c"); 97 assertEquals("{20} count", 21, fmt.getArgumentLimit()); 98 CharSequence[] values = new CharSequence[21]; 99 values[20] = "b"; 100 assertEquals("{20}=b", "abc", fmt.format(values)); 101 } 102 103 @Test 104 public void TestGetTextWithNoArguments() { 105 assertEquals( 106 "", 107 "Templates and are here.", 108 SimpleFormatter.compile( 109 "Templates {1}{2} and {3} are here.").getTextWithNoArguments()); 110 } 111 112 @Test 113 public void TestTooFewArgumentValues() { 114 SimpleFormatter fmt = SimpleFormatter.compile( 115 "Templates {2}{1} and {4} are out of order."); 116 try { 117 fmt.format("freddy", "tommy", "frog", "leg"); 118 fail("Expected IllegalArgumentException"); 119 } catch (IllegalArgumentException e) { 120 // Expected 121 } 122 try { 123 fmt.formatAndAppend( 124 new StringBuilder(), null, "freddy", "tommy", "frog", "leg"); 125 fail("Expected IllegalArgumentException"); 126 } catch (IllegalArgumentException e) { 127 // Expected 128 } 129 try { 130 fmt.formatAndReplace( 131 new StringBuilder(), null, "freddy", "tommy", "frog", "leg"); 132 fail("Expected IllegalArgumentException"); 133 } catch (IllegalArgumentException e) { 134 // Expected 135 } 136 } 137 138 @Test 139 public void TestWithArguments() { 140 SimpleFormatter fmt = SimpleFormatter.compile( 141 "Templates {2}{1} and {4} are out of order."); 142 assertEquals( 143 "getArgumentLimit", 144 5, 145 fmt.getArgumentLimit()); 146 assertEquals( 147 "toString", 148 "Templates {2}{1} and {4} are out of order.", 149 fmt.toString()); 150 int[] offsets = new int[6]; 151 assertEquals( 152 "format", 153 "123456: Templates frogtommy and {0} are out of order.", 154 fmt.formatAndAppend( 155 new StringBuilder("123456: "), 156 offsets, 157 "freddy", "tommy", "frog", "leg", "{0}").toString()); 158 159 int[] expectedOffsets = {-1, 22, 18, -1, 32, -1}; 160 verifyOffsets(expectedOffsets, offsets); 161 } 162 163 @Test 164 public void TestFormatUseAppendToAsArgument() { 165 SimpleFormatter fmt = SimpleFormatter.compile( 166 "Arguments {0} and {1}"); 167 StringBuilder appendTo = new StringBuilder("previous:"); 168 try { 169 fmt.formatAndAppend(appendTo, null, appendTo, "frog"); 170 fail("IllegalArgumentException expected."); 171 } catch (IllegalArgumentException e) { 172 // expected. 173 } 174 } 175 176 @Test 177 public void TestFormatReplaceNoOptimization() { 178 SimpleFormatter fmt = SimpleFormatter.compile("{2}, {0}, {1} and {3}"); 179 int[] offsets = new int[4]; 180 StringBuilder result = new StringBuilder("original"); 181 assertEquals( 182 "format", 183 "frog, original, freddy and by", 184 fmt.formatAndReplace( 185 result, 186 offsets, 187 result, "freddy", "frog", "by").toString()); 188 189 int[] expectedOffsets = {6, 16, 0, 27}; 190 verifyOffsets(expectedOffsets, offsets); 191 } 192 193 194 @Test 195 public void TestFormatReplaceNoOptimizationLeadingText() { 196 SimpleFormatter fmt = SimpleFormatter.compile("boo {2}, {0}, {1} and {3}"); 197 int[] offsets = new int[4]; 198 StringBuilder result = new StringBuilder("original"); 199 assertEquals( 200 "format", 201 "boo original, freddy, frog and by", 202 fmt.formatAndReplace( 203 result, 204 offsets, 205 "freddy", "frog", result, "by").toString()); 206 207 int[] expectedOffsets = {14, 22, 4, 31}; 208 verifyOffsets(expectedOffsets, offsets); 209 } 210 211 @Test 212 public void TestFormatReplaceOptimization() { 213 SimpleFormatter fmt = SimpleFormatter.compile("{2}, {0}, {1} and {3}"); 214 int[] offsets = new int[4]; 215 StringBuilder result = new StringBuilder("original"); 216 assertEquals( 217 "format", 218 "original, freddy, frog and by", 219 fmt.formatAndReplace( 220 result, 221 offsets, 222 "freddy", "frog", result, "by").toString()); 223 224 int[] expectedOffsets = {10, 18, 0, 27}; 225 verifyOffsets(expectedOffsets, offsets); 226 } 227 228 @Test 229 public void TestFormatReplaceOptimizationNoOffsets() { 230 SimpleFormatter fmt = SimpleFormatter.compile("{2}, {0}, {1} and {3}"); 231 StringBuilder result = new StringBuilder("original"); 232 assertEquals( 233 "format", 234 "original, freddy, frog and by", 235 fmt.formatAndReplace( 236 result, 237 null, 238 "freddy", "frog", result, "by").toString()); 239 240 } 241 242 @Test 243 public void TestFormatReplaceNoOptimizationNoOffsets() { 244 SimpleFormatter fmt = SimpleFormatter.compile( 245 "Arguments {0} and {1}"); 246 StringBuilder result = new StringBuilder("previous:"); 247 assertEquals( 248 "", 249 "Arguments previous: and frog", 250 fmt.formatAndReplace(result, null, result, "frog").toString()); 251 } 252 253 @Test 254 public void TestFormatReplaceNoOptimizationLeadingArgumentUsedTwice() { 255 SimpleFormatter fmt = SimpleFormatter.compile( 256 "{2}, {0}, {1} and {3} {2}"); 257 StringBuilder result = new StringBuilder("original"); 258 int[] offsets = new int[4]; 259 assertEquals( 260 "", 261 "original, freddy, frog and by original", 262 fmt.formatAndReplace( 263 result, 264 offsets, 265 "freddy", "frog", result, "by").toString()); 266 int[] expectedOffsets = {10, 18, 30, 27}; 267 verifyOffsets(expectedOffsets, offsets); 268 } 269 270 @Test 271 public void TestQuotingLikeMessageFormat() { 272 String pattern = "{0} don't can''t '{5}''}{a' again '}'{1} to the '{end"; 273 SimpleFormatter spf = SimpleFormatter.compile(pattern); 274 MessageFormat mf = new MessageFormat(pattern, ULocale.ROOT); 275 String expected = "X don't can't {5}'}{a again }Y to the {end"; 276 assertEquals("MessageFormat", expected, mf.format(new Object[] { "X", "Y" })); 277 assertEquals("SimpleFormatter", expected, spf.format("X", "Y")); 278 } 279 280 private void verifyOffsets(int[] expected, int[] actual) { 281 for (int i = 0; i < expected.length; ++i) { 282 if (expected[i] != actual[i]) { 283 errln("Expected "+expected[i]+", got " + actual[i]); 284 } 285 } 286 } 287 288 } 289