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 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")));
        }
Ejemplo n.º 4
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));
 }