2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / nunit24 / NUnitMocks / mocks / MockMethod.cs
blobc8799beda4c5f4d9b9f8bdd809354ab68bc9d361
1 // ****************************************************************
2 // Copyright 2007, Charlie Poole
3 // This is free software licensed under the NUnit license. You may
4 // obtain a copy of the license at http://nunit.org/?p=license&r=2.4
5 // ****************************************************************
7 using System;
8 using System.Collections;
9 using NUnit.Framework;
11 namespace NUnit.Mocks
13 /// <summary>
14 /// The MockMethod object represents one named method on a mock object.
15 /// All overloads are represented by one MockMethod. A method may return
16 /// a fixed value, throw a fixed exception or have an expected sequence
17 /// of calls. If it has a call sequence, then the signature must match and
18 /// each call provides it's own return value or exception.
19 /// </summary>
20 public class MockMethod : IMethod
22 #region Private Fields
24 /// <summary>
25 /// Name of this method
26 /// </summary>
27 private string methodName;
29 /// <summary>
30 /// Fixed return value
31 /// </summary>
32 private object returnVal;
34 /// <summary>
35 /// Exception to be thrown
36 /// </summary>
37 private Exception exception;
39 /// <summary>
40 /// Expected call sequence. If null, this method has no expectations
41 /// and simply provides a fixed return value or exception.
42 /// </summary>
43 private ArrayList expectedCalls = null;
45 /// <summary>
46 /// Actual sequence of calls... currently not used
47 /// </summary>
48 //private ArrayList actualCalls = null;
50 #endregion
52 #region Constructors
54 public MockMethod( string methodName )
55 : this( methodName, null, null ) { }
57 public MockMethod( string methodName, object returnVal )
58 : this( methodName, returnVal, null ) { }
60 public MockMethod( string methodName, object returnVal, Exception exception )
62 this.methodName = methodName;
63 this.returnVal = returnVal;
64 this.exception = exception;
67 #endregion
69 #region IMethod Members
71 public string Name
73 get { return methodName; }
76 public void Expect( ICall call )
78 if ( expectedCalls == null )
79 expectedCalls = new ArrayList();
81 expectedCalls.Add( call );
84 #endregion
86 #region ICall Members
88 public object Call( object[] args )
90 if ( expectedCalls == null )
92 if ( exception != null )
93 throw exception;
95 return returnVal;
97 else
99 //actualCalls.Add( new MethodCall( methodName, null, null, args ) );
100 Assert.IsTrue( expectedCalls.Count > 0, "Too many calls to " + Name );
101 MockCall mockCall = (MockCall)expectedCalls[0];
102 expectedCalls.RemoveAt( 0 );
103 return mockCall.Call( args );
107 #endregion
109 #region IVerify Members
111 public void Verify()
113 if ( expectedCalls != null )
114 Assert.IsTrue( expectedCalls.Count == 0, "Not all methods were called" );
117 #endregion