[Test] public void GetTargetOnTargetReturnsItself()
 {
     {
         var mock   = MockRepository.GenerateStub <IBar>();
         var target = NotifyPropertyChangeFactory.GetTarget(mock);
         Assert.That(target, Is.SameAs(mock));
     }
     {
         var mock   = MockRepository.GenerateStub <IEnumerator <IBar> >();
         var target = NotifyPropertyChangeFactory.GetTarget(mock);
         Assert.That(target, Is.SameAs(mock));
     }
     {
         var mock   = MockRepository.GenerateStub <IEnumerable <IBar> >();
         var target = NotifyPropertyChangeFactory.GetTarget(mock);
         Assert.That(target, Is.SameAs(mock));
     }
     {
         var mock   = MockRepository.GenerateStub <ICollection <IBar> >();
         var target = NotifyPropertyChangeFactory.GetTarget(mock);
         Assert.That(target, Is.SameAs(mock));
     }
     {
         var mock   = MockRepository.GenerateStub <IList <IBar> >();
         var target = NotifyPropertyChangeFactory.GetTarget(mock);
         Assert.That(target, Is.SameAs(mock));
     }
     {
         var mock   = MockRepository.GenerateStub <IDictionary <int, IBar> >();
         var target = NotifyPropertyChangeFactory.GetTarget(mock);
         Assert.That(target, Is.SameAs(mock));
     }
 }
        [Test] public void ProxyImplementsTargetProperty()
        {
            var mock  = MockRepository.GenerateStub <IBigMess>();
            var proxy = NotifyPropertyChangeFactory.GetProxy(mock);

            Assert.That(proxy, Is.InstanceOf <BigMessBase>());
            Assert.That(((BigMessBase)proxy).Target, Is.SameAs(mock));
        }
        [Test] public void GetProxyReturnsSameInstance()
        {
            var mock   = MockRepository.GenerateStub <IBar>();
            var proxy1 = NotifyPropertyChangeFactory.GetProxy(mock);
            var proxy2 = NotifyPropertyChangeFactory.GetProxy(mock);

            Assert.That(proxy2, Is.SameAs(proxy1));
        }
 [Test] public void GetTargetGetsNullOnNullProxy()
 {
     Assert.That(NotifyPropertyChangeFactory.GetTarget((IFoo)null), Is.Null);
     Assert.That(NotifyPropertyChangeFactory.GetTarget((IEnumerator <IFoo>)null), Is.Null);
     Assert.That(NotifyPropertyChangeFactory.GetTarget((IEnumerable <IFoo>)null), Is.Null);
     Assert.That(NotifyPropertyChangeFactory.GetTarget((ICollection <IFoo>)null), Is.Null);
     Assert.That(NotifyPropertyChangeFactory.GetTarget((IList <IFoo>)null), Is.Null);
     Assert.That(NotifyPropertyChangeFactory.GetTarget((IDictionary <int, IFoo>)null), Is.Null);
 }
 [Test] public void SetBaseTypeChangesTheBaseTypeOfGeneratedProxy()
 {
     Factory.Reset(true);
     NotifyPropertyChangeFactory.SetBaseType <GoodBaseDifferentRaiser>("FirePropertyChanged");
     VerifyProxyIsBaseType <GoodBaseDifferentRaiser>();
     Factory.Reset(true);
     NotifyPropertyChangeFactory.SetBaseType <GoodBase>();
     VerifyProxyIsBaseType <GoodBase>();
 }
        [Test] public void SetBaseTypeChokesONonExistentOnPropertyChangedMethod()
        {
            var e = Assert.Throws <ArgumentException>(NotifyPropertyChangeFactory.SetBaseType <NoOnPropertyChangedMethod>);

            Assert.That(e.Message, Is.StringContaining(NotifyPropertyChangeFactory.DefaultOnPropertyChangedMethodName));
            const string expected = "Anything";

            e = Assert.Throws <ArgumentException>(() => NotifyPropertyChangeFactory.SetBaseType <GoodBase>(expected));
            Assert.That(e.Message, Is.StringContaining(expected));
        }
        [Test] public void ProxyImplementsGenericTargetProperty()
        {
            Factory.Reset(true);
            NotifyPropertyChangeFactory.SetBaseType(typeof(NotifyPropertyChangeBase <>));
            var mock  = MockRepository.GenerateStub <IFoo>();
            var proxy = NotifyPropertyChangeFactory.GetProxy(mock);

            Assert.That(proxy, Is.InstanceOf <NotifyPropertyChangeBase <IFoo> >());
            Assert.That(((NotifyPropertyChangeBase <IFoo>)proxy).Target, Is.SameAs(mock));
        }
        [Test] public void ProxyOverridesAbstractMemberAndUtilizeImplementationInBase()
        {
            Factory.Reset(true);
            NotifyPropertyChangeFactory.SetMarkingAttribute <MarkingAttribute>(a => a.BaseType);
            var mock  = MockRepository.GenerateStub <IBigMess>();
            var proxy = NotifyPropertyChangeFactory.GetProxy(mock);

            Assert.That(proxy, Is.InstanceOf <AbstractBigMess>());
            Assert.That(((BigMessBase)proxy).Target, Is.SameAs(mock));
        }
        private static void VerifyProxyIsBaseType <T>()
            where T : INotifyPropertyChanged
        {
            IFoo proxy = NotifyPropertyChangeFactory.GetProxy(MockRepository.GenerateStub <IFoo>());

            Assert.That(proxy, Is.InstanceOf <T>());
            var b       = (T)proxy;
            var handler = MockRepository.GenerateStub <PropertyChangedEventHandler>();

            b.PropertyChanged += handler;
            proxy.IntPropery   = 100;
            handler.AssertWasCalled(x => x(Arg <object> .Is.Same(proxy),
                                           Arg <PropertyChangedEventArgs> .Matches(e => e.PropertyName == "IntPropery")));
        }
        [Test] public void SetMarkingAttributeChangesMarkingAttribute()
        {
            Factory.Reset(true);
            NotifyPropertyChangeFactory.SetMarkingAttribute <SimpleMarkerAttribute>();
            var foo      = MockRepository.GenerateStub <IFoo>();
            var koo      = MockRepository.GenerateStub <IKoo>();
            var bar      = MockRepository.GenerateStub <IBar>();
            var barProxy = NotifyPropertyChangeFactory.GetProxy(bar);

            barProxy.FooProperty = foo;
            barProxy.KooProperty = koo;
            Assert.That(barProxy, Is.InstanceOf <NotifyPropertyChangeBase>());
            Assert.That(barProxy.FooProperty, Is.SameAs(foo));
            Assert.That(barProxy.KooProperty, Is.InstanceOf <NotifyPropertyChangeBase>());
        }
        [Test] public void SetEventRaiserAttributeAllowsChangingRaiser()
        {
            Factory.Reset(true);
            NotifyPropertyChangeFactory.SetBaseType <GoodBase>();
            NotifyPropertyChangeFactory.SetEventRaiserAttribute <RaiserAttribute>(a => a.OnPropertyChanged);
            var handler  = MockRepository.GenerateStub <PropertyChangedEventHandler>();
            var bar      = MockRepository.GenerateStub <IBar>();
            var barProxy = NotifyPropertyChangeFactory.GetProxy(bar);

            Assert.That(barProxy, Is.InstanceOf <GoodBase>());
            ((INotifyPropertyChanged)barProxy).PropertyChanged += handler;
            barProxy.LongProperty = 100;
            handler.AssertWasCalled(x => x(Arg <object> .Is.Same(barProxy),
                                           Arg <PropertyChangedEventArgs> .Matches(e => e.PropertyName == "LongProperty Raised")));
        }
        [Test] public void NewProxyReturnsNewInstance()
        {
            var handler1  = MockRepository.GenerateStub <PropertyChangedEventHandler>();
            var handler2  = MockRepository.GenerateStub <PropertyChangedEventHandler>();
            var bar       = MockRepository.GenerateStub <IBar>();
            var barProxy1 = NotifyPropertyChangeFactory.NewProxy(bar);
            var barProxy2 = NotifyPropertyChangeFactory.NewProxy(bar);

            Assert.That(barProxy2, Is.Not.SameAs(barProxy1));
            ((INotifyPropertyChanged)barProxy1).PropertyChanged += handler1;
            ((INotifyPropertyChanged)barProxy2).PropertyChanged += handler2;
            barProxy1.LongProperty = 100;
            handler1.AssertWasCalled(x => x(Arg <object> .Is.Same(barProxy1),
                                            Arg <PropertyChangedEventArgs> .Matches(e => e.PropertyName == "LongProperty")));
            handler2.AssertWasNotCalled(x => x(Arg <object> .Is.Same(barProxy2),
                                               Arg <PropertyChangedEventArgs> .Matches(e => e.PropertyName == "LongProperty")));
        }
        [Test] public void ProxyRaisesEventAndSetsNewValueToTarget()
        {
            var barMock = MockRepository.GenerateStub <IBar>();
            var kooMock = MockRepository.GenerateStub <IKoo>();
            var proxy   = NotifyPropertyChangeFactory.GetProxy(barMock);
            var handler = MockRepository.GenerateStub <PropertyChangedEventHandler>();

            ((INotifyPropertyChanged)proxy).PropertyChanged += handler;
            const long expected = 9385;

            proxy.LongProperty = expected;
            proxy.KooProperty  = kooMock;
            Assert.That(barMock.LongProperty, Is.EqualTo(expected));
            Assert.That(barMock.KooProperty, Is.SameAs(kooMock));
            handler.AssertWasCalled(x => x(Arg <object> .Is.Same(proxy),
                                           Arg <PropertyChangedEventArgs> .Matches(e => e.PropertyName == "LongProperty")));
            handler.AssertWasCalled(x => x(Arg <object> .Is.Same(proxy),
                                           Arg <PropertyChangedEventArgs> .Matches(e => e.PropertyName == "KooProperty")));
        }
        [Test] public void ProxyRaisesNoEventWhenSameValueWasSet()
        {
            const long expected = 9385;
            var        barMock  = MockRepository.GenerateStub <IBar>();
            var        kooMock  = MockRepository.GenerateStub <IKoo>();

            barMock.LongProperty = expected;
            barMock.KooProperty  = kooMock;
            var proxy   = NotifyPropertyChangeFactory.GetProxy(barMock);
            var handler = MockRepository.GenerateStub <PropertyChangedEventHandler>();

            ((INotifyPropertyChanged)proxy).PropertyChanged += handler;
            proxy.LongProperty = expected;
            proxy.KooProperty  = kooMock;

            handler.AssertWasNotCalled(x => x(Arg <object> .Is.Same(proxy),
                                              Arg <PropertyChangedEventArgs> .Matches(e => e.PropertyName == "LongProperty")));
            handler.AssertWasNotCalled(x => x(Arg <object> .Is.Same(proxy),
                                              Arg <PropertyChangedEventArgs> .Matches(e => e.PropertyName == "KooProperty")));
        }
 [Test] public void GetProxyOnProxyReturnsItself()
 {
     {
         var mock   = MockRepository.GenerateStub <IBar>();
         var proxy1 = NotifyPropertyChangeFactory.GetProxy(mock);
         var proxy2 = NotifyPropertyChangeFactory.GetProxy(proxy1);
         Assert.That(proxy2, Is.SameAs(proxy1));
     }
     {
         var mock   = MockRepository.GenerateStub <IEnumerator <IBar> >();
         var proxy1 = NotifyPropertyChangeFactory.GetProxy(mock);
         var proxy2 = NotifyPropertyChangeFactory.GetProxy(proxy1);
         Assert.That(proxy2, Is.SameAs(proxy1));
     }
     {
         var mock   = MockRepository.GenerateStub <IEnumerable <IBar> >();
         var proxy1 = NotifyPropertyChangeFactory.GetProxy(mock);
         var proxy2 = NotifyPropertyChangeFactory.GetProxy(proxy1);
         Assert.That(proxy2, Is.SameAs(proxy1));
     }
     {
         var mock   = MockRepository.GenerateStub <ICollection <IBar> >();
         var proxy1 = NotifyPropertyChangeFactory.GetProxy(mock);
         var proxy2 = NotifyPropertyChangeFactory.GetProxy(proxy1);
         Assert.That(proxy2, Is.SameAs(proxy1));
     }
     {
         var mock   = MockRepository.GenerateStub <IList <IBar> >();
         var proxy1 = NotifyPropertyChangeFactory.GetProxy(mock);
         var proxy2 = NotifyPropertyChangeFactory.GetProxy(proxy1);
         Assert.That(proxy2, Is.SameAs(proxy1));
     }
     {
         var mock   = MockRepository.GenerateStub <IDictionary <int, IBar> >();
         var proxy1 = NotifyPropertyChangeFactory.GetProxy(mock);
         var proxy2 = NotifyPropertyChangeFactory.GetProxy(proxy1);
         Assert.That(proxy2, Is.SameAs(proxy1));
     }
 }
        [Test] public void SetMarkingAttributeChangesMarkingAttributeAndAllowsDifferentBaseType()
        {
            Factory.Reset(true);
            NotifyPropertyChangeFactory.SetMarkingAttribute <MarkingAttribute>(a => a.BaseType);
            var handler  = MockRepository.GenerateStub <PropertyChangedEventHandler>();
            var foo      = MockRepository.GenerateStub <IFoo>();
            var koo      = MockRepository.GenerateStub <IKoo>();
            var bar      = MockRepository.GenerateStub <IBar>();
            var barProxy = NotifyPropertyChangeFactory.GetProxy(bar);

            barProxy.FooProperty = foo;
            barProxy.KooProperty = koo;
            Assert.That(barProxy, Is.InstanceOf <GoodBase>());
            Assert.That(barProxy.KooProperty, Is.SameAs(koo));
            var fooProxy = barProxy.FooProperty;

            Assert.That(fooProxy, Is.InstanceOf <GoodBaseDifferentRaiser>());
            ((INotifyPropertyChanged)fooProxy).PropertyChanged += handler;
            fooProxy.IntPropery = 100;
            handler.AssertWasCalled(x => x(Arg <object> .Is.Same(fooProxy),
                                           Arg <PropertyChangedEventArgs> .Matches(e => e.PropertyName == "IntPropery Raised")));
        }
        [Test] public void SetBaseTypeChokesOnNullOnPropertyChangedMethod()
        {
            var e = Assert.Throws <ArgumentNullException>(() => NotifyPropertyChangeFactory.SetBaseType <GoodBase>(null));

            Assert.That(e.ParamName, Is.EqualTo("onPropertyChangedMethod"));
        }
 [Test] public void SetBaseTypeChokesWhenFactoryAlreadyInitialized()
 {
     NotifyPropertyChangeFactory.GetProxy(MockRepository.GenerateStub <IFoo>());
     Assert.Throws <InvalidOperationException>(NotifyPropertyChangeFactory.SetBaseType <GoodBase>);
 }
        [Test] public void SetBaseTypeChokesOnMoreThenOneOpenGenericTypeParameters()
        {
            var e = Assert.Throws <ArgumentException>(() => NotifyPropertyChangeFactory.SetBaseType(typeof(NotifyPropertyChangeBase <,>)));

            Assert.That(e.ParamName, Is.EqualTo("baseType"));
        }
 [Test] public void NewProxyGetsNullOnNullTarget()
 {
     Assert.That(NotifyPropertyChangeFactory.NewProxy((IFoo)null), Is.Null);
 }
 [Test] public void SetEventRaiserAttributeChokesOnNullConverter()
 {
     Assert.Throws <ArgumentNullException>(
         () => NotifyPropertyChangeFactory.SetEventRaiserAttribute <RaiserAttribute>(null));
 }
        [Test] public void SetBaseTypeChokesOnTypeDoesNotImplementINotifyPropertyChanged()
        {
            var e = Assert.Throws <ArgumentException>(() => NotifyPropertyChangeFactory.SetBaseType(typeof(NotNotifyPropertyChanged), ""));

            Assert.That(e.ParamName, Is.EqualTo("baseType"));
        }
        [Test] public void SetBaseTypeChokesOnNullBaseType()
        {
            var e = Assert.Throws <ArgumentNullException>(() => NotifyPropertyChangeFactory.SetBaseType(null, ""));

            Assert.That(e.ParamName, Is.EqualTo("baseType"));
        }