public void Test4118594() { MessageFormat mf = new MessageFormat("{0}, {0}, {0}"); String forParsing = "x, y, z"; Object[] objs = mf.Parse(forParsing, new ParsePosition(0)); Logln("pattern: \"" + mf.ToPattern() + "\""); Logln("text for parsing: \"" + forParsing + "\""); if (!objs[0].ToString().Equals("z")) { Errln("argument0: \"" + objs[0] + "\""); } mf.SetLocale(new CultureInfo("en-us")); mf.ApplyPattern("{0,number,#.##}, {0,number,#.#}"); Object[] oldobjs = { 3.1415d }; String result = mf.Format(oldobjs); Logln("pattern: \"" + mf.ToPattern() + "\""); Logln("text for parsing: \"" + result + "\""); // result now equals "3.14, 3.1" if (!result.Equals("3.14, 3.1")) { Errln("result = " + result); } Object[] newobjs = mf.Parse(result, new ParsePosition(0)); // newobjs now equals {new Double(3.1)} if (Convert.ToDouble(newobjs[0]) != 3.1) // was (Double) [alan] { Errln("newobjs[0] = " + newobjs[0]); } }
public void Test4120552() { MessageFormat mf = new MessageFormat("pattern"); String[] texts = new string[] { "pattern", "pat", "1234" }; Logln("pattern: \"" + mf.ToPattern() + "\""); for (int i = 0; i < texts.Length; i++) { ParsePosition pp = new ParsePosition(0); Object[] objs = mf.Parse(texts[i], pp); Log(" text for parsing: \"" + texts[i] + "\""); if (objs == null) { Logln(" (incorrectly formatted string)"); if (pp.ErrorIndex == -1) { Errln("Incorrect error index: " + pp.ErrorIndex); } } else { Logln(" (correctly formatted string)"); } } }
public void Test4031438() { String pattern1 = "Impossible {1} has occurred -- status code is {0} and message is {2}."; String pattern2 = "Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'."; MessageFormat messageFormatter = new MessageFormat(""); try { Logln("Apply with pattern : " + pattern1); messageFormatter.ApplyPattern(pattern1); Object[] paramArray = { (int)7 }; String tempBuffer = messageFormatter.Format(paramArray); if (!tempBuffer.Equals("Impossible {1} has occurred -- status code is 7 and message is {2}.")) { Errln("Tests arguments < substitution failed"); } Logln("Formatted with 7 : " + tempBuffer); ParsePosition status = new ParsePosition(0); Object[] objs = messageFormatter.Parse(tempBuffer, status); if (objs[paramArray.Length] != null) { Errln("Parse failed with more than expected arguments"); } for (int i = 0; i < objs.Length; i++) { if (objs[i] != null && !objs[i].ToString().Equals(paramArray[i].ToString())) { Errln("Parse failed on object " + objs[i] + " at index : " + i); } } tempBuffer = messageFormatter.Format((object)null); if (!tempBuffer.Equals("Impossible {1} has occurred -- status code is {0} and message is {2}.")) { Errln("Tests with no arguments failed"); } Logln("Formatted with null : " + tempBuffer); Logln("Apply with pattern : " + pattern2); messageFormatter.ApplyPattern(pattern2); tempBuffer = messageFormatter.Format(paramArray); if (!tempBuffer.Equals("Double ' Quotes 7 test and quoted {1} test plus 'other {2} stuff'.")) { Errln("quote format test (w/ params) failed."); } Logln("Formatted with params : " + tempBuffer); tempBuffer = messageFormatter.Format((object)null); if (!tempBuffer.Equals("Double ' Quotes {0} test and quoted {1} test plus 'other {2} stuff'.")) { Errln("quote format test (w/ null) failed."); } Logln("Formatted with null : " + tempBuffer); Logln("toPattern : " + messageFormatter.ToPattern()); } catch (Exception foo) { Warnln("Exception when formatting in bug 4031438. " + foo.Message); } }
public void Test4052223() { ParsePosition pos = new ParsePosition(0); if (pos.ErrorIndex != -1) { Errln("ParsePosition.getErrorIndex initialization failed."); } MessageFormat fmt = new MessageFormat("There are {0} apples growing on the {1} tree."); String str = "There is one apple growing on the peach tree."; Object[] objs = fmt.Parse(str, pos); Logln("unparsable string , should fail at " + pos.ErrorIndex); if (pos.ErrorIndex == -1) { Errln("Bug 4052223 failed : parsing string " + str); } pos.ErrorIndex = (4); if (pos.ErrorIndex != 4) { Errln("setErrorIndex failed, got " + pos.ErrorIndex + " instead of 4"); } if (objs != null) { Errln("objs should be null"); } ChoiceFormat f = new ChoiceFormat( "-1#are negative|0#are no or fraction|1#is one|1.0<is 1+|2#are two|2<are more than 2."); pos.Index = (0); pos.ErrorIndex = (-1); /*Number*/ object obj = f.Parse("are negative", pos); if (pos.ErrorIndex != -1 && Convert.ToDouble(obj) == -1.0) { Errln("Parse with \"are negative\" failed, at " + pos.ErrorIndex); } pos.Index = (0); pos.ErrorIndex = (-1); obj = f.Parse("are no or fraction ", pos); if (pos.ErrorIndex != -1 && Convert.ToDouble(obj) == 0.0) { Errln("Parse with \"are no or fraction\" failed, at " + pos.ErrorIndex); } pos.Index = (0); pos.ErrorIndex = (-1); obj = f.Parse("go postal", pos); if (pos.ErrorIndex == -1 && !double.IsNaN(Convert.ToDouble(obj))) { Errln("Parse with \"go postal\" failed, at " + pos.ErrorIndex); } }
public void Test4116444() { String[] patterns = { "", "one", "{0,date,short}" }; MessageFormat mf = new MessageFormat(""); for (int i = 0; i < patterns.Length; i++) { String pattern = patterns[i]; mf.ApplyPattern(pattern); try { Object[] array = mf.Parse(null, new ParsePosition(0)); Logln("pattern: \"" + pattern + "\""); Log(" parsedObjects: "); if (array != null) { Log("{"); for (int j = 0; j < array.Length; j++) { if (array[j] != null) { Err("\"" + array[j].ToString() + "\""); } else { Log("null"); } if (j < array.Length - 1) { Log(","); } } Log("}"); } else { Log("null"); } Logln(""); } catch (Exception e) { Errln("pattern: \"" + pattern + "\""); Errln(" Exception: " + e.Message); } } }
public void Test4118592() { MessageFormat mf = new MessageFormat(""); String pattern = "{0,choice,1#YES|2#NO}"; String prefix = ""; for (int i = 0; i < 5; i++) { String formatted = prefix + "YES"; mf.ApplyPattern(prefix + pattern); prefix += "x"; Object[] objs = mf.Parse(formatted, new ParsePosition(0)); Logln(i + ". pattern :\"" + mf.ToPattern() + "\""); Log(" \"" + formatted + "\" parsed as "); if (objs == null) { Logln(" null"); } else { Logln(" " + objs[0]); } } }