Exemple #1
0
        public void TestConvertStringToValue_DateTime()
        {
            var propertyDef =
                new PropertyDef(typeof(IRootElement).GetProperty(nameof(IRootElement.DateTimePropertyA), BindingFlags.Instance | BindingFlags.Public), new ConfigurationObjectSettings());

            Assert.Equal(new DateTime(2020, 12, 21, 12, 34, 56, DateTimeKind.Utc), propertyDef.ConvertStringToValue("PATH", "2020-12-21T12:34:56.0000000Z"));

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "NOT A DATE"); });
        }
Exemple #2
0
        public void TestConvertStringToValue_Single()
        {
            var propertyDef =
                new PropertyDef(typeof(IRootElement).GetProperty(nameof(IRootElement.SinglePropertyA), BindingFlags.Instance | BindingFlags.Public), new ConfigurationObjectSettings());

            Assert.Equal(123.456, (double)(float)propertyDef.ConvertStringToValue("PATH", "123.456"), 3);

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "NOT AN NUMBER"); });
        }
Exemple #3
0
        public void TestConvertStringToValue_Char()
        {
            var propertyDef =
                new PropertyDef(typeof(IRootElement).GetProperty(nameof(IRootElement.CharPropertyA), BindingFlags.Instance | BindingFlags.Public), new ConfigurationObjectSettings());

            Assert.Equal((char)'x', propertyDef.ConvertStringToValue("PATH", "x"));

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "XX"); });
        }
Exemple #4
0
        public void TestConvertStringToValue_TimeSpan()
        {
            var propertyDef =
                new PropertyDef(typeof(IRootElement).GetProperty(nameof(IRootElement.TimeSpanPropertyA), BindingFlags.Instance | BindingFlags.Public), new ConfigurationObjectSettings());

            Assert.Equal(TimeSpan.FromSeconds(1550), propertyDef.ConvertStringToValue("PATH", "00:25:50"));

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "NOT A DATE"); });
        }
Exemple #5
0
        public void TestConvertStringToValue_Boolean()
        {
            var propertyDef =
                new PropertyDef(typeof(IRootElement).GetProperty(nameof(IRootElement.BooleanPropertyA), BindingFlags.Instance | BindingFlags.Public), new ConfigurationObjectSettings());

            Assert.Equal(true, (bool)propertyDef.ConvertStringToValue("PATH", "True"));
            Assert.Equal(false, (bool)propertyDef.ConvertStringToValue("PATH", "False"));

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "NOT AN BOOLEAN"); });
        }
Exemple #6
0
        public void TestConvertStringToValue_Byte()
        {
            var propertyDef =
                new PropertyDef(typeof(IRootElement).GetProperty(nameof(IRootElement.BytePropertyA), BindingFlags.Instance | BindingFlags.Public), new ConfigurationObjectSettings());

            Assert.Equal((byte)123, propertyDef.ConvertStringToValue("PATH", "123"));

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "1234"); });

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "NOT AN INT"); });
        }
Exemple #7
0
        public void TestConvertStringToValue_Int64()
        {
            var propertyDef =
                new PropertyDef(typeof(IRootElement).GetProperty(nameof(IRootElement.Int64PropertyA), BindingFlags.Instance | BindingFlags.Public), new ConfigurationObjectSettings());

            Assert.Equal((long)123456789101112, propertyDef.ConvertStringToValue("PATH", "123456789101112"));

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "123456789012345678910"); });

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "NOT AN INT"); });
        }
Exemple #8
0
        public void TestConvertStringToValue_EnumFlags()
        {
            var propertyDef =
                new PropertyDef(typeof(IRootElement).GetProperty(nameof(IRootElement.EnumPropertyA), BindingFlags.Instance | BindingFlags.Public), new ConfigurationObjectSettings());

            Assert.Equal(BindingFlags.NonPublic, (BindingFlags)propertyDef.ConvertStringToValue("PATH", "NonPublic"));
            Assert.Equal(BindingFlags.CreateInstance, (BindingFlags)propertyDef.ConvertStringToValue("PATH", "CreateInstance"));
            Assert.Equal(BindingFlags.NonPublic, (BindingFlags)propertyDef.ConvertStringToValue("PATH", "32"));

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "NOT A VALUE"); });
        }
Exemple #9
0
        public void TestConvertStringToValue_Decimal()
        {
            var propertyDef =
                new PropertyDef(typeof(IRootElement).GetProperty(nameof(IRootElement.DecimalPropertyA), BindingFlags.Instance | BindingFlags.Public), new ConfigurationObjectSettings());

            Assert.Equal((decimal)123456789.101112, propertyDef.ConvertStringToValue("PATH", "123456789.101112"));

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "123456785436547659012345678910.1234557567865867890"); });

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "NOT AN INT"); });
        }
Exemple #10
0
        public void TestConvertStringToValue_EnumNonFlags()
        {
            var propertyDef =
                new PropertyDef(typeof(IRootElement).GetProperty(nameof(IRootElement.NonFlagsEnumPropertyA), BindingFlags.Instance | BindingFlags.Public), new ConfigurationObjectSettings());

            Assert.Equal(NonFlagsEnum.First, (NonFlagsEnum)propertyDef.ConvertStringToValue("PATH", "First"));
            Assert.Equal(NonFlagsEnum.Second, (NonFlagsEnum)propertyDef.ConvertStringToValue("PATH", "2"));
            Assert.Equal(NonFlagsEnum.Third, (NonFlagsEnum)propertyDef.ConvertStringToValue("PATH", "3"));

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "5"); });
            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", "NOT A VALUE"); });
        }
Exemple #11
0
        public void TestConvertStringToValue_String()
        {
            var propertyDef =
                new PropertyDef(typeof(IRootElement).GetProperty(nameof(IRootElement.StringPropertyA), BindingFlags.Instance | BindingFlags.Public), new ConfigurationObjectSettings());

            Assert.Equal("test-string", propertyDef.ConvertStringToValue("PATH", "test-string"));
        }
Exemple #12
0
        public void TestDefaults()
        {
            var propertyDef =
                new PropertyDef(typeof(IRootElement).GetProperty(nameof(IRootElement.SinglePropertyWithDefault), BindingFlags.Instance | BindingFlags.Public), new ConfigurationObjectSettings());

            Assert.Equal(123.456, (double)(float)propertyDef.ConvertStringToValue("PATH", null), 3);

            propertyDef = new PropertyDef(typeof(IRootElement).GetProperty(nameof(IRootElement.SinglePropertyNoDefault),
                                                                           BindingFlags.Instance | BindingFlags.Public), new ConfigurationObjectSettings());

            Assert.Throws <ConfigurationException>(() => { propertyDef.ConvertStringToValue("PATH", null); });
        }