public void SupplyingOpenGenericInterfacesAsAdditionalInterfacesThrows() { // arrange IInstanceInterceptor interceptor = new InterfaceInterceptor(); HasSomeProperties target = new HasSomeProperties(); // act Exception exception = null; try { object proxy = interceptor.CreateProxy( typeof(IHaveSomeProperties), target, typeof(IGenericOne <>)); } catch (ArgumentException e) { exception = e; } // assert Assert.IsNotNull(exception); }
public void SupplyingANullArrayOfAdditionalInterfacesThrows() { // arrange IInstanceInterceptor interceptor = new InterfaceInterceptor(); HasSomeProperties target = new HasSomeProperties(); // act Exception exception = null; try { object proxy = interceptor.CreateProxy( typeof(IHaveSomeProperties), target, (Type[])null); } catch (ArgumentNullException e) { exception = e; } // assert Assert.IsNotNull(exception); }
[Ignore] // TODO: Known issue - we don't copy attributes yet public void ProxyPropertyReflectionReturnsAttributes() { IInstanceInterceptor interceptor = new InterfaceInterceptor(); HasSomeProperties target = new HasSomeProperties(); IInterceptingProxy proxy = interceptor.CreateProxy(typeof(IHaveSomeProperties), target); PropertyInfo nameProp = proxy.GetType().GetProperty("Name"); SampleAttribute attr = (SampleAttribute)Attribute.GetCustomAttribute(nameProp, typeof(SampleAttribute)); Assert.AreEqual("A name", attr.Name); }
public void ReflectingOverProxyTypeReturnsProperties() { IInstanceInterceptor interceptor = new InterfaceInterceptor(); HasSomeProperties target = new HasSomeProperties(); IInterceptingProxy proxy = interceptor.CreateProxy(typeof(IHaveSomeProperties), target); List <PropertyInfo> interfaceProperties = new List <PropertyInfo>(typeof(IHaveSomeProperties).GetProperties()); List <PropertyInfo> proxyProperties = new List <PropertyInfo>(proxy.GetType().GetProperties()); Assert.AreEqual(interfaceProperties.Count, proxyProperties.Count); }
public void MultipleProxiesForTheSameInterfaceHaveTheSameType() { // arrange IInstanceInterceptor interceptor = new InterfaceInterceptor(); HasSomeProperties target = new HasSomeProperties(); // act object proxy1 = interceptor.CreateProxy(typeof(IHaveSomeProperties), target); object proxy2 = interceptor.CreateProxy(typeof(IHaveSomeProperties), target); // assert Assert.AreSame(proxy1.GetType(), proxy2.GetType()); }
public void AdditionalInterfaceIsImplementedExplicitly() { // arrange IInstanceInterceptor interceptor = new InterfaceInterceptor(); HasSomeProperties target = new HasSomeProperties(); // act object proxy = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(IInterfaceTwo)); InterfaceMapping interfaceMapping = proxy.GetType().GetInterfaceMap(typeof(IInterfaceTwo)); // assert Assert.IsNull(interfaceMapping.TargetMethods.FirstOrDefault(mi => mi.IsPublic)); }
public void CanCreateProxyForMultipleInterfaces() { // arrange IInstanceInterceptor interceptor = new InterfaceInterceptor(); HasSomeProperties target = new HasSomeProperties(); // act object proxy = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(IInterfaceOne)); // assert Assert.IsTrue(proxy is IHaveSomeProperties); Assert.IsTrue(proxy is IInterfaceOne); }
public void ProxiesForTheSameInterfaceButDifferentAdditionalInterfacesDoNotHaveTheSameType() { // arrange IInstanceInterceptor interceptor = new InterfaceInterceptor(); HasSomeProperties target = new HasSomeProperties(); // act object proxy1 = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(IBaseInterface)); object proxy2 = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(IBaseInterface2)); // assert Assert.AreNotSame(proxy1.GetType(), proxy2.GetType()); Assert.IsTrue(proxy1 is IBaseInterface); Assert.IsTrue(proxy2 is IBaseInterface2); }
public void CanImplementAdditionalClosedGenericInterface() { // arrange IInstanceInterceptor interceptor = new InterfaceInterceptor(); HasSomeProperties target = new HasSomeProperties(); // act object proxy = interceptor.CreateProxy( typeof(IHaveSomeProperties), target, typeof(IGenericOne <string>)); // assert Assert.IsTrue(proxy is IHaveSomeProperties); Assert.IsTrue(proxy is IGenericOne <string>); }
public void CanSuccessfullyInvokeAnAdditionalInterfaceMethodIfAnInterceptorDoesNotForwardTheCall() { // arrange IInstanceInterceptor interceptor = new InterfaceInterceptor(); HasSomeProperties target = new HasSomeProperties(); object proxy = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(IInterfaceOne)); bool invoked = false; ((IInterceptingProxy)proxy).AddInterceptionBehavior( new DelegateInterceptionBehavior( (input, getNext) => { invoked = true; return(input.CreateMethodReturn(null)); })); // act ((IInterfaceOne)proxy).TargetMethod(); // assert Assert.IsTrue(invoked); }
public void CanImplementMultipleAdditionalInterfacesWithCommonAncestors() { // arrange IInstanceInterceptor interceptor = new InterfaceInterceptor(); HasSomeProperties target = new HasSomeProperties(); // act object proxy = interceptor.CreateProxy( typeof(IHaveSomeProperties), target, typeof(IDerivedInterface1), typeof(IDerivedInterface2)); // assert Assert.IsTrue(proxy is IHaveSomeProperties); Assert.IsTrue(proxy is IDerivedInterface1); Assert.IsTrue(proxy is IDerivedInterface2); }
public void InvokingMethodOnAdditionalInterfaceThrowsIfNotHandledByInterceptor() { // arrange IInstanceInterceptor interceptor = new InterfaceInterceptor(); HasSomeProperties target = new HasSomeProperties(); object proxy = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(IInterfaceOne)); // act Exception exception = null; try { ((IInterfaceOne)proxy).TargetMethod(); Assert.Fail("should have thrown"); } catch (NotImplementedException e) { exception = e; } // assert Assert.IsNotNull(exception); }
public void CanImplementINotifyPropertyChanged() { IInstanceInterceptor interceptor = new InterfaceInterceptor(); HasSomeProperties target = new HasSomeProperties(); object proxy = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(INotifyPropertyChanged)); string changeProperty = null; PropertyChangedEventHandler handler = (sender, args) => changeProperty = args.PropertyName; ((IInterceptingProxy)proxy).AddInterceptionBehavior(new NaiveINotifyPropertyChangedInterceptionBehavior()); ((INotifyPropertyChanged)proxy).PropertyChanged += handler; ((IHaveSomeProperties)proxy).IntProperty = 100; Assert.AreEqual(100, ((IHaveSomeProperties)proxy).IntProperty); Assert.AreEqual("IntProperty", changeProperty); changeProperty = null; ((INotifyPropertyChanged)proxy).PropertyChanged -= handler; ((IHaveSomeProperties)proxy).IntProperty = 200; Assert.AreEqual(200, ((IHaveSomeProperties)proxy).IntProperty); Assert.AreEqual(null, changeProperty); }