/// <summary>
        ///   Should perform the conversion from the
        ///   configuration node specified to the type
        ///   specified.
        /// </summary>
        /// <param name="configuration" />
        /// <param name="targetType" />
        /// <returns />
        public override object PerformConversion(IConfiguration configuration, Type targetType)
        {
            var converter = new DefaultConverter(configuration.Children, Context);
              string name = converter.Get<string>("name");
              int age = converter.Get<int>("age");

              return new Person(name, age);
        }
Exemplo n.º 2
0
        public void DefaultConverterTest()
        {
            var c1 = new DefaultConverter <string>();

            c1.Set("hello").Should().Be("hello");
            c1.Get("hello").Should().Be("hello");
            c1.Set("").Should().Be("");
            c1.Get("").Should().Be("");
            c1.Get(null).Should().Be(null);
            c1.Set(null).Should().Be(null);
            var c3 = new DefaultConverter <double?>()
            {
                Culture = CultureInfo.InvariantCulture
            };

            c3.Set(1.7).Should().Be("1.7");
            c3.Get("1.7").Should().Be(1.7);
            c3.Get("1234567.15").Should().Be(1234567.15);
            c3.Set(1234567.15).Should().Be("1234567.15");
            c3.Set(c3.Get("1234567.15")).Should().Be("1234567.15");
            c3.Get(c3.Set(1234567.15)).Should().Be(1234567.15);
            c3.Set(null).Should().Be(null);
            c3.Get(null).Should().Be(null);
            c3.Culture = CultureInfo.GetCultureInfo("de-AT");
            c3.Set(1.7).Should().Be("1,7");
            c3.Get("1,7").Should().Be(1.7);
            var c4 = new DefaultConverter <ButtonType>();

            c4.Set(ButtonType.Button).Should().Be("Button");
            c4.Get("Button").Should().Be(ButtonType.Button);
            var c5   = new DefaultConverter <DateTime?>();
            var date = DateTime.Today;

            c5.Get(c5.Set(date)).Should().Be(date);
            c5.Set(null).Should().Be(null);
            c5.Get(null).Should().Be(null);
            var c6   = new DefaultConverter <TimeSpan>();
            var time = DateTime.Now.TimeOfDay;

            c6.Get(c6.Set(time)).Should().Be(time);
            var c7    = new DefaultConverter <TimeSpan?>();
            var time2 = DateTime.Now.TimeOfDay;

            c7.Get(c7.Set(time2)).Should().Be(time2);
            c7.Set(null).Should().Be(null);
            c7.Get(null).Should().Be(null);
            var c8 = new DefaultConverter <bool>();

            c8.Set(true).Should().Be("True");
            c8.Set(false).Should().Be("False");
            c8.Get("true").Should().Be(true);
            c8.Get("True").Should().Be(true);
            c8.Get("false").Should().Be(false);
            c8.Get("ON").Should().Be(true);
            c8.Get("off").Should().Be(false);
            c8.Get("").Should().Be(false);
            c8.Get("asdf").Should().Be(false);
            var c9 = new DefaultConverter <bool?>();

            c9.Set(true).Should().Be("True");
            c9.Get("true").Should().Be(true);
            c9.Set(false).Should().Be("False");
            c9.Get("false").Should().Be(false);
            c9.Set(null).Should().Be(null);
            c9.Get(null).Should().Be(null);
        }
Exemplo n.º 3
0
        public void DefaultConverterTestWithCustomFormat()
        {
            var float1 = new DefaultConverter <float>()
            {
                Format = "0.00"
            };

            float1.Culture = new CultureInfo("en-US");
            float1.Set(1.7f).Should().Be("1.70");
            float1.Set(1.773f).Should().Be("1.77");
            float1.Get("1.773").Should().Be(1.773f);
            float1.Get("1.77").Should().Be(1.77f);
            float1.Get("1.7").Should().Be(1.7f);
            float1.Culture = new CultureInfo("pt-BR");
            float1.Set(1.7f).Should().Be("1,70");
            float1.Set(1.773f).Should().Be("1,77");
            float1.Get("1,773").Should().Be(1.773f);
            float1.Get("1,77").Should().Be(1.77f);
            float1.Get("1,7").Should().Be(1.7f);

            var float2 = new DefaultConverter <float?>()
            {
                Format = "0.00"
            };

            float2.Culture = new CultureInfo("en-US");
            float2.Set(1.7f).Should().Be("1.70");
            float2.Set(1.773f).Should().Be("1.77");
            float2.Set(null).Should().Be(null);
            float2.Get("1.773").Should().Be(1.773f);
            float2.Get("1.77").Should().Be(1.77f);
            float2.Get("1.7").Should().Be(1.7f);
            float2.Get(null).Should().Be(null);
            float2.Culture = new CultureInfo("pt-BR");
            float2.Set(1.7f).Should().Be("1,70");
            float2.Set(1.773f).Should().Be("1,77");
            float2.Get("1,773").Should().Be(1.773f);
            float2.Get("1,77").Should().Be(1.77f);
            float2.Get("1,7").Should().Be(1.7f);

            var dbl1 = new DefaultConverter <double>()
            {
                Format = "0.00"
            };

            dbl1.Culture = new CultureInfo("en-US");
            dbl1.Set(1.7d).Should().Be("1.70");
            dbl1.Set(1.773d).Should().Be("1.77");
            dbl1.Get("1.773").Should().Be(1.773d);
            dbl1.Get("1.77").Should().Be(1.77d);
            dbl1.Get("1.7").Should().Be(1.7d);
            dbl1.Culture = new CultureInfo("pt-BR");
            dbl1.Set(1.7d).Should().Be("1,70");
            dbl1.Set(1.773d).Should().Be("1,77");
            dbl1.Get("1,773").Should().Be(1.773d);
            dbl1.Get("1,77").Should().Be(1.77d);
            dbl1.Get("1,7").Should().Be(1.7d);

            var dbl2 = new DefaultConverter <double?>()
            {
                Format = "0.00"
            };

            dbl2.Culture = new CultureInfo("en-US");
            dbl2.Set(1.7d).Should().Be("1.70");
            dbl2.Set(1.773d).Should().Be("1.77");
            dbl2.Set(null).Should().Be(null);
            dbl2.Get("1.773").Should().Be(1.773d);
            dbl2.Get("1.77").Should().Be(1.77d);
            dbl2.Get("1.7").Should().Be(1.7d);
            dbl2.Get(null).Should().Be(null);
            dbl2.Culture = new CultureInfo("pt-BR");
            dbl2.Set(1.7d).Should().Be("1,70");
            dbl2.Set(1.773d).Should().Be("1,77");
            dbl2.Get("1,773").Should().Be(1.773d);
            dbl2.Get("1,77").Should().Be(1.77d);
            dbl2.Get("1,7").Should().Be(1.7d);

            var dec1 = new DefaultConverter <decimal>()
            {
                Format = "0.00"
            };

            dec1.Culture = new CultureInfo("en-US");
            dec1.Set(1.7m).Should().Be("1.70");
            dec1.Set(1.773m).Should().Be("1.77");
            dec1.Get("1.773").Should().Be(1.773m);
            dec1.Get("1.77").Should().Be(1.77m);
            dec1.Get("1.7").Should().Be(1.7m);
            dec1.Culture = new CultureInfo("pt-BR");
            dec1.Set(1.7m).Should().Be("1,70");
            dec1.Set(1.773m).Should().Be("1,77");
            dec1.Get("1,773").Should().Be(1.773m);
            dec1.Get("1,77").Should().Be(1.77m);
            dec1.Get("1,7").Should().Be(1.7m);

            var dec2 = new DefaultConverter <decimal?>()
            {
                Format = "0.00"
            };

            dec2.Culture = new CultureInfo("en-US");
            dec2.Set(1.7m).Should().Be("1.70");
            dec2.Set(1.773m).Should().Be("1.77");
            dec2.Set(null).Should().Be(null);
            dec2.Get("1.773").Should().Be(1.773m);
            dec2.Get("1.77").Should().Be(1.77m);
            dec2.Get("1.7").Should().Be(1.7m);
            dec2.Get(null).Should().Be(null);
            dec2.Culture = new CultureInfo("pt-BR");
            dec2.Set(1.7m).Should().Be("1,70");
            dec2.Set(1.773m).Should().Be("1,77");
            dec2.Get("1,773").Should().Be(1.773m);
            dec2.Get("1,77").Should().Be(1.77m);
            dec2.Get("1,7").Should().Be(1.7m);

            var dt1 = new DefaultConverter <DateTime>()
            {
                Format = "MM/dd/yyyy"
            };

            dt1.Culture = new CultureInfo("en-US");
            dt1.Set(new DateTime(2020, 11, 03)).Should().Be("11/03/2020");
            dt1.Get("11/03/2020").Should().Be(new DateTime(2020, 11, 03));
            dt1.Culture = new CultureInfo("pt-BR");
            dt1.Format  = "dd/MM/yyyy";
            dt1.Set(new DateTime(2020, 11, 03)).Should().Be("03/11/2020");
            dt1.Get("03/11/2020").Should().Be(new DateTime(2020, 11, 03));

            var dt2 = new DefaultConverter <DateTime?>()
            {
                Format = "MM/dd/yyyy"
            };

            dt2.Culture = new CultureInfo("en-US");
            dt2.Set(new DateTime(2020, 11, 03)).Should().Be("11/03/2020");
            dt2.Set(null).Should().Be(null);
            dt2.Get("11/03/2020").Should().Be(new DateTime(2020, 11, 03));
            dt2.Get(null).Should().Be(null);
            dt2.Culture = new CultureInfo("pt-BR");
            dt2.Format  = "dd/MM/yyyy";
            dt2.Set(new DateTime(2020, 11, 03)).Should().Be("03/11/2020");
            dt2.Get("03/11/2020").Should().Be(new DateTime(2020, 11, 03));
        }
Exemplo n.º 4
0
        public void ErrorCheckingTest()
        {
            //format exception
            var dt1 = new DefaultConverter <DateTime>();

            dt1.Get("12/34/56").Should().Be(default);
Exemplo n.º 5
0
        public void DefaultIntegerConverterTest()
        {
            var c10 = new DefaultConverter <sbyte>();

            c10.Set(123).Should().Be("123");
            c10.Get("123").Should().Be(123);
            var c11 = new DefaultConverter <sbyte?>();

            c11.Set(123).Should().Be("123");
            c11.Get("123").Should().Be(123);
            c11.Set(null).Should().Be(null);
            c11.Get(null).Should().Be(null);
            var c12 = new DefaultConverter <byte>();

            c12.Set(234).Should().Be("234");
            c12.Get("234").Should().Be(234);
            var c13 = new DefaultConverter <byte?>();

            c13.Set(234).Should().Be("234");
            c13.Get("234").Should().Be(234);
            c13.Set(null).Should().Be(null);
            c13.Get(null).Should().Be(null);
            var c14 = new DefaultConverter <short>();

            c14.Set(1234).Should().Be("1234");
            c14.Get("1234").Should().Be(1234);
            var c15 = new DefaultConverter <short?>();

            c15.Set(1234).Should().Be("1234");
            c15.Get("1234").Should().Be(1234);
            c15.Set(null).Should().Be(null);
            c15.Get(null).Should().Be(null);
            var c16 = new DefaultConverter <ushort>();

            c16.Set(12345).Should().Be("12345");
            c16.Get("12345").Should().Be(12345);
            var c17 = new DefaultConverter <ushort?>();

            c17.Set(12345).Should().Be("12345");
            c17.Get("12345").Should().Be(12345);
            c17.Set(null).Should().Be(null);
            c17.Get(null).Should().Be(null);
            var c18 = new DefaultConverter <int>();

            c18.Set(34567).Should().Be("34567");
            c18.Get("34567").Should().Be(34567);
            var c19 = new DefaultConverter <int?>();

            c19.Set(34567).Should().Be("34567");
            c19.Get("34567").Should().Be(34567);
            c19.Set(null).Should().Be(null);
            c19.Get(null).Should().Be(null);
            var c20 = new DefaultConverter <uint>();

            c20.Set(45678).Should().Be("45678");
            c20.Get("45678").Should().Be(45678);
            var c21 = new DefaultConverter <uint?>();

            c21.Set(45678).Should().Be("45678");
            c21.Get("45678").Should().Be(45678);
            c21.Set(null).Should().Be(null);
            c21.Get(null).Should().Be(null);
            var c22 = new DefaultConverter <long>();

            c22.Set(456789).Should().Be("456789");
            c22.Get("456789").Should().Be(456789);
            var c23 = new DefaultConverter <long?>();

            c23.Set(456789).Should().Be("456789");
            c23.Get("456789").Should().Be(456789);
            c23.Set(null).Should().Be(null);
            c23.Get(null).Should().Be(null);
            var c24 = new DefaultConverter <ulong>();

            c24.Set(4567890).Should().Be("4567890");
            c24.Get("4567890").Should().Be(4567890);
            var c25 = new DefaultConverter <ulong?>();

            c25.Set(4567890).Should().Be("4567890");
            c25.Get("4567890").Should().Be(4567890);
            c25.Set(null).Should().Be(null);
            c25.Get(null).Should().Be(null);
        }