예제 #1
0
        public void ParserShouldParseMultiExpressionWithConverter()
        {
            const string targetPath = "Text";
            const string sourcePath1 = "SourceText1";
            const string sourcePath2 = "SourceText2";
            const string converterName = "TestConverter";
            const string binding = "Text $TestConverter(SourceText1, SourceText2)";
            var target = new object();
            var args = new object[] { "tset", 1 };
            var sourceModel = new BindingSourceModel { IntProperty = int.MaxValue };
            var bindingMock = new DataBindingMock
            {
                TargetAccessor = new BindingSourceAccessorMock
                {
                    Source = new BindingSourceMock
                    {
                        GetPathMembers = b => new BindingPathMembersMock(this, BindingPath.Create("IntProperty"), new BindingMemberInfo("IntProperty", BindingSourceModel.IntPropertyInfo, typeof(BindingSourceModel)))
                    }
                }
            };

            var converterMock = new ValueConverterCoreMock
            {
                Convert = (o, type, arg3, arg4) =>
                {
                    type.ShouldEqual(BindingSourceModel.IntPropertyInfo.PropertyType);
                    o.ShouldEqual(args[0]);
                    arg3.ShouldEqual(args[1]);
                    return sourceModel;
                }
            };
            var provider = new BindingProvider();
            var resolver = new BindingResourceResolver();
            BindingServiceProvider.ResourceResolver = resolver;
            resolver.AddConverter(converterName, converterMock, true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: provider);

            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, target, null).Single());
            context.Add(BindingConstants.Binding, bindingMock);

            IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath);
            path.Path.ShouldEqual(targetPath);

            var expression = context.GetData(BindingBuilderConstants.MultiExpression);
            expression(context, args).ShouldEqual(sourceModel);

            var sources = context.GetData(BindingBuilderConstants.Sources);
            BindingSourceShouldBeValidDataContext(target, sources[0].Invoke(context), sourcePath1);
            BindingSourceShouldBeValidDataContext(target, sources[1].Invoke(context), sourcePath2);
        }
예제 #2
0
        public void ParserShouldParseConverterName()
        {
            const string converterName = "test";
            const string binding = "Text SourceText, Converter=test";
            var value = new InverseBooleanValueConverter();
            var resolver = new BindingResourceResolver();
            resolver.AddConverter(converterName, value, true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: new BindingProvider());
            BindingServiceProvider.ResourceResolver = resolver;

            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, new object(), null).Single());
            context.GetData(BindingBuilderConstants.Converter).Invoke(EmptyContext).ShouldEqual(value);
        }