public void Cannot_mixin_several_delegate_types_with_same_signature() { var options = new ProxyGenerationOptions(); options.AddDelegateTypeMixin(typeof(Func <Exception, bool>)); options.AddDelegateTypeMixin(typeof(Predicate <Exception>)); Assert.Throws <InvalidMixinConfigurationException>(() => options.Initialize()); }
/// <inheritdoc /> public override object CreateProxy(Type mockType, Moq.IInterceptor interceptor, Type[] interfaces, object[] arguments) { // All generated proxies need to implement `IProxy`: var additionalInterfaces = new Type[1 + interfaces.Length]; additionalInterfaces[0] = typeof(IProxy); Array.Copy(interfaces, 0, additionalInterfaces, 1, interfaces.Length); if (mockType.IsInterface) { // While `CreateClassProxy` could also be used for interface types, // `CreateInterfaceProxyWithoutTarget` is much faster (about twice as fast): return(generator.CreateInterfaceProxyWithoutTarget(mockType, additionalInterfaces, this.generationOptions, new Interceptor(interceptor))); } else if (mockType.IsDelegateType()) { var options = new ProxyGenerationOptions(); options.AddDelegateTypeMixin(mockType); var container = generator.CreateClassProxy(typeof(object), additionalInterfaces, options, new Interceptor(interceptor)); return(Delegate.CreateDelegate(mockType, container, container.GetType().GetMethod("Invoke"))); } try { return(generator.CreateClassProxy(mockType, additionalInterfaces, this.generationOptions, arguments, new Interceptor(interceptor))); } catch (TypeLoadException e) { throw new ArgumentException(Resources.TypeNotMockable, e); } catch (MissingMethodException e) { throw new ArgumentException(Resources.ConstructorNotFound, e); } }
/// <inheritdoc /> public object CreateStunt(Assembly stuntsAssembly, Type baseType, Type[] implementedInterfaces, object[] constructorArguments) { var notImplemented = false; if (baseType.IsInterface) { var fixedInterfaces = new Type[implementedInterfaces.Length + 1]; fixedInterfaces[0] = baseType; implementedInterfaces.CopyTo(fixedInterfaces, 1); implementedInterfaces = fixedInterfaces; baseType = typeof(object); notImplemented = true; } if (!implementedInterfaces.Contains(typeof(IStunt))) { var fixedInterfaces = new Type[implementedInterfaces.Length + 1]; fixedInterfaces[0] = typeof(IStunt); implementedInterfaces.CopyTo(fixedInterfaces, 1); implementedInterfaces = fixedInterfaces; } if (baseType.BaseType == typeof(MulticastDelegate)) { var mixinOptions = new ProxyGenerationOptions(); mixinOptions.AddDelegateTypeMixin(baseType); var proxy = CreateProxy(typeof(object), implementedInterfaces, mixinOptions, Array.Empty <object>(), notImplemented); return(Delegate.CreateDelegate(baseType, proxy, proxy.GetType().GetMethod("Invoke"))); } else { return(CreateProxy(baseType, implementedInterfaces, options, constructorArguments, notImplemented)); } }
private Type GenerateProxyType <TDelegate>() { var options = new ProxyGenerationOptions(); options.AddDelegateTypeMixin(typeof(TDelegate)); return(generator.ProxyBuilder.CreateClassProxyType(typeof(object), null, options)); }
public void ProxyGenerator_CreateInterfaceProxyWithoutTarget_can_create_delegate_proxy_without_target() { var options = new ProxyGenerationOptions(); options.AddDelegateTypeMixin(typeof(Action)); var _ = new Interceptor(); var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IComparable), options, _); }
public void ProxyGenerator_CreateClassProxy_can_create_delegate_proxy_without_target() { var options = new ProxyGenerationOptions(); options.AddDelegateTypeMixin(typeof(Action)); var _ = new Interceptor(); var proxy = generator.CreateClassProxy(typeof(object), options, _); }
public void Can_mixin_several_different_delegate_types_simultaneously() { var options = new ProxyGenerationOptions(); options.AddDelegateTypeMixin(typeof(Action)); options.AddDelegateTypeMixin(typeof(Action <int>)); var interceptor = new Interceptor(); var proxy = generator.CreateClassProxy(typeof(object), options, interceptor); var action = ProxyUtil.CreateDelegateToMixin <Action>(proxy); Assert.NotNull(action); action.Invoke(); var intAction = ProxyUtil.CreateDelegateToMixin <Action <int> >(proxy); Assert.NotNull(action); intAction.Invoke(42); }
public void ProxyGenerator_CreateInterfaceProxyWithoutTarget_cannot_proceed_to_delegate_type_mixin() { var options = new ProxyGenerationOptions(); options.AddDelegateTypeMixin(typeof(Action)); var interceptor = new Interceptor(shouldProceed: true); var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IComparable), options, interceptor); var action = ProxyUtil.CreateDelegateToMixin <Action>(proxy); Assert.NotNull(action); Assert.Throws <NotImplementedException>(() => action.Invoke()); }
public void ProxyGenerator_CreateClassProxy_can_create_callable_delegate_proxy_without_target() { var options = new ProxyGenerationOptions(); options.AddDelegateTypeMixin(typeof(Action)); var interceptor = new Interceptor(); var proxy = generator.CreateClassProxy(typeof(object), options, interceptor); var action = ProxyUtil.CreateDelegateToMixin <Action>(proxy); Assert.NotNull(action); action.Invoke(); Assert.AreSame(typeof(Action).GetMethod("Invoke"), interceptor.LastInvocation.Method); }
public void ProxyGenerationOptions_AddDelegateTypeMixin_when_given_delegate_type_succeeds() { var options = new ProxyGenerationOptions(); options.AddDelegateTypeMixin(typeof(Action)); }
public void ProxyGenerationOptions_AddDelegateTypeMixin_when_given_non_delegate_type_throws_ArgumentException() { var options = new ProxyGenerationOptions(); Assert.Throws <ArgumentException>(() => options.AddDelegateTypeMixin(typeof(Exception))); }
public void ProxyGenerationOptions_AddDelegateTypeMixin_when_given_null_throws_ArgumentNullException() { var options = new ProxyGenerationOptions(); Assert.Throws <ArgumentNullException>(() => options.AddDelegateTypeMixin(null)); }