Пример #1
0
        public void TestDynamicProxyObservability()
        {
            // SomeDynamic inherits from System.Dynamic.DynamicObject
            dynamic d = new SomeObj().ToDynamicObservable();

            int notificationCount = 0;
            Queue<object[]> expected = new Queue<object[]>();
            expected.Enqueue(new object[] { d, "IntProp", 0, 10 });
            expected.Enqueue(new object[] { d, "IntProp", 10, 20 });

            (d as IObjectObservable).PropertyChanged2 += (source, propertyName, oldValue, newValue) =>
            {
                var expectedResult = expected.Dequeue();
                Assert.AreEqual(expectedResult[0], source);
                Assert.AreEqual(expectedResult[1], propertyName);
                Assert.AreEqual(expectedResult[2], oldValue);
                Assert.AreEqual(expectedResult[3], newValue);
                notificationCount++;
            };

            d.IntProp = 10;
            d.IntProp = 20;

            try
            {
                d.DoesNotExist = "Should throw";
                Assert.Fail("I should not be able to extend this object");
            }
            catch (RuntimeBinderException ex)  { }

            Assert.AreEqual(2, notificationCount);
            Assert.AreEqual(20, d.IntProp);
        }
Пример #2
0
        public void TestTransactionPositive()
        {
            var obj = new SomeObj();
            obj.IntProp = 20;
            using (var context = obj.ForgivableScope())
            {
                context.Target.IntProp = 50;
                context.Complete();
            }

            Assert.AreEqual(50, obj.IntProp);
        }
Пример #3
0
        public void TestProxiedExtendableObservable()
        {
            var poco = new SomeObj();
            var extendedObserver = poco.GetObservableProxy();

            var token = extendedObserver.SetExpectedChanges(
                new Change(poco, "IntProp", 0, 1),
                new Change(poco, "IntProp", 1, 2));

            poco.SetProperty("IntProp", 1);
            poco.SetProperty("IntProp", 2);
            Assert.AreEqual(2, poco.IntProp);
            token.ValidateAllNotificationsHappened();
        }
Пример #4
0
        public void TestBasicExtensibility()
        {
            var extendable = new SomeObj().Dynamify();
            extendable.NewProperty = 10;
            extendable.IntProp = 10;

            // Validate an extended property
            Assert.AreEqual(10, extendable.NewProperty);
            Assert.AreEqual(10, (extendable.Unwrap() as object).GetProperty("NewProperty"));

            // Validate a normal property
            Assert.AreEqual(10, extendable.IntProp);
            Assert.AreEqual(10, (extendable.Unwrap() as object).GetProperty("IntProp"));
            Assert.AreEqual(10, (extendable.Unwrap() as SomeObj).IntProp);
        }
Пример #5
0
        public void TestThatExtendableIsAlsoObservable()
        {
            var extendable = new SomeObj().Dynamify();

            var token = (extendable as IObjectObservable).SetExpectedChanges(
                new Change(extendable, "First", null, "Adam"),
                new Change(extendable, "First", "Adam", "Joe"),
                new Change(extendable, "IntProp", 0, 10));

            extendable.First = "Adam";
            extendable.First = "Joe";
            Assert.AreEqual("Joe", (extendable.Unwrap() as object).GetProperty("First"));

            extendable.IntProp = 10;
            token.ValidateAllNotificationsHappened();
        }
Пример #6
0
 public void TestTransactionNegative()
 {
     var obj = new SomeObj();
     obj.IntProp = 20;
     try
     {
         using (var context = obj.ForgivableScope())
         {
             context.Target.IntProp = 50;
             throw new InternalTestFailureException();
             context.Complete();
         }
     }
     catch (InternalTestFailureException) { }
     Assert.AreEqual(20, obj.IntProp);
 }
Пример #7
0
        public void TestCollectionObservable()
        {
            var obj = new SomeObj().Observable();
            obj.IntegerList = new ObservableList<int>();

            int counter = 0;
            (obj as IObjectObservable).PropertyChanged+=new PropertyChangedEventHandler((a,b)=>
            {
                counter++;
            });

            Assert.AreEqual(0, obj.IntegerList.Count);
            obj.IntegerList.Add(1);
            Assert.AreEqual(1, obj.IntegerList.Count);
            Assert.AreEqual(1, obj.IntegerList[0]);
            Assert.AreEqual(1, counter);
        }
Пример #8
0
        public void TestRedo()
        {
            ForgivenessContext forgiver;
            var obj = new SomeObj().Forgivable(out forgiver);

            obj.IntProp = 10;
            obj.IntProp = 20;

            Assert.AreEqual(20, obj.IntProp);
            Assert.IsFalse(forgiver.CanRedo);
            forgiver.Undo();
            Assert.AreEqual(10, obj.IntProp);
            Assert.IsTrue(forgiver.CanRedo);

            forgiver.Redo();
            Assert.AreEqual(20, obj.IntProp);
        }
Пример #9
0
        public void TestUndo()
        {
            ForgivenessContext forgiver;
            var obj = new SomeObj().Forgivable(out forgiver);

            Assert.IsFalse(forgiver.CanUndo);
            Assert.IsFalse(forgiver.CanRedo);

            for (int i = 0; i < 10; i++)
            {
                obj.IntProp = i;
                Assert.IsTrue(obj.IntProp == i);
                Assert.IsTrue(forgiver.CanUndo);
                Assert.IsFalse(forgiver.CanRedo);
            }

            for (int i = 9; i >= 0; i--)
            {
                Assert.IsTrue(obj.IntProp == i);
                Assert.IsTrue(forgiver.CanUndo);
                forgiver.Undo();
                Assert.IsTrue(forgiver.CanRedo);
            }
        }
Пример #10
0
        public void TestPoco()
        {
            var obj = new SomeObj().Observable();
            Assert.IsTrue(obj is SomeObj);

            bool observer1Notified = false, observer2Notified = false;

            (obj as IObjectObservable).PropertyChanged2 += (source, propertyName, oldValue, newValue) =>
            {
                Assert.AreEqual(0, oldValue);
                Assert.AreEqual(10, newValue);
                Assert.AreEqual("IntProp", propertyName);
                Assert.IsTrue(source is SomeObj);
                Assert.AreEqual((source as SomeObj).IntProp, newValue);
                observer1Notified = true;
            };

            // Add observers with the classic, well supported INotifyPropertyChanged mechanism
            (obj as INotifyPropertyChanged).PropertyChanged += (source, args) =>
            {
                Assert.IsTrue(source is SomeObj);
                Assert.AreEqual(10, (source as SomeObj).IntProp);
                Assert.AreEqual("IntProp", args.PropertyName);
                observer2Notified = true;
            };

            obj.IntProp = 10;

            Assert.IsTrue(observer1Notified);
            Assert.IsTrue(observer2Notified);

            // You can always unwrap the proxy if needed
            var unwrapped = (obj as IObjectObservable).Wrapped as SomeObj;
            Assert.IsNotNull(unwrapped);
        }
Пример #11
0
        public void TestNotificationSuppression()
        {
            var obj = new SomeObj().Observable();

            (obj as IObjectObservable).SetExpectedChanges(
                new Change(obj, "IntProp", 0, 10),
                new Change(obj, "IntProp", 10, 20));
            (obj as IObjectObservable).SuppressNonChanges = true;

            // This should fire an event
            obj.IntProp = 10;

            // This should not fire an event
            obj.IntProp = 10;

            // This should fire an event
            obj.IntProp = 20;
        }