public void CanReadAllSupportedTypes(string value, Type type)
        {
            // arrange
            var dic = new Dictionary <string, string>
            {
                { "Value", value }
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(dic);
            var config = configurationBuilder.Build();

            var optionsType   = typeof(GenericOptions <>).MakeGenericType(type);
            var options       = Activator.CreateInstance(optionsType);
            var expectedValue = TypeDescriptor.GetConverter(type).ConvertFromInvariantString(value);

            // act
            config.HzBind(options);
            var optionsValue  = options.GetType().GetProperty("Value").GetValue(options);
            var getValueValue = ConfigurationBinder.GetValue(config, type, "Value");
            var getValue      = ConfigurationBinder.Get(config.GetSection("Value"), type);

            // assert
            Assert.AreEqual(expectedValue, optionsValue);
            Assert.AreEqual(expectedValue, getValue);
            Assert.AreEqual(expectedValue, getValueValue);
        }
        public void CanBindIConfigurationSectionWithDerivedOptionsSection()
        {
            var dic = new Dictionary <string, string>
            {
                { "Section:Integer", "-2" },
                { "Section:Boolean", "TRUe" },
                { "Section:Nested:Integer", "11" },
                { "Section:Virtual", "Sup" },
                { "Section:DerivedSection:Nested:Integer", "11" },
                { "Section:DerivedSection:Virtual", "Sup" }
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(dic);
            var config = configurationBuilder.Build();

            var options = ConfigurationBinder.Get <ConfigurationInterfaceOptions>(config);

            var childOptions = ConfigurationBinder.Get <DerivedOptionsWithIConfigurationSection>(options.Section);

            var childDerivedOptions = ConfigurationBinder.Get <DerivedOptions>(childOptions.DerivedSection);

            Assert.True(childOptions.Boolean);
            Assert.AreEqual(-2, childOptions.Integer);
            Assert.AreEqual(11, childOptions.Nested.Integer);
            Assert.AreEqual("Derived:Sup", childOptions.Virtual);
            Assert.AreEqual(11, childDerivedOptions.Nested.Integer);
            Assert.AreEqual("Derived:Sup", childDerivedOptions.Virtual);

            Assert.AreEqual("Section", options.Section.Key);
            Assert.AreEqual("Section", options.Section.Path);
            Assert.AreEqual("DerivedSection", childOptions.DerivedSection.Key);
            Assert.AreEqual("Section:DerivedSection", childOptions.DerivedSection.Path);
            Assert.Null(options.Section.Value);
        }
        public void EmptyStringIsNullable()
        {
            var dic = new Dictionary <string, string>
            {
                { "empty", "" },
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(dic);
            var config = configurationBuilder.Build();

            Assert.Null(ConfigurationBinder.GetValue <bool?>(config, "empty"));
            Assert.Null(ConfigurationBinder.GetValue <int?>(config, "empty"));
        }
        public void NonPublicModeGetStillIgnoresReadonly(string property)
        {
            var dic = new Dictionary <string, string>
            {
                { property, "stuff" },
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(dic);
            var config = configurationBuilder.Build();

            var options = ConfigurationBinder.Get <ComplexOptions>(config, o => o.BindNonPublicProperties = true);

            Assert.Null(options.GetType().GetTypeInfo().GetDeclaredProperty(property).GetValue(options));
        }
        public void GetCanSetNonPublicWhenSet(string property)
        {
            var dic = new Dictionary <string, string>
            {
                { property, "stuff" },
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(dic);
            var config = configurationBuilder.Build();

            var options = ConfigurationBinder.Get <ComplexOptions>(config, o => o.BindNonPublicProperties = true);

            Assert.AreEqual("stuff", options.GetType().GetTypeInfo().GetDeclaredProperty(property).GetValue(options));
        }
        public void GetIgnoresTests(string property)
        {
            var dic = new Dictionary <string, string>
            {
                { property, "stuff" },
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(dic);
            var config = configurationBuilder.Build();

            var options = ConfigurationBinder.Get <ComplexOptions>(config);

            Assert.Null(options.GetType().GetTypeInfo().GetDeclaredProperty(property).GetValue(options));
        }
        public void GetUri()
        {
            var dic = new Dictionary <string, string>
            {
                { "AnUri", "http://www.bing.com" }
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(dic);
            var config = configurationBuilder.Build();

            var uri = ConfigurationBinder.GetValue <Uri>(config, "AnUri");

            Assert.AreEqual("http://www.bing.com", uri.OriginalString);
        }
        public void GetScalarNullable()
        {
            var dic = new Dictionary <string, string>
            {
                { "Integer", "-2" },
                { "Boolean", "TRUe" },
                { "Nested:Integer", "11" }
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(dic);
            var config = configurationBuilder.Build();

            Assert.True(ConfigurationBinder.GetValue <bool?>(config, "Boolean"));
            Assert.AreEqual(-2, ConfigurationBinder.GetValue <int?>(config, "Integer"));
            Assert.AreEqual(11, ConfigurationBinder.GetValue <int?>(config, "Nested:Integer"));
        }
        public void CanGetComplexOptionsWhichHasAlsoHasValue()
        {
            var dic = new Dictionary <string, string>
            {
                { "obj", "whut" },
                { "obj:Integer", "-2" },
                { "obj:Boolean", "TRUe" },
                { "obj:Nested:Integer", "11" }
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(dic);
            var config = configurationBuilder.Build();

            var options = ConfigurationBinder.Get <ComplexOptions>(config.GetSection("obj"));

            Assert.NotNull(options);
            Assert.True(options.Boolean);
            Assert.AreEqual(-2, options.Integer);
            Assert.AreEqual(11, options.Nested.Integer);
        }
        public void GetDefaultsWhenDataDoesNotExist()
        {
            var dic = new Dictionary <string, string>
            {
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(dic);
            var config = configurationBuilder.Build();

            Assert.False(ConfigurationBinder.GetValue <bool>(config, "Boolean"));
            Assert.AreEqual(0, ConfigurationBinder.GetValue <int>(config, "Integer"));
            Assert.AreEqual(0, ConfigurationBinder.GetValue <int>(config, "Nested:Integer"));
            Assert.Null(ConfigurationBinder.GetValue <ComplexOptions>(config, "Object"));
            Assert.True(ConfigurationBinder.GetValue(config, "Boolean", true));
            Assert.AreEqual(3, ConfigurationBinder.GetValue(config, "Integer", 3));
            Assert.AreEqual(1, ConfigurationBinder.GetValue(config, "Nested:Integer", 1));
            var foo = new ComplexOptions();

            Assert.AreSame(ConfigurationBinder.GetValue(config, "Object", foo), foo);
        }
        public void ConsistentExceptionOnFailedBinding(Type type)
        {
            // arrange
            const string IncorrectValue = "Invalid data";
            const string ConfigKey      = "Value";
            var          dic            = new Dictionary <string, string>
            {
                { ConfigKey, IncorrectValue }
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(dic);
            var config = configurationBuilder.Build();

            var optionsType = typeof(GenericOptions <>).MakeGenericType(type);
            var options     = Activator.CreateInstance(optionsType);

            // act
            var exception = Assert.Throws <InvalidOperationException>(
                () => config.HzBind(options));

            var getValueException = Assert.Throws <InvalidOperationException>(
                () => ConfigurationBinder.GetValue(config, type, "Value"));

            var getException = Assert.Throws <InvalidOperationException>(
                () => ConfigurationBinder.Get(config.GetSection("Value"), type));

            // assert
            Assert.NotNull(exception.InnerException);
            Assert.NotNull(getException.InnerException);
            //Assert.AreEqual(
            //    Resources.FormatError_FailedBinding(ConfigKey, type),
            //    exception.Message);
            //Assert.AreEqual(
            //    Resources.FormatError_FailedBinding(ConfigKey, type),
            //    getException.Message);
            //Assert.AreEqual(
            //    Resources.FormatError_FailedBinding(ConfigKey, type),
            //    getValueException.Message);
        }
        public void GetNullValue()
        {
            var dic = new Dictionary <string, string>
            {
                { "Integer", null },
                { "Boolean", null },
                { "Nested:Integer", null },
                { "Object", null }
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(dic);
            var config = configurationBuilder.Build();

            Assert.False(ConfigurationBinder.GetValue <bool>(config, "Boolean"));
            Assert.AreEqual(0, ConfigurationBinder.GetValue <int>(config, "Integer"));
            Assert.AreEqual(0, ConfigurationBinder.GetValue <int>(config, "Nested:Integer"));
            Assert.Null(ConfigurationBinder.GetValue <ComplexOptions>(config, "Object"));
            Assert.False(ConfigurationBinder.Get <bool>(config.GetSection("Boolean")));
            Assert.AreEqual(0, ConfigurationBinder.Get <int>(config.GetSection("Integer")));
            Assert.AreEqual(0, ConfigurationBinder.Get <int>(config.GetSection("Nested:Integer")));
            Assert.Null(ConfigurationBinder.Get <ComplexOptions>(config.GetSection("Object")));
        }
예제 #13
0
 public void ArgumentExceptions()
 {
     Assert.Throws <ArgumentNullException>(() => ConfigurationBinder.Get(((IConfiguration)null), typeof(int), null));
     Assert.Throws <ArgumentNullException>(() => ConfigurationBinder.Get <int>(((IConfiguration)null), null));
     Assert.Throws <ArgumentNullException>(() => ConfigurationBinder.GetValue(((IConfiguration)null), typeof(int), "key", 42));
 }