Esempio n. 1
0
        public void Test_setCurrencyLjava_util_Currency()
        {
            // Test for method void setCurrency(java.util.Currency)
            // a subclass that supports currency formatting
            Currency currA = Currency.GetInstance("ARS");

            IBM.ICU.Text.NumberFormat format = IBM.ICU.Text.NumberFormat.GetInstance(new ILOG.J2CsMapping.Util.Locale("hu", "HU"));
            format.SetCurrency(currA);
            NUnit.Framework.Assert.AreSame(currA, format.GetCurrency(), "Returned incorrect currency");

            // a subclass that doesn't support currency formatting
            ChoiceFormat cformat = new ChoiceFormat(
                "0#Less than one|1#one|1<Between one and two|2<Greater than two");

            try
            {
                //((IBM.ICU.Text.NumberFormat)cformat).SetCurrency(currA);
                //NUnit.Framework.Assert.Fail("Expected UnsupportedOperationException");
            }
            catch (NotSupportedException e)
            {
            }
        }
        public void Test_applyPatternLjava_lang_String()
        {
            // Test for method void
            // java.text.IBM.ICU.Text.MessageFormat.applyPattern(java.lang.String)
            IBM.ICU.Text.MessageFormat format = new IBM.ICU.Text.MessageFormat("test");
            format.ApplyPattern("xx {0}");
            NUnit.Framework.Assert.AreEqual("xx 46", format.FormatObject(new Object[] { ((int)(46)) }), "Invalid number");
            DateTime date     = DateTime.Now;
            String   result   = format.FormatObject(new Object[] { date });
            String   expected = "xx " + IBM.ICU.Text.DateFormat.GetInstance().Format(date);

            NUnit.Framework.Assert.IsTrue(result.Equals(expected), "Invalid date:\n" + result + "\n" + expected);
            format = new IBM.ICU.Text.MessageFormat("{0,date}{1,time}{2,number,integer}");
            format.ApplyPattern("nothing");
            NUnit.Framework.Assert.AreEqual("nothing", format.ToPattern(), "Found formats");

            format.ApplyPattern("{0}");
            NUnit.Framework.Assert.IsNull(format.GetFormats()[0], "Wrong format");
            NUnit.Framework.Assert.AreEqual("{0}", format.ToPattern(), "Wrong pattern");

            format.ApplyPattern("{0, \t\u001ftime }");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat.GetTimeInstance()), "Wrong time format");
            NUnit.Framework.Assert.AreEqual("{0,time}", format.ToPattern(), "Wrong time pattern");
            format.ApplyPattern("{0,Time, Short\n}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetTimeInstance(IBM.ICU.Text.DateFormat.SHORT)), "Wrong short time format");
            NUnit.Framework.Assert.AreEqual("{0,time,short}", format.ToPattern(), "Wrong short time pattern");
            format.ApplyPattern("{0,TIME,\nmedium  }");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetTimeInstance(IBM.ICU.Text.DateFormat.MEDIUM)), "Wrong medium time format");
            NUnit.Framework.Assert.AreEqual("{0,time}", format.ToPattern(), "Wrong medium time pattern");
            format.ApplyPattern("{0,time,LONG}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetTimeInstance(IBM.ICU.Text.DateFormat.LONG)), "Wrong long time format");
            NUnit.Framework.Assert.AreEqual("{0,time,long}", format.ToPattern(), "Wrong long time pattern");
            format.SetLocale(Locale.FRENCH);  // use French since English has the
            // same LONG and FULL time patterns
            format.ApplyPattern("{0,time, Full}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat.GetTimeInstance(
                                                                            IBM.ICU.Text.DateFormat.FULL, Locale.FRENCH)), "Wrong full time format");
            NUnit.Framework.Assert.AreEqual("{0,time,full}", format.ToPattern(), "Wrong full time pattern");
            format.SetLocale(Locale.GetDefault());

            format.ApplyPattern("{0, date}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat.GetDateInstance()), "Wrong date format");
            NUnit.Framework.Assert.AreEqual("{0,date}", format.ToPattern(), "Wrong date pattern");
            format.ApplyPattern("{0, date, short}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetDateInstance(IBM.ICU.Text.DateFormat.SHORT)), "Wrong short date format");
            NUnit.Framework.Assert.AreEqual("{0,date,short}", format.ToPattern(), "Wrong short date pattern");
            format.ApplyPattern("{0, date, medium}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetDateInstance(IBM.ICU.Text.DateFormat.MEDIUM)), "Wrong medium date format");
            NUnit.Framework.Assert.AreEqual("{0,date}", format.ToPattern(), "Wrong medium date pattern");
            format.ApplyPattern("{0, date, long}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetDateInstance(IBM.ICU.Text.DateFormat.LONG)), "Wrong long date format");
            NUnit.Framework.Assert.AreEqual("{0,date,long}", format.ToPattern(), "Wrong long date pattern");
            format.ApplyPattern("{0, date, full}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetDateInstance(IBM.ICU.Text.DateFormat.FULL)), "Wrong full date format");
            NUnit.Framework.Assert.AreEqual("{0,date,full}", format.ToPattern(), "Wrong full date pattern");

            format.ApplyPattern("{0, date, MMM d {hh:mm:ss}}");
            NUnit.Framework.Assert.AreEqual(" MMM d {hh:mm:ss}", ((IBM.ICU.Text.SimpleDateFormat)(format.GetFormats()[0])).ToPattern(), "Wrong time/date format");
            NUnit.Framework.Assert.AreEqual("{0,date, MMM d {hh:mm:ss}}", format.ToPattern(), "Wrong time/date pattern");

            format.ApplyPattern("{0, number}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.NumberFormat.GetNumberInstance()), "Wrong number format");
            NUnit.Framework.Assert.AreEqual("{0,number}", format.ToPattern(), "Wrong number pattern");
            format.ApplyPattern("{0, number, currency}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.NumberFormat
                                                                        .GetCurrencyInstance()), "Wrong currency number format");
            NUnit.Framework.Assert.AreEqual("{0,number,currency}", format.ToPattern(), "Wrong currency number pattern");
            format.ApplyPattern("{0, number, percent}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.NumberFormat.GetPercentInstance()), "Wrong percent number format");
            NUnit.Framework.Assert.AreEqual("{0,number,percent}", format.ToPattern(), "Wrong percent number pattern");
            format.ApplyPattern("{0, number, integer}");
            IBM.ICU.Text.NumberFormat nf = IBM.ICU.Text.NumberFormat.GetInstance();
            nf.SetMaximumFractionDigits(0);
            nf.SetParseIntegerOnly(true);
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(nf), "Wrong integer number format");
            NUnit.Framework.Assert.AreEqual("{0,number,integer}", format.ToPattern(), "Wrong integer number pattern");

            format.ApplyPattern("{0, number, {'#'}##0.0E0}");

            /*
             * TODO validate these assertions String actual =
             * ((DecimalFormat)(format.getFormats()[0])).toPattern();
             * assertEquals("Wrong pattern number format", "' {#}'##0.0E0", actual);
             * assertEquals("Wrong pattern number pattern",
             * "{0,number,' {#}'##0.0E0}", format.toPattern());
             */

            format.ApplyPattern("{0, choice,0#no|1#one|2#{1,number}}");
            NUnit.Framework.Assert.AreEqual("0.0#no|1.0#one|2.0#{1,number}", ((ILOG.J2CsMapping.Formatting.ChoiceFormat)format.GetFormats()[0]).ToPattern(), "Wrong choice format");
            NUnit.Framework.Assert.AreEqual("{0,choice,0.0#no|1.0#one|2.0#{1,number}}", format.ToPattern(), "Wrong choice pattern");
            NUnit.Framework.Assert.AreEqual("3.6", format.FormatObject(new Object[] { ((int)(2)), (float)(3.6d) }), "Wrong formatted choice");

            try
            {
                format.ApplyPattern("WRONG MESSAGE FORMAT {0,number,{}");
                NUnit.Framework.Assert.Fail("Expected IllegalArgumentException for invalid pattern");
            }
            catch (ArgumentException e)
            {
            }

            // Regression for HARMONY-65
            IBM.ICU.Text.MessageFormat mf = new IBM.ICU.Text.MessageFormat("{0,number,integer}");
            String badpattern             = "{0,number,#";

            try
            {
                mf.ApplyPattern(badpattern);
                NUnit.Framework.Assert.Fail("Assert 0: Failed to detect unmatched brackets.");
            }
            catch (ArgumentException e_0)
            {
                // expected
            }
        }
Esempio n. 3
0
 public void Test_getMaximumIntegerDigits()
 {
     IBM.ICU.Text.NumberFormat format = IBM.ICU.Text.NumberFormat.GetInstance();
     format.SetMaximumIntegerDigits(2);
     NUnit.Framework.Assert.AreEqual("23", format.Format(123), "Wrong result");
 }