Exemplo n.º 1
0
        public void AllProxiesAreOfSameProxyType()
        {
            A a1 = TrackableObjectFactory.CreateFrom(new A());
            A a2 = TrackableObjectFactory.CreateFrom(new A());

            Assert.AreEqual(a1.GetType(), a2.GetType());
        }
Exemplo n.º 2
0
        public void CanTrackListPropertyChanges()
        {
            A a = TrackableObjectFactory.CreateFrom(new A());

            a.Items.Add("hola");

            B b = TrackableObjectFactory.CreateFrom(new B());

            b.Dogs.First().Name = "Rex";
            b.Dogs.Add(new Dog {
                Name = "Rex"
            });

            C c = TrackableObjectFactory.CreateFrom(new C());

            c.Dogs.Add(new Dog {
                Name = "Rex"
            });

            IObjectChangeTracker Atracking = a.GetChangeTracker();
            IObjectChangeTracker Btracking = b.GetChangeTracker();
            IObjectChangeTracker Ctracking = c.GetChangeTracker();

            Assert.AreEqual(1, Atracking.ChangedProperties.Count);
            Assert.AreEqual(1, Btracking.ChangedProperties.Count);
            Assert.AreEqual(1, Ctracking.ChangedProperties.Count);
        }
Exemplo n.º 3
0
        public void TrackingTypeWithNonTrackableCollectionWontCrash()
        {
            D d = TrackableObjectFactory.CreateFrom(new D());

            d.Mask = new BitArray(38);

            Assert.IsTrue(d.PropertyHasChanged(o => o.Mask));
        }
Exemplo n.º 4
0
        public void CanCreateProxyWithConstructorArguments()
        {
            B b = TrackableObjectFactory.CreateOf <B>(1, 2);

            Assert.IsTrue(b.IsTrackable());

            b.Text = "hello world";
        }
Exemplo n.º 5
0
        public void CanTrackCollectionPropertiesOfNonTrackableTypes()
        {
            WhateverParent parent = TrackableObjectFactory.CreateFrom(new WhateverParent());

            parent.List2.Add("hey");
            parent.List.Add(new Whatever());

            Assert.AreEqual(2, parent.GetChangeTracker().ChangedProperties.Count);
        }
Exemplo n.º 6
0
        public void CanCallMethodOfDictionary()
        {
            E e = new E();

            e.Dictionary.Add("hello", "world");
            e = TrackableObjectFactory.CreateFrom(e);

            e.Dictionary.ContainsKey("hello");
        }
Exemplo n.º 7
0
        public void CanTrackChangesOfCollectionOfEnclosingType()
        {
            F f = TrackableObjectFactory.CreateFrom(new F());

            f.ListOfF.Add(new F());

            Assert.IsTrue(f.PropertyHasChanged(o => o.ListOfF));
            Assert.AreEqual(1, ((IReadOnlyChangeTrackableCollection)f.ListOfF).AddedItems.Count);
        }
Exemplo n.º 8
0
        public void ArrayMustNotBeTrackedAsCollections()
        {
            G g = TrackableObjectFactory.CreateFrom(new G());

            g.Buffer = new byte[] { };

            Assert.IsTrue(g.PropertyHasChanged(o => o.Buffer));
            Assert.IsNotInstanceOfType(g.Buffer, typeof(IReadOnlyChangeTrackableCollection));
        }
Exemplo n.º 9
0
        public void DictionaryKeysAreNotMessedUp()
        {
            E e = TrackableObjectFactory.CreateOf <E>();

            e.Dictionary.Add("Hey", "Oops!");

            E untrackedE = e.ToUntracked();

            Assert.AreEqual("Hey", e.Dictionary.First().Key);
        }
Exemplo n.º 10
0
        public void CanTrackDictionaryChanges()
        {
            E e = new E();

            e.Dictionary.Add("hello", "world");
            e = TrackableObjectFactory.CreateFrom(e);
            e.Dictionary.Add("bye", "bye");

            Assert.IsTrue(e.PropertyHasChanged(o => o.Dictionary));
        }
Exemplo n.º 11
0
        public void CanExtractProxyTargetWithChanges()
        {
            C c = TrackableObjectFactory.CreateFrom(new C {
                Text = "hello world"
            });

            c.Text = "hello world 2";

            C noProxy = c.ToUntracked();

            Assert.AreEqual("hello world 2", noProxy.Text);
        }
        private void ToTrackableCollection(PropertyInfo property, IChangeTrackableObject parentObject)
        {
            Contract.Requires(() => property != null, "Cannot turn the object held by the property because the given property is null");
            Contract.Requires(() => parentObject != null, "A non-null reference to the object owning given property is mandatory");

            if (property.IsEnumerable() && !property.PropertyType.IsArray && ChangeTrackingContext.Configuration.Collections.CanTrack(property.PropertyType))
            {
                object proxiedCollection = TrackableObjectFactory.CreateForCollection(property.GetValue(parentObject), parentObject, property);

                Contract.Assert(() => property.SetMethod != null, $"Property '{property.Name} must implement a setter");
                property.SetValue(parentObject, proxiedCollection);
            }
        }
Exemplo n.º 13
0
        public void CanTrackSetPropertyChanges()
        {
            C c = TrackableObjectFactory.CreateFrom(new C());

            IObjectChangeTracker Ctracking = c.GetChangeTracker();
            IReadOnlyChangeTrackableCollection trackableCollection = (IReadOnlyChangeTrackableCollection)c.Dogs;

            c.Dogs.Add(new Dog {
                Name = "Rex"
            });

            Assert.AreEqual(1, Ctracking.ChangedProperties.Count);
        }
Exemplo n.º 14
0
        public void CanTurnCollectionItemsToUntracked()
        {
            B b = TrackableObjectFactory.CreateFrom(new B());

            b.Dogs.First().Name = "Rex";
            b.Dogs.Add(new Dog {
                Name = "Rex"
            });

            b = b.ToUntracked();

            Assert.IsFalse(b.IsTrackable());
            Assert.IsFalse(b.Dogs.Any(dog => dog.IsTrackable()));
        }
Exemplo n.º 15
0
        public void CanClearCollectionChanges()
        {
            A a = TrackableObjectFactory.CreateOf <A>();

            a.Items.Add("a");
            a.Items.Add("b");

            a.Items.ClearChanges();

            IReadOnlyChangeTrackableCollection trackableCollection
                = (IReadOnlyChangeTrackableCollection)a.Items;

            Assert.AreEqual(0, trackableCollection.AddedItems.Count);
            Assert.AreEqual(0, trackableCollection.RemovedItems.Count);
        }
Exemplo n.º 16
0
        public void CollectionItemsArePreservedWhenTurningParentObjectIntoTrackable()
        {
            A a = TrackableObjectFactory.CreateFrom(new A());

            Assert.AreEqual(3, a.Items.Count);

            WhateverParent parent = new WhateverParent();

            parent.List.Add(new Whatever());
            parent.List2.Add("hey");

            parent = TrackableObjectFactory.CreateFrom(parent);

            Assert.AreEqual(1, parent.List.Count);
            Assert.AreEqual(1, parent.List2.Count);
        }
Exemplo n.º 17
0
        public void CanTrackSetIntersections()
        {
            C c = TrackableObjectFactory.CreateFrom(new C());

            c.Dogs.Add(new Dog {
                Name = "Rex"
            });

            IReadOnlyChangeTrackableCollection trackableCollection = (IReadOnlyChangeTrackableCollection)c.Dogs;

            c.Dogs.IntersectWith(new[] { new Dog {
                                             Name = "Rex"
                                         } });

            Assert.AreEqual(1, c.Dogs.Count);
            Assert.AreEqual(2, trackableCollection.RemovedItems.Count);
            Assert.AreEqual(1, trackableCollection.AddedItems.Count);
        }
Exemplo n.º 18
0
        public void CanTrackDynamicObjectPropertyChanges()
        {
            dynamic dynamicObject = TrackableObjectFactory.CreateFrom(new TestDynamicObject());

            dynamicObject.Text  = "hello world 1";
            dynamicObject.Text  = "hello world 2";
            dynamicObject.Text2 = "hello world 3";
            ((TestDynamicObject)dynamicObject).DeclaredText = "hello world 4";
            ((TestDynamicObject)dynamicObject).DeclaredText = "hello world 5";

            IObjectChangeTracker          tracker  = ((object)dynamicObject).GetChangeTracker();
            IObjectPropertyChangeTracking tracking = tracker.GetDynamicTrackingByProperty("Text");

            Assert.AreEqual(2, tracker.ChangedProperties.Count);
            Assert.AreEqual("Text", tracking.PropertyName);
            Assert.AreEqual("hello world 1", tracking.OldValue);
            Assert.AreEqual("hello world 2", tracking.CurrentValue);
            Assert.AreEqual("hello world 1", ((object)dynamicObject).OldPropertyValue("Text"));
            Assert.AreEqual(null, ((TestDynamicObject)dynamicObject).OldPropertyValue(o => o.DeclaredText));
            Assert.AreEqual("hello world 2", ((object)dynamicObject).CurrentPropertyValue("Text"));
            Assert.AreEqual("hello world 5", ((TestDynamicObject)dynamicObject).CurrentPropertyValue(o => o.DeclaredText));
            Assert.IsTrue(((object)dynamicObject).PropertyHasChanged("Text"));
            Assert.IsFalse(((object)dynamicObject).PropertyHasChanged("Text2"));
        }
Exemplo n.º 19
0
        public void CanUntrackDynamicProperties()
        {
            const string text1 = "hello";
            const string text2 = "world";

            dynamic a = TrackableObjectFactory.CreateFrom
                        (
                new TestDynamicObject
            {
                DeclaredText = text1
            }
                        );

            a.DynamicText = text2;
            a.A           = new A();

            TestDynamicObject untrackedA = ((TestDynamicObject)a).ToUntracked();

            Assert.AreEqual(text1, untrackedA.DeclaredText);
            Assert.AreEqual(text2, ((dynamic)untrackedA).DynamicText);
            Assert.IsNotNull(((dynamic)untrackedA).A);
            Assert.IsFalse(untrackedA.IsTrackable());
            Assert.IsNotNull(((TestDynamicObject)((dynamic)untrackedA)).IsTrackable());
        }