Beispiel #1
0
        public void BindCanReadComplexProperties()
        {
            var dic = new Dictionary <string, string>
            {
                { "Integer", "-2" },
                { "Boolean", "TRUe" },
                { "Nested:Integer", "11" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var instance = new ComplexOptions();

            config.Bind(instance);

            Assert.Multiple(() =>
            {
                Assert.That(instance.Boolean, Is.True);
                Assert.That(-2, Is.EqualTo(instance.Integer));
                Assert.That(11, Is.EqualTo(instance.Nested.Integer));
            });
        }
Beispiel #2
0
        public void GetCanReadInheritedProperties()
        {
            var dic = new Dictionary <string, string>
            {
                { "Integer", "-2" },
                { "Boolean", "TRUe" },
                { "Nested:Integer", "11" },
                { "Virtual", "Sup" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = new DerivedOptions();

            config.Bind(options);

            Assert.Multiple(() =>
            {
                Assert.That(options.Boolean, Is.True);
                Assert.That(-2, Is.EqualTo(options.Integer));
                Assert.That(11, Is.EqualTo(options.Nested.Integer));
                Assert.That("Derived:Sup", Is.EqualTo(options.Virtual));
            });
        }
Beispiel #3
0
        public void ExceptionOnFailedBindingIncludesPath()
        {
            const string IncorrectValue = "Invalid data";
            const string ConfigKey      = "Nested:Value";

            var dic = new Dictionary <string, string>
            {
                { ConfigKey, IncorrectValue }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = new OptionsWithNesting();

            Assert.Multiple(() =>
            {
                var exception = Assert.Throws <InvalidOperationException>(
                    () => config.Bind(options));

                Assert.That(exception.Message, Does.Contain(ConfigKey));
            });
        }
Beispiel #4
0
        public void ConsistentExceptionOnFailedBinding(Type type)
        {
            const string IncorrectValue = "Invalid data";
            const string ConfigKey      = "Value";
            var          dic            = new Dictionary <string, string>
            {
                { ConfigKey, IncorrectValue }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

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

            var exception = Assert.Throws <InvalidOperationException>(
                () => config.Bind(options));

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

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

            Assert.Multiple(() =>
            {
                Assert.That(exception.InnerException, Is.Not.Null);
                Assert.That(getException.InnerException, Is.Not.Null);
            });
        }
Beispiel #5
0
        public void CanReadAllSupportedTypes(string value, Type type)
        {
            var dic = new Dictionary <string, string>
            {
                { "Value", value }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

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

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

            Assert.Multiple(() =>
            {
                Assert.That(expectedValue, Is.EqualTo(optionsValue));
                Assert.That(expectedValue, Is.EqualTo(getValue));
                Assert.That(expectedValue, Is.EqualTo(getValueValue));
            });
        }
Beispiel #6
0
        public void BinderIgnoresIndexerProperties()
        {
            var configManager = new ConfigurationManagerFixture()
                                .Build();

            var config = new AppConfiguration(configManager);

            config.Bind(new List <string>());
        }
Beispiel #7
0
        public void ExceptionIncludesKeyOfFailedBinding()
        {
            var dic = new Dictionary <string, string>
            {
                { "NestedOptionsProperty:NestedOptions2Property:ISomeInterfaceProperty:subkey", "x" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.That(() => config.Bind(new TestOptions()), Throws.InvalidOperationException);
        }
Beispiel #8
0
        public void ExceptionWhenTryingToBindClassWithoutParameterlessConstructor()
        {
            var dic = new Dictionary <string, string>
            {
                { "ClassWithoutPublicConstructorProperty:Subkey", "x" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.That(() => config.Bind(new TestOptions()), Throws.InvalidOperationException);
        }
Beispiel #9
0
        public void ExceptionWhenTryingToBindToInterface()
        {
            var dic = new Dictionary <string, string>
            {
                { "ISomeInterfaceProperty:Subkey", "x" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.That(() => config.Bind(new TestOptions()), Throws.InvalidOperationException);
        }
Beispiel #10
0
        public void ExceptionWhenTryingToBindToTypeThrowsWhenActivated()
        {
            var dic = new Dictionary <string, string>
            {
                { "ThrowsWhenActivatedProperty:subkey", "x" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.Multiple(() =>
            {
                var exception = Assert.Throws <InvalidOperationException>(() => config.Bind(new TestOptions()));
                Assert.That(exception.InnerException, Is.Not.Null);
            });
        }
Beispiel #11
0
        public void NonPublicModeBindStillIgnoresReadonly(string property)
        {
            var dic = new Dictionary <string, string>
            {
                { property, "stuff" },
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = new ComplexOptions();

            config.Bind(options, o => o.BindNonPublicProperties = true);

            Assert.That(options.GetType().GetTypeInfo().GetDeclaredProperty(property).GetValue(options), Is.Null);
        }
Beispiel #12
0
        public void BindCanSetNonPublicWhenSet(string property)
        {
            var dic = new Dictionary <string, string>
            {
                { property, "stuff" },
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = new ComplexOptions();

            config.Bind(options, o => o.BindNonPublicProperties = true);

            Assert.That("stuff", Is.EqualTo(options.GetType().GetTypeInfo().GetDeclaredProperty(property).GetValue(options)));
        }
Beispiel #13
0
        public void BindIgnoresTests(string property)
        {
            var dic = new Dictionary <string, string>
            {
                { property, "stuff" },
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = new ComplexOptions();

            config.Bind(options);

            Assert.That(options.GetType().GetTypeInfo().GetDeclaredProperty(property).GetValue(options), Is.Null);
        }
Beispiel #14
0
        public void BindCanReadStaticProperty()
        {
            var dic = new Dictionary <string, string>
            {
                { "StaticProperty", "other stuff" },
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var instance = new ComplexOptions();

            config.Bind(instance);

            Assert.That("other stuff", Is.EqualTo(ComplexOptions.StaticProperty));
        }
Beispiel #15
0
        public void CanBindToObjectProperty()
        {
            var dic = new Dictionary <string, string>
            {
                { "Object", "whatever" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = new ComplexOptions();

            config.Bind(options);

            Assert.That("whatever", Is.EqualTo(options.Object));
        }
Beispiel #16
0
        public void Bind_WithTenantSettingInCategory_ReturnsTenantValue()
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting("Category1:Key1", "Value1")
                                .WithAppSetting("Category1:Key2", 2)
                                .WithAppSetting("Tenant:Category1:Key2", 3)
                                .Build();

            var config = new AppConfiguration(configManager);

            var settings = new TestSettings();

            config.Bind("Category1", "Tenant", settings);

            Assert.Multiple(() =>
            {
                Assert.That(settings.Key1, Is.EqualTo("Value1"));

                Assert.That(settings.Key2, Is.EqualTo(3));
            });
        }