コード例 #1
0
        public void Setting_WithNoValueInResolver_Throws()
        {
            Attr2 a2 = new Attr2(string.Empty, string.Empty)
            {
                ResolvedSetting = "appsetting"
            };

            Assert.Throws <InvalidOperationException>(() => new AttributeCloner <Attr2>(a2, EmptyContract, null));
        }
コード例 #2
0
        public void Setting_Null()
        {
            Attr2 a2 = new Attr2(string.Empty, string.Empty);

            var cloner = new AttributeCloner <Attr2>(a2, emptyContract);

            Attr2 a2Clone = cloner.ResolveFromBindingData(GetCtx(null));

            Assert.Null(a2Clone.ResolvedSetting);
        }
コード例 #3
0
        public void Setting_WithNoValueInResolver_ThrowsIfNoDefault()
        {
            Attr2 a2 = new Attr2(string.Empty, string.Empty)
            {
                ResolvedSetting = "appsetting"
            };
            var exc = Assert.Throws <InvalidOperationException>(() => new AttributeCloner <Attr2>(a2, emptyContract));

            Assert.Equal($"Unable to resolve app setting for property 'Attr2.ResolvedSetting'. Make sure the app setting exists and has a valid value.", exc.Message);
        }
コード例 #4
0
        public async Task Setting_Null()
        {
            Attr2 a2 = new Attr2(string.Empty, string.Empty);

            var cloner = new AttributeCloner <Attr2>(a2, EmptyContract, null);

            Attr2 a2Clone = await cloner.ResolveFromBindingDataAsync(GetCtx(null));

            Assert.Null(a2Clone.ResolvedSetting);
        }
コード例 #5
0
        public void Setting_WithNoValueInResolver_UsesDefault()
        {
            Attr2 a2 = new Attr2(string.Empty, string.Empty)
            {
                ResolvedSetting = "appsetting"
            };
            var nameResolver = new FakeNameResolver().Add("appsetting", "ABC").Add("default", "default");
            var cloner       = new AttributeCloner <Attr2>(a2, emptyContract, nameResolver);

            var a2Cloned = cloner.GetNameResolvedAttribute();

            Assert.Equal("default", a2Cloned.DefaultSetting);
        }
コード例 #6
0
        public void Setting()
        {
            Attr2 a2 = new Attr2(string.Empty, string.Empty)
            {
                ResolvedSetting = "appsetting"
            };

            var nameResolver = new FakeNameResolver().Add("appsetting", "ABC");
            var cloner       = new AttributeCloner <Attr2>(a2, EmptyContract, nameResolver);

            var a2Cloned = cloner.GetNameResolvedAttribute();

            Assert.Equal("ABC", a2Cloned.ResolvedSetting);
        }
コード例 #7
0
        public void TryAutoResolveValue_UnresolvedValue_ThrowsExpectedException()
        {
            var resolver  = new FakeNameResolver();
            var attribute = new Attr2(string.Empty, string.Empty)
            {
                ResolvedSetting = "MySetting"
            };
            var    prop          = attribute.GetType().GetProperty("ResolvedSetting");
            string resolvedValue = null;

            var ex = Assert.Throws <InvalidOperationException>(() => AttributeCloner <Attr2> .TryAutoResolveValue(attribute, prop, resolver, out resolvedValue));

            Assert.Equal("Unable to resolve value for property 'Attr2.ResolvedSetting'.", ex.Message);
        }
コード例 #8
0
        public void TryAutoResolveValue_UnresolvedValue_ThrowsExpectedException()
        {
            var resolver  = new FakeNameResolver();
            var attribute = new Attr2(string.Empty, string.Empty)
            {
                ResolvedSetting = "MySetting"
            };
            var    prop          = attribute.GetType().GetProperty("ResolvedSetting");
            var    attr          = prop.GetCustomAttribute <AppSettingAttribute>();
            string resolvedValue = "MySetting";

            var ex = Assert.Throws <InvalidOperationException>(() => AttributeCloner <Attr2> .GetAppSettingResolver(resolvedValue, attr, resolver, prop, skipValidation));

            Assert.Contains("Unable to resolve app setting for property 'Attr2.ResolvedSetting'.", ex.Message);
        }
コード例 #9
0
        public void Setting_WithNoValueInResolver_UsesDefault()
        {
            Attr2 a2 = new Attr2(string.Empty, string.Empty)
            {
                ResolvedSetting = "appsetting"
            };
            var nameResolver = new FakeNameResolver();
            var config       = TestHelpers.CreateInMemoryCollection()
                               .AddSetting("appsetting", "ABC")
                               .AddSetting("default", "default")
                               .BuildConfiguration();

            var cloner = new AttributeCloner <Attr2>(a2, emptyContract, config, nameResolver);

            var a2Cloned = cloner.GetNameResolvedAttribute();

            Assert.Equal("default", a2Cloned.DefaultSetting);
        }
コード例 #10
0
        public void Setting()
        {
            Attr2 a2 = new Attr2(string.Empty, string.Empty)
            {
                ResolvedSetting = "appsetting"
            };

            var nameResolver = new FakeNameResolver();

            var config = TestHelpers.CreateInMemoryCollection()
                         .AddSetting("appsetting", "ABC")
                         .AddSetting("connectionStrings:default", "fromConnStr")
                         .BuildConfiguration();

            var cloner = new AttributeCloner <Attr2>(a2, emptyContract, config, nameResolver);

            var a2Cloned = cloner.GetNameResolvedAttribute();

            Assert.Equal("ABC", a2Cloned.ResolvedSetting);
            Assert.Equal("fromConnStr", a2Cloned.Connection);
        }
コード例 #11
0
        public async Task InvokeStringMultipleResolvedProperties()
        {
            Attr2 attr = new Attr2("{p2}", "constant")
            {
                ResolvedProp1 = "{p1}"
            };

            var cloner = new AttributeCloner <Attr2>(attr, GetBindingContract("p1", "p2"));

            Attr2 attrResolved = cloner.ResolveFromBindings(new Dictionary <string, object> {
                { "p1", "v1" }, { "p2", "v2" }
            });

            Assert.Equal("v1", attrResolved.ResolvedProp1);
            Assert.Equal("v2", attrResolved.ResolvedProp2);
            Assert.Equal(attr.ConstantProp, attrResolved.ConstantProp);

            var invokeString = cloner.GetInvokeString(attrResolved);
            var attr2        = await cloner.ResolveFromInvokeString(invokeString);

            Assert.Equal(attrResolved.ResolvedProp1, attr2.ResolvedProp1);
            Assert.Equal(attrResolved.ResolvedProp2, attr2.ResolvedProp2);
            Assert.Equal(attrResolved.ConstantProp, attr2.ConstantProp);
        }
コード例 #12
0
        public void Fail_MalformedPath_CtorArg()
        {
            var attr = new Attr2("%bad", "constant");

            Assert.Throws <InvalidOperationException>(() => new AttributeCloner <Attr2>(attr, EmptyContract));
        }
コード例 #13
0
        public void Fail_MalformedPath_CtorArg()
        {
            var attr = new Attr2("%bad", "constant");

            Assert.Throws<InvalidOperationException>(() => new AttributeCloner<Attr2>(attr, EmptyContract));
        }
コード例 #14
0
        public async Task Setting_Null()
        {
            Attr2 a2 = new Attr2(string.Empty, string.Empty);

            var cloner = new AttributeCloner<Attr2>(a2, EmptyContract, null);

            Attr2 a2Clone = await cloner.ResolveFromBindingDataAsync(GetCtx(null));

            Assert.Null(a2Clone.ResolvedSetting);
        }
コード例 #15
0
 public void Setting_WithNoValueInResolver_Throws()
 {
     Attr2 a2 = new Attr2(string.Empty, string.Empty) { ResolvedSetting = "appsetting" };
     Assert.Throws<InvalidOperationException>(() => new AttributeCloner<Attr2>(a2, EmptyContract, null));
 }
コード例 #16
0
        public void Setting()
        {
            Attr2 a2 = new Attr2(string.Empty, string.Empty) { ResolvedSetting = "appsetting" };

            var nameResolver = new FakeNameResolver().Add("appsetting", "ABC");
            var cloner = new AttributeCloner<Attr2>(a2, EmptyContract, nameResolver);

            var a2Cloned = cloner.GetNameResolvedAttribute();
            Assert.Equal("ABC", a2Cloned.ResolvedSetting);
        }
コード例 #17
0
        public async Task InvokeStringMultipleResolvedProperties()
        {
            Attr2 attr = new Attr2("{p2}", "constant") { ResolvedProp1 = "{p1}" };

            var cloner = new AttributeCloner<Attr2>(attr, GetBindingContract("p1", "p2"));

            Attr2 attrResolved = cloner.ResolveFromBindings(new Dictionary<string, object> {
                { "p1", "v1" }, { "p2", "v2" }});

            Assert.Equal("v1", attrResolved.ResolvedProp1);
            Assert.Equal("v2", attrResolved.ResolvedProp2);
            Assert.Equal(attr.ConstantProp, attrResolved.ConstantProp);

            var invokeString = cloner.GetInvokeString(attrResolved);
            var attr2 = await cloner.ResolveFromInvokeStringAsync(invokeString);

            Assert.Equal(attrResolved.ResolvedProp1, attr2.ResolvedProp1);
            Assert.Equal(attrResolved.ResolvedProp2, attr2.ResolvedProp2);
            Assert.Equal(attrResolved.ConstantProp, attr2.ConstantProp);
        }