Пример #1
0
        public void ParserShouldParseMultiExpressionWithCustomObjectValueDynamic2()
        {
            const string targetPath = "Text";
            const string sourcePath1 = "SourceText1";
            const string sourcePath2 = "SourceText2";
            const string objectName = "TestObject";
            const string binding = "Text SourceText1 + SourceText2 == $TestObject.IntProperty";
            var targetObj = new object();
            var model = new BindingSourceModel { IntProperty = 10 };
            var args = new object[] { 9, 1, model.IntProperty };

            var provider = new BindingProvider();
            var resolver = new BindingResourceResolver();
            BindingServiceProvider.ResourceResolver = resolver;
            resolver.AddObject(objectName, new BindingResourceObject(model), true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: provider);

            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, targetObj, null).Single());
            IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath);
            path.Path.ShouldEqual(targetPath);

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

            var sources = context.GetData(BindingBuilderConstants.Sources);
            BindingSourceShouldBeValidDataContext(targetObj, sources[0].Invoke(context), sourcePath1);
            BindingSourceShouldBeValidDataContext(targetObj, sources[1].Invoke(context), sourcePath2);
            BindingSourceShouldBeValid(sources[2].Invoke(context),
                GetMemberPath(model, sourceModel => sourceModel.IntProperty), model);
        }
Пример #2
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);
        }
Пример #3
0
        public void ParserShouldParseMultiExpressionWithCustomObjectValueDynamic1()
        {
            const string targetPath = "Text";
            const string sourcePath1 = "SourceText1";
            const string sourcePath2 = "SourceText2";
            const string sourcePath3 = "Item1";
            const string objectName = "TestObject";
            const string binding = "Text SourceText1 + SourceText2 == $TestObject.Item1";
            var target = new object();
            var tuple = new Tuple<int>(10);
            var expressionObject = new BindingResourceObject(tuple);
            var args = new object[] { 9, 1, tuple.Item1 };

            var provider = new BindingProvider();
            var resolver = new BindingResourceResolver();
            BindingServiceProvider.ResourceResolver = resolver;
            resolver.AddObject(objectName, expressionObject, true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: provider);

            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, target, null).Single());
            IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath);
            path.Path.ShouldEqual(targetPath);

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

            var sources = context.GetData(BindingBuilderConstants.Sources);
            BindingSourceShouldBeValidDataContext(target, sources[0].Invoke(context), sourcePath1);
            BindingSourceShouldBeValidDataContext(target, sources[1].Invoke(context), sourcePath2);
            BindingSourceShouldBeValid(sources[2].Invoke(context), sourcePath3, tuple);
        }
Пример #4
0
        public void ParserShouldParseMultiExpressionWithCustomMethod()
        {
            const string targetPath = "Text";
            const string sourcePath1 = "SourceText1";
            const string sourcePath2 = "SourceText2";
            const string methodName = "TestMethod";
            const string binding = "Text $TestMethod(SourceText1, SourceText2).IntProperty";
            var target = new object();
            var args = new object[] { "tset", 1 };
            var sourceModel = new BindingSourceModel { IntProperty = int.MaxValue };

            var provider = new BindingProvider();
            var resolver = new BindingResourceResolver();
            BindingServiceProvider.ResourceResolver = resolver;
            var method = new BindingResourceMethod((list, objects, c) =>
            {
                objects[0].ShouldEqual(args[0]);
                objects[1].ShouldEqual(args[1]);
                return sourceModel;
            }, typeof(BindingSourceModel));
            resolver.AddMethod(methodName, method, true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: provider);


            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, target, null).Single());
            IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath);
            path.Path.ShouldEqual(targetPath);

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

            var sources = context.GetData(BindingBuilderConstants.Sources);
            BindingSourceShouldBeValidDataContext(target, sources[0].Invoke(context), sourcePath1);
            BindingSourceShouldBeValidDataContext(target, sources[1].Invoke(context), sourcePath2);
        }
Пример #5
0
 protected virtual IBindingParser CreateBindingParser(IVisualTreeManager treeManager = null, IBindingProvider bindingProvider = null, IObserverProvider observerProvider = null)
 {
     if (bindingProvider == null)
         bindingProvider = new BindingProvider();
     BindingServiceProvider.BindingProvider = bindingProvider;
     if (treeManager != null)
         BindingServiceProvider.VisualTreeManager = treeManager;
     if (observerProvider != null)
         BindingServiceProvider.ObserverProvider = observerProvider;
     return new BindingParser();
 }
        public void ParserShouldParseMultiExpressionWithOneTimeScope()
        {
            const string targetPath = "Text";
            const string sourcePath1 = "SourceText1";
            const string sourcePath2 = "SourceText2";
            const string sourcePath3 = "SourceText3";
            const string methodName = "TestMethod";
            const string binding = "Text $OneTime($TestMethod(SourceText1, SourceText2).IntProperty) + SourceText3";

            const int firstValue = -1;
            int executionCount = 0;
            var target = new object();
            var args = new object[] { "tset", 1, 3 };
            var sourceModel = new BindingSourceModel { IntProperty = firstValue };

            var provider = new BindingProvider();
            var resolver = new BindingResourceResolver();
            BindingServiceProvider.ResourceResolver = resolver;
            var method = new BindingResourceMethod((list, objects, c) =>
            {
                ++executionCount;
                objects[0].ShouldEqual(args[0]);
                objects[1].ShouldEqual(args[1]);
                return sourceModel;
            }, typeof(BindingSourceModel));
            resolver.AddMethod(methodName, method, true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: provider);


            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, target, null).Single());
            IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath);
            path.Path.ShouldEqual(targetPath);

            var expression = context.GetData(BindingBuilderConstants.MultiExpression);
            expression(context, args).ShouldEqual(firstValue + 3);
            sourceModel.IntProperty = int.MinValue;
            args[2] = 5;
            expression(context, args).ShouldEqual(firstValue + 5);
            args[2] = 6;
            expression(context, args).ShouldEqual(firstValue + 6);

            executionCount.ShouldEqual(1);

            var sources = context.GetData(BindingBuilderConstants.Sources);
            BindingSourceShouldBeValidDataContext(target, sources[0].Invoke(context), sourcePath1);
            BindingSourceShouldBeValidDataContext(target, sources[1].Invoke(context), sourcePath2);
            BindingSourceShouldBeValidDataContext(target, sources[2].Invoke(context), sourcePath3);
        }
        public void ParserShouldParseMultiExpressionWithCustomObjectValueStatic2()
        {
            const string targetPath = "Text";
            const string sourcePath1 = "SourceText1";
            const string sourcePath2 = "SourceText2";
            const string objectName = "TestObject";
            const string binding = "Text (SourceText1 + SourceText2).GetHashCode() == $$TestObject.GetHashCode()";
            var target = new object();
            var args = new object[] { 9, 1 };

            var provider = new BindingProvider();
            var resolver = new BindingResourceResolver();
            BindingServiceProvider.ResourceResolver = resolver;
            resolver.AddObject(objectName, new BindingResourceObject(10), true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: provider);

            var context = new BindingBuilder(bindingParser.Parse(target, binding, null, null).Single());
            IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath);
            path.Path.ShouldEqual(targetPath);

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

            var sources = context.GetData(BindingBuilderConstants.Sources);
            BindingSourceShouldBeValidDataContext(target, sources[0].Invoke(context), sourcePath1);
            BindingSourceShouldBeValidDataContext(target, sources[1].Invoke(context), sourcePath2);
        }