Example #1
0
        public void Implementation_IsReadonly_ObservedPropertyShouldThrowException()
        {
            // Arrange
            Mock <IObservableProperty> nestedObservableMock = new Mock <IObservableProperty>();

            nestedObservableMock.SetupGet(x => x.ObservedType).Returns(typeof(ISimpleInterface));
            nestedObservableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>());
            nestedObservableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>());
            nestedObservableMock.Setup(x => x.TryGetMember(It.IsAny <string>(), out It.Ref <object> .IsAny)).Returns(true);
            var nestedObservable = (object)nestedObservableMock.Object;

            Mock <IObservableProperty> observableMock = new Mock <IObservableProperty>();

            observableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>
            {
                [nameof(IInterfaceWithNestedObservable.NestedObservable)] = nestedObservableMock.Object
            });
            observableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>());
            observableMock.Setup(x => x.TryGetMember(It.IsAny <string>(), out nestedObservable)).Returns(true);
            Mock <IPropertyAccessedSubscriber> subscriberMock = new Mock <IPropertyAccessedSubscriber>();

            // Act
            var manager = new RuntimeProxyManager <IInterfaceWithNestedObservable>(observableMock.Object, true);

            manager.Subscribe(subscriberMock.Object);

            IInterfaceWithNestedObservable impl = manager.Implementation;

            impl.NestedObservable.StringValue = "test";

            // Assert
            // exc
        }
Example #2
0
        public void Implementation_NestedObservedProperty_NotifyAllPropertyAccessed()
        {
            // Arrange
            Mock <IObservableProperty> nestedObservableMock = new Mock <IObservableProperty>();

            nestedObservableMock.SetupGet(x => x.ObservedType).Returns(typeof(ISimpleInterface));
            nestedObservableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>());
            nestedObservableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>());
            nestedObservableMock.Setup(x => x.TryGetMember(It.IsAny <string>(), out It.Ref <object> .IsAny)).Returns(true);
            var nestedObservable = (object)nestedObservableMock.Object;

            Mock <IObservableProperty> observableMock = new Mock <IObservableProperty>();

            observableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>
            {
                [nameof(IInterfaceWithNestedObservable.NestedObservable)] = nestedObservableMock.Object
            });
            observableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>());
            observableMock.Setup(x => x.TryGetMember(nameof(IInterfaceWithNestedObservable.NestedObservable), out nestedObservable)).Returns(true);

            Mock <IPropertyAccessedSubscriber> subscriberMock = new Mock <IPropertyAccessedSubscriber>();

            // Act
            var manager = new RuntimeProxyManager <IInterfaceWithNestedObservable>(observableMock.Object);

            manager.Subscribe(subscriberMock.Object);
            IInterfaceWithNestedObservable impl = manager.Implementation;

            Trace.WriteLine(impl.NestedObservable.StringValue);

            // Assert
            subscriberMock.Verify(x => x.OnPropertyAccessed(It.Is <PropertyAccessedArgs>(args => args.PropertyName == nameof(IInterfaceWithNestedObservable.NestedObservable))), Times.Once);
            subscriberMock.Verify(x => x.OnPropertyAccessed(It.Is <PropertyAccessedArgs>(args => args.PropertyName == nameof(ISimpleInterface.StringValue))), Times.Once);
        }
Example #3
0
        public void Implementation_Manager_IsCorrectInstance()
        {
            // Arrange
            Mock <IObservableProperty> observableMock = new Mock <IObservableProperty>();

            observableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>());
            observableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>());

            Mock <IPropertyAccessedSubscriber> subscriberMock = new Mock <IPropertyAccessedSubscriber>();
            // Act
            var manager           = new RuntimeProxyManager <ISimpleInterface>(observableMock.Object);
            ISimpleInterface impl = manager.Implementation;

            // Assert
            Assert.AreEqual(manager, ((IRuntimeProxy)impl).Manager);
        }
        public void RuntimeProxy__HasModelWithArray_ReturnCorrectValue()
        {
            // Arrange
            string propName = nameof(ModelWithArrayProperty.StringArray);
            object arr      = new string[0];

            var observableMock = new Mock <IObservableProperty>(MockBehavior.Strict);

            observableMock.Setup(x => x.TryGetMember(propName, out arr)).Returns(true);
            observableMock.Setup(x => x.CreateFactory()).Returns((IObservableFactory)null);
            observableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>());
            observableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>());
            observableMock.Setup(x => x.TrySetDefaultValue(propName, null)).Returns(true);

            var manager = new RuntimeProxyManager <ModelWithArrayProperty>(observableMock.Object);

            // Act
            var instance = manager.Implementation;

            // Assert
            Assert.AreEqual(arr, instance.StringArray);
        }
Example #5
0
        public void Implementation_IsReadonly_ThrowException()
        {
            // Arrange
            Mock <IObservableProperty> observableMock = new Mock <IObservableProperty>();

            observableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>());
            observableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>());
            observableMock.Setup(x => x.TryGetMember(It.IsAny <string>(), out It.Ref <object> .IsAny)).Returns(true);
            Mock <IPropertyAccessedSubscriber> subscriberMock = new Mock <IPropertyAccessedSubscriber>();

            // Act
            var manager = new RuntimeProxyManager <ISimpleInterface>(observableMock.Object, true);

            manager.Subscribe(subscriberMock.Object);

            ISimpleInterface impl = manager.Implementation;

            impl.StringValue = "test";

            // Assert
            // exc
        }
Example #6
0
        public void Implementation_ValueType_NotifyPropertyAccessed()
        {
            // Arrange
            object value = 10;
            Mock <IObservableProperty> observableMock = new Mock <IObservableProperty>();

            observableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>());
            observableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>());
            observableMock.Setup(x => x.TryGetMember(nameof(ISimpleInterface.ValueTypeValue), out value)).Returns(true);
            Mock <IPropertyAccessedSubscriber> subscriberMock = new Mock <IPropertyAccessedSubscriber>();

            // Act
            var manager = new RuntimeProxyManager <ISimpleInterface>(observableMock.Object);

            manager.Subscribe(subscriberMock.Object);
            ISimpleInterface impl = manager.Implementation;

            Trace.WriteLine(impl.ValueTypeValue);

            // Assert
            subscriberMock.Verify(x => x.OnPropertyAccessed(It.Is <PropertyAccessedArgs>(args => args.PropertyName == nameof(impl.ValueTypeValue))), Times.Once);
        }
Example #7
0
        public void Implementation_InterfaceWithReadonlyProperty_NotifyPropertyAccessed()
        {
            // Arrange
            Mock <IObservableProperty> observableMock = new Mock <IObservableProperty>();

            observableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>());
            observableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>());
            observableMock.Setup(x => x.TryGetMember(It.IsAny <string>(), out It.Ref <object> .IsAny)).Returns(true);

            Mock <IPropertyAccessedSubscriber> subscriberMock = new Mock <IPropertyAccessedSubscriber>();

            // Act
            var manager = new RuntimeProxyManager <IInterfaceWithReadonlyProperty>(observableMock.Object);

            manager.Subscribe(subscriberMock.Object);
            IInterfaceWithReadonlyProperty impl = manager.Implementation;

            Trace.WriteLine(impl.ReadonlyStringProperty);

            // Assert
            subscriberMock.Verify(x => x.OnPropertyAccessed(It.Is <PropertyAccessedArgs>(args => args.PropertyName == nameof(impl.ReadonlyStringProperty))), Times.Once);
        }
Example #8
0
        public void Implementation_RuntimeTypeWithoutInterceptions_CacheRuntimeType()
        {
            // Arrange
            Mock <IObservableProperty> observableMock = new Mock <IObservableProperty>();

            observableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>());
            observableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>());
            observableMock.Setup(x => x.TryGetMember(It.IsAny <string>(), out It.Ref <object> .IsAny)).Returns(true);

            Mock <IObservableProperty> nestedObservableMock = new Mock <IObservableProperty>();

            nestedObservableMock.SetupGet(x => x.ObservedType).Returns(typeof(ISimpleInterface));
            nestedObservableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>());
            nestedObservableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>());
            nestedObservableMock.Setup(x => x.TryGetMember(It.IsAny <string>(), out It.Ref <object> .IsAny)).Returns(true);
            var nestedObservable = (object)nestedObservableMock.Object;

            Mock <IObservableProperty> observableMock2 = new Mock <IObservableProperty>();

            observableMock2.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>
            {
                [nameof(IInterfaceWithNestedObservable.NestedObservable)] = nestedObservableMock.Object
            });
            observableMock2.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>());
            observableMock2.Setup(x => x.TryGetMember(It.IsAny <string>(), out nestedObservable)).Returns(true);

            // Act
            var manager  = new RuntimeProxyManager <ISimpleInterface>(observableMock.Object);
            var manager2 = new RuntimeProxyManager <IInterfaceWithNestedObservable>(observableMock2.Object);

            ISimpleInterface impl = manager.Implementation;
            IInterfaceWithNestedObservable impl2 = manager2.Implementation;

            Trace.WriteLine(impl.StringValue);
            Trace.WriteLine(impl2.NestedObservable);

            // Assert
            Assert.AreEqual(impl.GetType(), impl2.NestedObservable.GetType());
        }