Пример #1
0
		void checkReturnTypeIsValid(MethodSignature signature, object returnVal)
		{
			if (returnVal == null)
			{
				return;
			}

			Type realReturnVal;
			MethodInfo method;
			PropertyInfo property;
			FindMethodOrProperty(type, signature, out method, out property);
			if (method == null)
			{
				if (property == null)
					throw new ArgumentException(string.Format("Method/property <{0}> not found", signature.methodName));
				else
					realReturnVal = property.PropertyType;
			}
			else
				realReturnVal = method.ReturnType;


			if (realReturnVal == null)
			{
				realReturnVal = GetPropertyHelper(type, signature).PropertyType;
			}

			if (realReturnVal != returnVal.GetType() && !realReturnVal.IsAssignableFrom(returnVal.GetType())
				&& !realReturnVal.IsInstanceOfType(returnVal))
			{
				throw new ArgumentException(String.Format("method <{0}> returns a {1}", signature.methodName, realReturnVal));
			}
		}
Пример #2
0
		public override void SetupResult(string methodName, object returnVal, params Type[] argTypes)
		{
			MethodSignature signature = new MethodSignature(Name, methodName, argTypes);
			checkMethodIsValidIfNoConstraints(signature);
			checkReturnTypeIsValid(signature, returnVal);
			base.SetupResult(methodName, returnVal, argTypes);
		}
Пример #3
0
		public MockCall(MethodSignature signature, object returnValue, Exception e, object[] expectedArgs,
			object[] returnArgs)
		{
			this.signature = signature;
			this.returnValue = returnValue;
			this.e = e;
			this.expectedArgs = argsAsConstraints(expectedArgs);
			this.returnArgs = returnArgs;
		}
Пример #4
0
		private PropertyInfo GetPropertyHelper(Type type, MethodSignature signature)
		{
			MemberInfo[] properties = type.FindMembers(MemberTypes.Property,
				ClassGenerator.ALL_INSTANCE_METHODS,
				new MemberFilter(PropertyFilter), signature);

			if (properties != null && properties.Length == 1)
				return properties[0] as PropertyInfo;

			return null;
		}
Пример #5
0
		private MethodInfo GetMethodHelper(Type type, MethodSignature signature)
		{
			MemberInfo[] methods = type.FindMembers(MemberTypes.Method,
				ClassGenerator.ALL_INSTANCE_METHODS,
				new MemberFilter(MethodFilter), signature);

			if (methods != null && methods.Length == 1)
				return methods[0] as MethodInfo;

			return null;
		}
Пример #6
0
		void checkMethodIsValidIfNoConstraints(MethodSignature signature)
		{
			Type[] allTypes = new InterfaceLister().List(type);
			foreach (Type t in allTypes)
			{
				MethodInfo method;
				PropertyInfo property;
				FindMethodOrProperty(t, signature, out method, out property);
				if(method != null)
				{
					if(!method.IsVirtual)
					{
						string message;
						if (property != null)
							message = string.Format("Property <{0}> is not virtual", signature.methodName);
						else
							message = string.Format("Method <{0}> is not virtual", signature.methodName);
						throw new ArgumentException(message);
					}
					return;
				}
			}

			foreach(string argTypeStr in signature.argumentTypes)
			{
				if(typeof(IConstraint).IsAssignableFrom(Type.GetType(argTypeStr)))
					return;
			}

			throw new MissingMethodException(String.Format("method <{0}> not defined", signature.methodName));
		}
Пример #7
0
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Adds a method without expectations
		/// </summary>
		/// <param name="signature">Signature of the method</param>
		/// <param name="newMethod">Method to add if none exists yet</param>
		/// <param name="returnVal">The value that should be returned when the method is called
		/// </param>
		/// <param name="returnParams">The values of the parameters set on return of the method
		/// </param>
		/// <param name="inParams">Input parameters</param>
		/// ------------------------------------------------------------------------------------
		private void AddMethodWithoutExpecations(MethodSignature signature, Type methodType,
			object returnVal, object[] returnParams, object[] inParams)
		{
			IMethod method = getMethod(signature);
			if (method == null || method.GetType() != methodType)
			{
				ConstructorInfo ctor = methodType.GetConstructor(
					new Type[] { typeof(MethodSignature) });
				method = (IMethod)ctor.Invoke(new object[] { signature });
				methods[signature.methodName] = method;
			}
			method.SetExpectation(new MockCall(signature, returnVal, null, inParams,
				returnParams));
		}
Пример #8
0
		public MockCall(MethodSignature signature, object returnValue, Exception e, object[] expectedArgs)
			: this(signature, returnValue, e, expectedArgs, null)
		{
		}
Пример #9
0
 public MockCall(MethodSignature signature, object returnValue, Exception e, object[] expectedArgs)
     : this(signature, returnValue, e, expectedArgs, null)
 {
 }
Пример #10
0
 public MockNoCall(MethodSignature signature) : base(signature, null, null, null)
 {
 }
Пример #11
0
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Constructs a new object of type <see cref="CallMethodOrder"/>
 /// </summary>
 /// <param name="signature"></param>
 /// ------------------------------------------------------------------------------------
 public CallMethodOrder(MethodSignature signature)
     : base(signature)
 {
 }
Пример #12
0
		protected virtual IMethod getMethod(MethodSignature signature)
		{
			return (IMethod)methods[signature.methodName];
		}
Пример #13
0
		public virtual object Invoke(string methodName, object[] args, string[] typeNames)
		{
			if ((methodName.StartsWith("get_") && args.Length == 0) ||
				(methodName == "get_Item" && args.Length == 1))
			{
				methodName = methodName.Substring(4); // without get_
			}
			else if (methodName.StartsWith("set_") && args.Length == 1)
				methodName = methodName.Substring(4); // without set_

			MethodSignature signature = new MethodSignature(Name, methodName, typeNames);
			IMethod method = getMethod(signature);
			if (method == null)
			{
				if (strict)
				{
					throw new VerifyException(methodName + "() called too many times", 0, 1);
				}
				return null;
			}
			return method.Call(args);
		}
Пример #14
0
		protected void addExpectation(string methodName, MockCall call)
		{
			MethodSignature signature = new MethodSignature(Name, methodName, call.ArgTypes);
			IMethod method = getMethod(signature);
			if (method == null)
			{
				method = new Method(signature);
				methods[methodName] = method;
			}
			method.SetExpectation(call);
		}
Пример #15
0
		private void FindMethodOrProperty(Type t, MethodSignature signature,
			out MethodInfo method, out PropertyInfo property)
		{
			method = GetMethodHelper(t, signature);
			property = GetPropertyHelper(t, signature);
			if (property != null)
			{
				method = null;
				if (property.CanRead)
				{
					method = t.GetMethod("get_" + signature.methodName,
						ClassGenerator.ALL_INSTANCE_METHODS);
				}
				if (property.CanWrite && method == null)
				{
					method = t.GetMethod("set_" + signature.methodName,
						ClassGenerator.ALL_INSTANCE_METHODS);
				}
			}
			else if (method == null)
			{
				if (signature.argumentTypes.Length == 1)
				{
					// try to find a set_ method
					MethodSignature tmpSignature = new MethodSignature(signature.typeName,
						"set_" + signature.methodName, signature.argumentTypes);
					method = GetMethodHelper(t, tmpSignature);
				}
				else
				{
					// try to find a get_ method
					MethodSignature tmpSignature = new MethodSignature(signature.typeName,
						"get_" + signature.methodName, signature.argumentTypes);
					method = GetMethodHelper(t, tmpSignature);
				}
			}
		}
Пример #16
0
		protected override IMethod getMethod(MethodSignature signature)
		{
			checkMethodIsValidIfNoConstraints(signature);

			return base.getMethod(signature);
		}
		public CallMethodWithoutExpectation(MethodSignature signature)
		{
			this.name = signature.methodName;
		}
Пример #18
0
 protected virtual IMethod getMethod(MethodSignature signature)
 {
     return((IMethod)methods[signature.methodName]);
 }
Пример #19
0
		public Method(MethodSignature signature)
		{
			this.signature = signature;
			expectations = new CallSequence(signature.methodName);
		}
Пример #20
0
		public MockNoCall(MethodSignature signature) : base(signature, null, null, null)
		{
		}
Пример #21
0
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Constructs a new object of type <see cref="CallMethodOrder"/>
		/// </summary>
		/// <param name="signature"></param>
		/// ------------------------------------------------------------------------------------
		public CallMethodOrder(MethodSignature signature)
			: base(signature)
		{
		}
Пример #22
0
		/// -----------------------------------------------------------------------------------
		/// <summary>
		/// Initializes a new instance of the <see cref="CallMethodWithParams"/> class.
		/// </summary>
		/// -----------------------------------------------------------------------------------
		public CallMethodWithParams(MethodSignature signature)
			: base(signature)
		{
		}
Пример #23
0
 public Method(MethodSignature signature)
 {
     this.signature = signature;
     expectations   = new CallSequence(signature.methodName);
 }