示例#1
0
        public void ShouldBindBetweenIndexedObjects()
        {
            var binder = new Binder <ObservableDictionary <UniversalStub> >();

            var dict = new ObservableDictionary <UniversalStub>();

            binder.BindIf(x => x.ContainsKey("first") && x.ContainsKey("second"), x => x["first"].String).To(x => x["second"].String);

            using (binder.Attach(dict))
            {
                var first  = new UniversalStub();
                var second = new UniversalStub {
                    String = "a"
                };
                dict.Add("second", second);
                second.String.ShouldBe("a");
                using (second.VerifyChangedOnce("String"))
                {
                    dict.Add("first", first);
                }
                second.String.ShouldBe(null);

                using (second.VerifyChangedOnce("String"))
                {
                    first.String = "b";
                }
                second.String.ShouldBe("b");
            }
        }
        public void ShouldUnsubscribeFromRemovedItems()
        {
            _binder.Bind(x => string.Join(";", x.Collection.Select(s => s.String).ToArray())).To(x => x.String);
            _stub.Collection = new ObservableCollection <UniversalStub>();

            var item1 = new UniversalStub {
                String = "1"
            };
            var item2 = new UniversalStub {
                String = "2"
            };

            _stub.Collection.Add(item1);
            _stub.Collection.Add(item2);

            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe("1;2");
                item1.SubscriptionsCount.ShouldBe(1);
                item2.SubscriptionsCount.ShouldBe(1);

                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.Collection.Remove(item1);
                }
                _stub.String.ShouldBe("2");
                item1.SubscriptionsCount.ShouldBe(0);
            }

            item2.SubscriptionsCount.ShouldBe(0);
        }
        public void ShouldBindBetweenIndexedObjects()
        {
            var binder = new Binder<ObservableDictionary<UniversalStub>>();

            var dict= new ObservableDictionary<UniversalStub>();

            binder.BindIf(x => x.ContainsKey("first") && x.ContainsKey("second"), x => x["first"].String).To(x => x["second"].String);

            using (binder.Attach(dict))
            {
                var first = new UniversalStub();
                var second = new UniversalStub { String = "a" };
                dict.Add("second", second);
                second.String.ShouldBe("a");
                using (second.VerifyChangedOnce("String"))
                {
                    dict.Add("first", first);
                }
                second.String.ShouldBe(null);

                using (second.VerifyChangedOnce("String"))
                {
                    first.String = "b";
                }
                second.String.ShouldBe("b");
            }
        }
        public void ShouldOverrideMultipleRules()
        {
            _binder.Bind(x => x.Int.ToString()).To(x => x.String);
            _binder.Bind(x => x.Flag.ToString()).To(x => x.String2);
            _binder.Bind(x => x.Int.ToString() + "2").To(x => x.String);
            _binder.Bind(x => x.Flag.ToString() + "2").To(x => x.String2);

            _stub = new UniversalStub();
            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe("02");
                _stub.String2.ShouldBe("False2");
                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.Int = 1;
                }
                _stub.String.ShouldBe("12");

                using (_stub.VerifyChangedOnce("String2"))
                {
                    _stub.Flag = true;
                }
                _stub.String2.ShouldBe("True2");
            }
        }
        public void ShouldNotBindToTheSameCollectionItemTwice()
        {
            _binder.Bind(x => x.Collection.Sum(y => y.Int)).To(x => x.Int);
            using (_binder.Attach(_stub))
            {
                var item = new UniversalStub();
                _stub.Collection.Add(item);
                _stub.Collection.Add(item);
                _stub.Int.ShouldBe(0);

                using (_stub.VerifyChangedOnce("Int"))
                {
                    item.Int = 1;
                }
                _stub.Int.ShouldBe(2);

                using (_stub.VerifyChangedOnce("Int"))
                {
                    _stub.Collection.RemoveAt(1);
                }
                _stub.Int.ShouldBe(1);

                using (_stub.VerifyChangedOnce("Int"))
                {
                    item.Int = 3;
                }
                _stub.Int.ShouldBe(3);
            }
        }
        public void ShouldNotSubscribeToCollectionItemsIfTheirPropertiesAreNotReferenced()
        {
            _binder.Bind(x => string.Join(";", x.Collection.Select(s => s.ToString()).ToArray())).To(x => x.String);
            _stub.Collection = new ObservableCollection <UniversalStub>();
            var item1 = new UniversalStub {
                String = "1"
            };

            _stub.Collection.Add(item1);

            using (_binder.Attach(_stub))
            {
                item1.SubscriptionsCount.ShouldBe(0);
            }
        }
        public void ShouldWorkOnNonObservableCollection()
        {
            _binder.Bind(x => string.Join(";", x.EnumerableCollection.Select(s => s.String))).To(x => x.String);
            var stub1 = new UniversalStub {
                String = "1"
            };
            var stub2 = new UniversalStub {
                String = "2"
            };

            _stub.EnumerableCollection = new[] { stub1, stub2 };

            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe("1;2");
                stub1.String = "a";
                _stub.String.ShouldBe("a;2");
            }
        }
        public void ModifiedClonedBindersShouldNotAffectOriginalOnes()
        {
            _binder.Bind(x => x.Int.ToString()).To(x => x.String);

            var binder2 = _binder.Clone <UniversalStub>();

            binder2.Bind(x => x.Flag.ToString()).To(x => x.String2);

            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe("0");
                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.Int = 1;
                }
                _stub.String.ShouldBe("1");

                _stub.String2.ShouldBe(null);
                using (_stub.VerifyNotChanged("String2"))
                {
                    _stub.Flag = true;
                }
                _stub.String2.ShouldBe(null);
            }

            _stub = new UniversalStub();
            using (binder2.Attach(_stub))
            {
                _stub.String.ShouldBe("0");
                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.Int = 1;
                }
                _stub.String.ShouldBe("1");

                _stub.String2.ShouldBe("False");
                using (_stub.VerifyChangedOnce("String2"))
                {
                    _stub.Flag = true;
                }
                _stub.String2.ShouldBe("True");
            }
        }
        public void ModifiedClonedBindersShouldNotAffectOriginalOnes()
        {
            _binder.Bind(x => x.Int.ToString()).To(x => x.String);

            var binder2 = _binder.Clone<UniversalStub>();
            binder2.Bind(x => x.Flag.ToString()).To(x => x.String2);

            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe("0");
                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.Int = 1;
                }
                _stub.String.ShouldBe("1");

                _stub.String2.ShouldBe(null);
                using (_stub.VerifyNotChanged("String2"))
                {
                    _stub.Flag = true;
                }
                _stub.String2.ShouldBe(null);
            }

            _stub = new UniversalStub();
            using (binder2.Attach(_stub))
            {
                _stub.String.ShouldBe("0");
                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.Int = 1;
                }
                _stub.String.ShouldBe("1");

                _stub.String2.ShouldBe("False");
                using (_stub.VerifyChangedOnce("String2"))
                {
                    _stub.Flag = true;
                }
                _stub.String2.ShouldBe("True");
            }
        }
        public void ShouldBindIndexedField()
        {
            var binder = new Binder<UniversalStub>();
            var stub = new UniversalStub();
            stub.Dictionary = new ObservableDictionary<string>();

            binder.Bind(x => x.Dictionary["test"]).To(x => x.String);

            using (binder.Attach(stub))
            {
                stub.String.ShouldBe(null);

                using (stub.VerifyChangedOnce("String"))
                {
                    stub.Dictionary.Add("test", "a");
                }

                stub.String.ShouldBe("a");
            }
        }
示例#11
0
        public void ShouldBindIndexedField()
        {
            var binder = new Binder <UniversalStub>();
            var stub   = new UniversalStub();

            stub.Dictionary = new ObservableDictionary <string>();

            binder.Bind(x => x.Dictionary["test"]).To(x => x.String);

            using (binder.Attach(stub))
            {
                stub.String.ShouldBe(null);

                using (stub.VerifyChangedOnce("String"))
                {
                    stub.Dictionary.Add("test", "a");
                }

                stub.String.ShouldBe("a");
            }
        }
        public void ShouldNotOverrideBindingRulesIfKeysAreDifferent()
        {
            _binder.Bind(x => x.Int.ToString()).OverrideKey("test1").To(x => x.String);
            _binder.Bind(x => x.String2).DoNotOverride().OverrideKey("test2").To(x => x.String);

            _stub = new UniversalStub();
            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe(null);
                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.Int = 1;
                }
                _stub.String.ShouldBe("1");

                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.String2 = "a";
                }
                _stub.String.ShouldBe("a");
            }
        }
        public void ShouldOverrideBindingRules()
        {
            _binder.Bind(x => x.Int.ToString()).To(x => x.String);
            _binder.Bind(x => x.String2).To(x => x.String);

            _stub = new UniversalStub();
            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe(null);
                using (_stub.VerifyNotChanged("String"))
                {
                    _stub.Int = 1;
                }
                _stub.String2.ShouldBe(null);

                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.String2 = "a";
                }
                _stub.String2.ShouldBe("a");
            }
        }
        public void ShouldOverrideConditionalBindingRulesByCustomKey()
        {
            _binder.BindIf(x => true, x => x.Int.ToString()).OverrideKey("mykey").To((x, v) => x.String = v);
            _binder.BindIf(x => true, x => x.String2).OverrideKey("mykey").To((x, v) => x.String        = v);

            _stub = new UniversalStub();
            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe(null);
                using (_stub.VerifyNotChanged("String"))
                {
                    _stub.Int = 1;
                }
                _stub.String2.ShouldBe(null);

                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.String2 = "a";
                }
                _stub.String2.ShouldBe("a");
            }
        }
        public void ShouldNotOverrideBindingRulesIfKeysAreDifferent()
        {
            _binder.Bind(x => x.Int.ToString()).OverrideKey("test1").To(x => x.String);
            _binder.Bind(x => x.String2).DoNotOverride().OverrideKey("test2").To(x => x.String);

            _stub = new UniversalStub();
            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe(null);
                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.Int = 1;
                }
                _stub.String.ShouldBe("1");

                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.String2 = "a";
                }
                _stub.String.ShouldBe("a");
            }
        }
        public void ShouldOverrideBindingRules()
        {
            _binder.Bind(x => x.Int.ToString()).To(x => x.String);
            _binder.Bind(x => x.String2).To(x => x.String);

            _stub = new UniversalStub();
            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe(null);
                using (_stub.VerifyNotChanged("String"))
                {
                    _stub.Int = 1;
                }
                _stub.String2.ShouldBe(null);

                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.String2 = "a";
                }
                _stub.String2.ShouldBe("a");
            }
        }
        public void ShouldDowncastClasses()
        {
            var binder = new Binder <UniversalStub>();

            binder.Bind(x => ((UniversalStubEx)x.Nested).String3).To(x => x.String);

            var stub   = new UniversalStub();
            var stubEx = new UniversalStubEx();

            stub.Nested = stubEx;

            using (binder.Attach(stub))
            {
                stub.String.ShouldBe(null);

                stubEx.String3 = "a";
                stub.String.ShouldBe("a");

                stub.Nested = new UniversalStubEx {
                    String3 = "b"
                };
                stub.String.ShouldBe("b");
            }
        }
        public void ShouldWorkOnObservableCollectionDeclaredAsEnumerable()
        {
            _binder.Bind(x => string.Join(";", x.EnumerableCollection.Select(s => s.String))).To(x => x.String);
            var stub1 = new UniversalStub {
                String = "1"
            };
            var stub2 = new UniversalStub {
                String = "2"
            };
            var collection = new ObservableCollection <UniversalStub>(new[] { stub1, stub2 });

            _stub.EnumerableCollection = collection;

            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe("1;2");
                collection.Add(new UniversalStub {
                    String = "3"
                });
                _stub.String.ShouldBe("1;2;3");
                stub1.String = "a";
                _stub.String.ShouldBe("a;2;3");
            }
        }
        public void ShouldOverrideMultipleRules()
        {
            _binder.Bind(x => x.Int.ToString()).To(x => x.String);
            _binder.Bind(x => x.Flag.ToString()).To(x => x.String2);
            _binder.Bind(x => x.Int.ToString() + "2").To(x => x.String);
            _binder.Bind(x => x.Flag.ToString() + "2").To(x => x.String2);

            _stub = new UniversalStub();
            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe("02");
                _stub.String2.ShouldBe("False2");
                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.Int = 1;
                }
                _stub.String.ShouldBe("12");

                using (_stub.VerifyChangedOnce("String2"))
                {
                    _stub.Flag = true;
                }
                _stub.String2.ShouldBe("True2");
            }
        }
        public void ShouldOverrideConditionalBindingRulesByCustomKey()
        {
            _binder.BindIf(x => true, x => x.Int.ToString()).OverrideKey("mykey").To((x, v) => x.String = v);
            _binder.BindIf(x => true, x => x.String2).OverrideKey("mykey").To((x, v) => x.String = v);

            _stub = new UniversalStub();
            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe(null);
                using (_stub.VerifyNotChanged("String"))
                {
                    _stub.Int = 1;
                }
                _stub.String2.ShouldBe(null);

                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.String2 = "a";
                }
                _stub.String2.ShouldBe("a");
            }
        }
 private static void ActionBinding(UniversalStub stub, int source)
 {
     stub.Int = source;
 }
        public void ShouldNotBindToTheSameCollectionItemTwice()
        {
            _binder.Bind(x => x.Collection.Sum(y => y.Int)).To(x => x.Int);
            using (_binder.Attach(_stub))
            {
                var item = new UniversalStub();
                _stub.Collection.Add(item);
                _stub.Collection.Add(item);
                _stub.Int.ShouldBe(0);

                using (_stub.VerifyChangedOnce("Int"))
                {
                    item.Int = 1;
                }
                _stub.Int.ShouldBe(2);

                using (_stub.VerifyChangedOnce("Int"))
                {
                    _stub.Collection.RemoveAt(1);
                }
                _stub.Int.ShouldBe(1);

                using (_stub.VerifyChangedOnce("Int"))
                {
                    item.Int = 3;
                }
                _stub.Int.ShouldBe(3);
            }
        }
        public void ShouldNotSubscribeToCollectionItemsIfTheirPropertiesAreNotReferenced()
        {
            _binder.Bind(x => string.Join(";", x.Collection.Select(s => s.ToString()).ToArray())).To(x => x.String);
            _stub.Collection = new ObservableCollection<UniversalStub>();
            var item1 = new UniversalStub { String = "1" };
            _stub.Collection.Add(item1);

            using (_binder.Attach(_stub))
            {
                item1.SubscriptionsCount.ShouldBe(0);
            }
        }
 private static bool ExternalCondition(UniversalStub stub)
 {
     return(stub.Flag);
 }
        public void ShouldUnsubscribeFromRemovedItems()
        {
            _binder.Bind(x => string.Join(";", x.Collection.Select(s => s.String).ToArray())).To(x => x.String);
            _stub.Collection = new ObservableCollection<UniversalStub>();

            var item1 = new UniversalStub { String = "1" };
            var item2 = new UniversalStub { String = "2" };
            _stub.Collection.Add(item1);
            _stub.Collection.Add(item2);

            using (_binder.Attach(_stub))
            {
                _stub.String.ShouldBe("1;2");
                item1.SubscriptionsCount.ShouldBe(1);
                item2.SubscriptionsCount.ShouldBe(1);

                using (_stub.VerifyChangedOnce("String"))
                {
                    _stub.Collection.Remove(item1);
                }
                _stub.String.ShouldBe("2");
                item1.SubscriptionsCount.ShouldBe(0);
            }

            item2.SubscriptionsCount.ShouldBe(0);
        }
示例#26
0
 public void SetUp()
 {
     _binder = new Binder <UniversalStub>();
     _stub   = new UniversalStub();
 }
 private static bool ExternalCondition(UniversalStub stub)
 {
     return stub.Flag;
 }