Permalink
Browse files

Improved support for properties and added a few more tests.

  • Loading branch information...
1 parent 3958475 commit 1e928aacc7bb2e26cca5d677230cf3014293bc3d @martydill committed Dec 9, 2011
@@ -41,7 +41,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
- <Compile Include="MethodCountTest.cs" />
+ <Compile Include="MirrorCountTest.cs" />
<Compile Include="MirrorThrowsTest.cs" />
<Compile Include="MirrorCallsTest.cs" />
<Compile Include="MirrorReturnsTest.cs" />
@@ -9,6 +9,8 @@ public class MirrorCallsTest
interface ITest
{
void DoStuff();
+
+ string Name { get; set; }
}
@@ -32,5 +34,18 @@ public void TestCallsWithValidLambdaCallsLambda()
test.It.DoStuff();
Assert.AreEqual(3, counter);
}
+
+
+ [Test]
+ public void TestCallsForPropertyGetter()
+ {
+ var test = new Mirror<ITest>();
+
+ bool called = false;
+ test.Calls(t => t.Name, () => called = true);
+
+ string s = test.It.Name;
+ Assert.IsTrue(called);
+ }
}
}
@@ -1,10 +1,11 @@
using Mirror.Framework;
using NUnit.Framework;
+using System;
namespace Mirror.Tests
{
[TestFixture]
- class MethodCountTest
+ class MirrorCountTest
{
class test
{
@@ -19,6 +20,14 @@ interface ITest
[Test]
+ public void TestCountThrowsExceptionIfLambdaIsNull()
+ {
+ var mock = new Mirror<ITest>();
+ Assert.Throws<ArgumentNullException>(() => mock.Count(null));
+ }
+
+
+ [Test]
public void TestCountReturnsCorrectValueForNoParameterMethod()
{
var mock = new Mirror<ITest>();
@@ -1,5 +1,6 @@
using NUnit.Framework;
using Mirror.Framework;
+using System;
namespace Mirror.Tests
{
@@ -35,6 +36,14 @@ public void TestCallNonArrangedMethodDoesNothing()
[Test]
+ public void TestReturnsNullLambdaThrowsException()
+ {
+ var test = new Mirror<ITest>();
+ Assert.Throws<ArgumentNullException>(() => test.Returns(null, 5));
+ }
+
+
+ [Test]
public void TestCallNonArrangedMethodWithReturnValueReturnsDefault()
{
var test = new Mirror<ITest>();
@@ -122,19 +131,6 @@ public void TestReturnValueForGetter()
}
- [Test]
- public void TestCallsForGetter()
- {
- var test = new Mirror<ITest>();
-
- bool called = false;
- test.Calls(t => t.Name, () => called = true);
-
- string s = test.It.Name;
- Assert.IsTrue(called);
- }
-
-
private int IntReturner()
{
return 12345;
@@ -10,22 +10,54 @@ public class MirrorThrowsTest
interface ITest
{
void DoStuff();
+
+ void DoStuff(int i);
+
+ string Name { get; }
+ }
+
+ [Test]
+ public void TestThrowsThrowsExceptionIfLambdaIsNull()
+ {
+ var test = new Mirror<ITest>();
+ Assert.Throws<ArgumentNullException>(() => test.Throws(null, new Exception()));
}
[Test]
public void TestThrowsThrowsExceptionIfExceptionIsNull()
{
var test = new Mirror<ITest>();
- Assert.Throws<MirrorArrangeException>(() => test.Throws(s => s.DoStuff(), null));
+ Assert.Throws<ArgumentNullException>(() => test.Throws(s => s.DoStuff(), null));
}
[Test]
- public void TestMethodArrangeWithThrows()
+ public void TestThrowsWithNoParameterMethodThrows()
{
var test = new Mirror<ITest>();
test.Throws(s => s.DoStuff(), new Exception());
Assert.Throws<Exception>(() => test.It.DoStuff());
}
+
+ [Test]
+ public void TestThrowsWithParametersThrowsWhenParametersMatch()
+ {
+ var test = new Mirror<ITest>();
+ test.Throws(s => s.DoStuff(5), new Exception());
+
+ Assert.DoesNotThrow(() => test.It.DoStuff());
+ Assert.DoesNotThrow(() => test.It.DoStuff(1));
+ Assert.Throws<Exception>(() => test.It.DoStuff(5));
+ Assert.DoesNotThrow(() => test.It.DoStuff(55));
+ }
+
+ [Test]
+ public void TestThrowsForPropertyThrowsException()
+ {
+ var test = new Mirror<ITest>();
+ test.Throws(s => s.Name, new ArgumentException());
+
+ Assert.Throws<ArgumentException>(() => { var n = test.It.Name; });
+ }
}
}
@@ -24,7 +24,7 @@ internal class ParameterInfo
private readonly List<CallCountInstance> _methodCallCounts = new List<CallCountInstance>();
private readonly List<ParameterInfo> _parameterValues = new List<ParameterInfo>();
- internal void AddReturnValue(object returnValue, object[] parameterValues)
+ internal void AddReturns(object returnValue, object[] parameterValues)
{
_parameterValues.Add(new ParameterInfo() { ReturnValue = returnValue, ParameterValues = parameterValues });
}
@@ -96,12 +96,12 @@ private static object GetValueForParameterValue(object result)
return result;
}
- internal void AddMethodExecution(Action methodToCall, object[] parameterValues)
+ internal void AddCalls(Action methodToCall, object[] parameterValues)
{
_parameterValues.Add(new ParameterInfo() { MethodToCall = methodToCall, ParameterValues = parameterValues });
}
- internal void AddMethodException(Exception exception, object[] ParameterValues)
+ internal void AddThrows(Exception exception, object[] ParameterValues)
{
_parameterValues.Add(new ParameterInfo() { ExceptionToThrow = exception, ParameterValues = ParameterValues });
}
@@ -39,6 +39,9 @@ public TMirroredType It
/// </summary>
public int Count(Expression<Action<TMirroredType>> inputFunc)
{
+ if (inputFunc == null)
+ throw new ArgumentNullException("inputFunc", "inputFunc is null.");
+
var methodCallExpression = inputFunc.Body as MethodCallExpression;
var method = methodCallExpression.Method;
var parameters = methodCallExpression.Arguments;
@@ -51,18 +54,21 @@ public int Count(Expression<Action<TMirroredType>> inputFunc)
/// <summary>
/// Configures the mirror to return the specified value for the specified method or property call
/// </summary>
- public void Returns<TReturnType>(Expression<Func<TMirroredType, TReturnType>> inputFunc, TReturnType returnValue )
+ public void Returns<TReturnType>(Expression<Func<TMirroredType, TReturnType>> inputFunc, TReturnType returnValue)
{
+ if (inputFunc == null)
+ throw new ArgumentNullException("inputFunc", "inputFunc is null.");
+
if (inputFunc.Body is MethodCallExpression)
{
- var methodCallInfo = AddMethod((MethodCallExpression)inputFunc.Body);
- var parameterValues = GetMethodParameters((MethodCallExpression)inputFunc.Body);
- methodCallInfo.AddReturnValue(returnValue, parameterValues);
+ var methodCallInfo = AddMethod((MethodCallExpression)inputFunc.Body);
+ var parameterValues = GetMethodParameters((MethodCallExpression)inputFunc.Body);
+ methodCallInfo.AddReturns(returnValue, parameterValues);
}
else if (inputFunc.Body is MemberExpression)
{
var memberInfo = AddMember((MemberExpression)inputFunc.Body);
- memberInfo.AddReturnValue(returnValue, null);
+ memberInfo.AddReturns(returnValue, null);
}
else
throw new MirrorArrangeException("Unsupported expression type " + inputFunc.Body.GetType().Name);
@@ -83,7 +89,7 @@ public void Calls(Expression<Action<TMirroredType>> inputFunc, Action methodToCa
{
var methodCallInfo = AddMethod((MethodCallExpression)inputFunc.Body);
var parameterValues = GetMethodParameters((MethodCallExpression)inputFunc.Body);
- methodCallInfo.AddMethodExecution(methodToCall, parameterValues);
+ methodCallInfo.AddCalls(methodToCall, parameterValues);
}
else
throw new MirrorArrangeException("Unsupported expression type " + inputFunc.Body.GetType().Name);
@@ -105,7 +111,12 @@ public void Calls<TReturnType>(Expression<Func<TMirroredType, TReturnType>> inpu
{
var methodCallInfo = AddMethod((MethodCallExpression)inputFunc.Body);
var parameterValues = GetMethodParameters((MethodCallExpression)inputFunc.Body);
- methodCallInfo.AddMethodExecution(methodToCall, parameterValues);
+ methodCallInfo.AddCalls(methodToCall, parameterValues);
+ }
+ else if (inputFunc.Body is MemberExpression)
+ {
+ var memberInfo = AddMember((MemberExpression)inputFunc.Body);
+ memberInfo.AddCalls(methodToCall, null);
}
else
throw new MirrorArrangeException("Unsupported expression type " + inputFunc.Body.GetType().Name);
@@ -119,20 +130,49 @@ public void Calls<TReturnType>(Expression<Func<TMirroredType, TReturnType>> inpu
/// <param name="exceptionToThrow">The exception to be thrown</param>
public void Throws(Expression<Action<TMirroredType>> inputFunc, Exception exceptionToThrow)
{
+ if (inputFunc == null)
+ throw new ArgumentNullException("inputFunc", "inputFunc is null.");
if (exceptionToThrow == null)
- throw new MirrorArrangeException("exceptionToThrow cannot be null");
+ throw new ArgumentNullException("exceptionToThrow", "exceptionToThrow is null.");
if (inputFunc.Body is MethodCallExpression)
{
var methodCallInfo = AddMethod((MethodCallExpression)inputFunc.Body);
var parameterValues = GetMethodParameters((MethodCallExpression)inputFunc.Body);
- methodCallInfo.AddMethodException(exceptionToThrow, parameterValues);
+ methodCallInfo.AddThrows(exceptionToThrow, parameterValues);
}
else
throw new MirrorArrangeException("Unsupported expression type " + inputFunc.Body.GetType().Name);
}
+ /// <summary>
+ /// Throws the specified exception when the specified method or property is called with the specified parameters
+ /// </summary>
+ /// <param name="inputFunc">The function being mocked</param>
+ /// <param name="exceptionToThrow">The exception to be thrown</param>
+ public void Throws<TReturnType>(Expression<Func<TMirroredType, TReturnType>> inputFunc, Exception exceptionToThrow)
+ {
+ if (inputFunc == null)
+ throw new ArgumentNullException("inputFunc", "inputFunc is null.");
+ if (exceptionToThrow == null)
+ throw new ArgumentNullException("exceptionToThrow", "exceptionToThrow is null.");
+
+ if (inputFunc.Body is MethodCallExpression)
+ {
+ var methodCallInfo = AddMethod((MethodCallExpression)inputFunc.Body);
+ var parameterValues = GetMethodParameters((MethodCallExpression)inputFunc.Body);
+ methodCallInfo.AddThrows(exceptionToThrow, parameterValues);
+ }
+ else if (inputFunc.Body is MemberExpression)
+ {
+ var memberInfo = AddMember((MemberExpression)inputFunc.Body);
+ memberInfo.AddThrows(exceptionToThrow, null);
+ }
+ else
+ throw new MirrorArrangeException("Unsupported expression type " + inputFunc.Body.GetType().Name);
+ }
+
private MemberCallInfo GetMethodCallInfo(System.Reflection.MethodInfo method)
{
MemberCallInfo memberCallInfo = null;

0 comments on commit 1e928aa

Please sign in to comment.