示例#1
0
        public void TestConvertTo_Converter_Nullable()
        {
            // Primitive types.
            Assert.AreEqual(123, new XElement(ELEMENT_NAME, " 123 \n\t")
                .ConvertTo<int?>(v => Int32.Parse(v, CultureInfo.InvariantCulture)));
            Assert.AreEqual(-3.1415926, new XElement(ELEMENT_NAME, "-3.1415926 ")
                .ConvertTo<double?>(v => Double.Parse(v, CultureInfo.InvariantCulture)));
            Assert.AreEqual(true, new XElement(ELEMENT_NAME, "TRue")
                .ConvertTo<bool?>(v => Boolean.Parse(v)));

            // Enumerations.
            Assert.AreEqual(ActivationMode.SingleCall, new XElement(ELEMENT_NAME, "singlecall")
                .ConvertTo<ActivationMode?>(v => (ActivationMode)Enum.Parse(typeof(ActivationMode), v, true)));
            Assert.AreEqual(DayOfWeek.Saturday, new XElement(ELEMENT_NAME, "saturDay")
                .ConvertTo<DayOfWeek?>(v => (DayOfWeek)Enum.Parse(typeof(DayOfWeek), v, true)));

            // Value types.
            Assert.AreEqual(DATE_SAMPLE, new XElement(ELEMENT_NAME, DATE_STRING_SAMPLE)
                .ConvertTo<DateTime?>(v => DateTime.Parse(v, CultureInfo.InvariantCulture)));
            Assert.AreEqual(DATE_SAMPLE, new XElement(ELEMENT_NAME, DATE_SAMPLE)
                .ConvertTo<DateTime?>(v => DateTime.Parse(v, CultureInfo.InvariantCulture)));

            var colorConverter = new ColorConverter();
            Assert.AreEqual(Color.Red, new XElement(ELEMENT_NAME, "red")
                .ConvertTo(v => (Color?)colorConverter.ConvertFromString(null, CultureInfo.InvariantCulture, v)));
            var countryConverter = new CountryTypeConverter();
            Assert.AreEqual(Country.UnitedStates, new XElement(ELEMENT_NAME, "us")
                .ConvertTo(v => (Country?)countryConverter.ConvertFromString(null, CultureInfo.InvariantCulture, v)));
        }
示例#2
0
        public void TestConvertTo_Converter()
        {
            // Primitive types.
            Assert.AreEqual(123, new XElement(ELEMENT_NAME, " 123 \n\t")
                .ConvertTo(v => Int32.Parse(v, CultureInfo.InvariantCulture)));
            Assert.AreEqual(-3.1415926, new XElement(ELEMENT_NAME, "-3.1415926 ")
                .ConvertTo(v => Double.Parse(v, CultureInfo.InvariantCulture)));
            Assert.IsTrue(new XElement(ELEMENT_NAME, "TRue").ConvertTo(Boolean.Parse));

            // Enumerations.
            Assert.AreEqual(ActivationMode.SingleCall, new XElement(ELEMENT_NAME, "singlecall")
                .ConvertTo(v => (ActivationMode)Enum.Parse(typeof(ActivationMode), v, true)));
            Assert.AreEqual(DayOfWeek.Saturday, new XElement(ELEMENT_NAME, "saturDay")
                .ConvertTo(v => (DayOfWeek)Enum.Parse(typeof(DayOfWeek), v, true)));

            // Value types.
            Assert.AreEqual(DATE_SAMPLE, new XElement(ELEMENT_NAME, DATE_STRING_SAMPLE)
                .ConvertTo(v => DateTime.Parse(v, CultureInfo.InvariantCulture)));
            Assert.AreEqual(DATE_SAMPLE, new XElement(ELEMENT_NAME, DATE_SAMPLE)
                .ConvertTo(v => DateTime.Parse(v, CultureInfo.InvariantCulture)));

            var colorConverter = new ColorConverter();
            Assert.AreEqual(Color.Red, new XElement(ELEMENT_NAME, "red")
                .ConvertTo(v =>
                {
                    var convertFromString = colorConverter.ConvertFromString(null, CultureInfo.InvariantCulture, v);
                    return (convertFromString != null ? (Color)convertFromString : new Color());
                }));
            var countryConverter = new CountryTypeConverter();
            Assert.AreEqual(Country.UnitedStates, new XElement(ELEMENT_NAME, "us")
                .ConvertTo(v =>
                {
                    var fromString = countryConverter.ConvertFromString(null, CultureInfo.InvariantCulture, v);
                    return (fromString != null ? (Country)fromString : new Country());
                }));

            // Classes.
            var account = new XElement(ELEMENT_NAME, UnitTestHelper.ACCOUNT_NUMBER)
                .ConvertTo(v => (Account)new AccountTypeConverter().ConvertFromString(null, CultureInfo.InvariantCulture, v));
            Assert.IsNotNull(account);
            Assert.AreEqual(UnitTestHelper.ACCOUNT_NUMBER, account.Number);

            Assert.AreEqual(UnitTestHelper.SAMPLE_STRING, new XElement(ELEMENT_NAME, UnitTestHelper.SAMPLE_STRING).ConvertTo(v => v));
            Assert.AreEqual("123", new XElement(ELEMENT_NAME, 123).ConvertTo(v => v));

            var brush = new XElement(ELEMENT_NAME, "azure")
                .ConvertTo(v => (SolidColorBrush)new BrushConverter().ConvertFromString(null, CultureInfo.InvariantCulture, v));
            Assert.IsNotNull(brush);
            Assert.IsInstanceOf(typeof(SolidColorBrush), brush);
            Assert.AreEqual(Colors.Azure, brush.Color);

            Assert.AreEqual(new CultureInfo("en-US"), new XElement(ELEMENT_NAME, "en-US")
                .ConvertTo(v => (CultureInfo)new CultureInfoConverter().ConvertFromString(null, CultureInfo.InvariantCulture, v)));
        }