コード例 #1
0
        public void ShouldBeIgnoredTests(string property)
        {
            var dic = new Dictionary <string, string>
            {
                { property, "stuff" },
            };
            var builder = new ConfigurationBuilder().AddInMemoryCollection(dic);
            var config  = builder.Build();
            var options = new ComplexOptions();

            ConfigurationBinder.Bind(config, options);
            Assert.Null(options.GetType().GetProperty(property).GetValue(options));
        }
コード例 #2
0
        public void CanReadStaticProperty()
        {
            var dic = new Dictionary <string, string>
            {
                { "StaticProperty", "stuff" },
            };
            var builder = new ConfigurationBuilder().AddInMemoryCollection(dic);
            var config  = builder.Build();
            var options = new ComplexOptions();

            ConfigurationBinder.Bind(config, options);
            Assert.Equal("stuff", ComplexOptions.StaticProperty);
        }
コード例 #3
0
        public void CanReadComplexProperties()
        {
            var dic = new Dictionary <string, string>
            {
                { "Integer", "-2" },
                { "Boolean", "TRUe" },
                { "Nested:Integer", "11" }
            };
            var builder = new ConfigurationBuilder().AddInMemoryCollection(dic);
            var config  = builder.Build();
            var options = new ComplexOptions();

            ConfigurationBinder.Bind(config, options);
            Assert.True(options.Boolean);
            Assert.Equal(-2, options.Integer);
            Assert.Equal(11, options.Nested.Integer);
        }
コード例 #4
0
        public void ValidationResultFailedOrSkipped()
        {
            var services = new ServiceCollection();

            services.AddOptions <ComplexOptions>("Name")
            .Validate(o => o.Boolean);

            var sp = services.BuildServiceProvider();

            var validations = sp.GetService <IEnumerable <IValidateOptions <ComplexOptions> > >();
            var options     = new ComplexOptions
            {
                Boolean = false,
            };

            foreach (var v in validations)
            {
                Assert.True(v.Validate(Options.DefaultName, options).Skipped);
                Assert.True(v.Validate("Name", options).Failed);
            }
        }
コード例 #5
0
        public void ValidationResultSuccessIfNameMatched()
        {
            var services = new ServiceCollection();

            services.AddOptions <ComplexOptions>()
            .Validate(o => o.Boolean)
            .Validate(o => o.Integer > 12);

            var sp = services.BuildServiceProvider();

            var validations = sp.GetService <IEnumerable <IValidateOptions <ComplexOptions> > >();
            var options     = new ComplexOptions
            {
                Boolean = true,
                Integer = 13
            };

            foreach (var v in validations)
            {
                Assert.True(v.Validate(Options.DefaultName, options).Succeeded);
                Assert.True(v.Validate("Something", options).Skipped);
            }
        }