2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / nunit20 / mocks / Mock.cs
1 using System;
2 using System.Collections;
3 using System.Runtime.Remoting.Proxies;
4 using System.Runtime.Remoting.Messaging;
5 using NUnit.Framework;
6
7 namespace NUnit.Mocks
8 {
9         /// <summary>
10         /// Summary description for MockObject.
11         /// </summary>
12         public class Mock : IMock
13         {
14                 #region Private Fields
15
16                 private string name;
17
18                 private bool strict;
19
20                 private IDictionary methods = new Hashtable();
21
22                 private Exception lastException;
23
24                 #endregion
25
26                 #region Properties
27
28                 public Exception LastException
29                 {
30                         get { return lastException; }
31                 }
32
33                 #endregion
34
35                 #region Constructors
36
37                 public Mock() : this( "Mock" ) { }
38
39                 public Mock( string name )
40                 {
41                         this.name = name;
42                 }
43
44                 #endregion
45
46                 #region IMock Members
47
48                 public string Name
49                 {
50                         get { return name; }
51                 }
52
53                 public bool Strict
54                 {
55                         get { return strict; }
56                         set { strict = value; }
57                 }
58
59                 public void Expect( string methodName, params object[] args )
60                 {
61                         ExpectAndReturn( methodName, null, args );
62                 }
63
64                 public void Expect( string methodName )
65                 {
66                         ExpectAndReturn( methodName, null, null );
67                 }
68
69                 public void ExpectNoCall( string methodName )
70                 {
71                         methods[methodName] = new MockMethod( methodName, null, 
72                                 new AssertionException("Unexpected call to method " + methodName) );
73                 }
74
75                 public void ExpectAndReturn( string methodName, object returnVal, params object[] args )
76                 {
77                         AddExpectedCall( methodName, returnVal, null, args );
78                 }
79
80                 public void ExpectAndThrow( string methodName, Exception exception, params object[] args )
81                 {
82                         AddExpectedCall( methodName, null, exception, args );
83                 }
84
85                 public void SetReturnValue( string methodName, object returnVal )
86                 {
87                         methods[methodName] = new MockMethod( methodName, returnVal );
88                 }
89
90                 #endregion
91
92                 #region IVerify Members
93
94                 public virtual void Verify()
95                 {
96                         foreach( IMethod method in methods.Values )
97                                 method.Verify();
98                 }
99
100                 #endregion
101
102                 #region ICallHandler Members
103
104                 public virtual object Call( string methodName, params object[] args )
105                 {
106                         if ( methods.Contains( methodName ) )
107                         {
108                                 try
109                                 {
110                                         IMethod method = (IMethod)methods[methodName];
111                                         return method.Call( args );
112                                 }
113                                 catch( Exception exception )
114                                 {
115                                         // Save exception in case MO is running on a separate thread
116                                         lastException = exception;
117                                         throw;
118                                 }
119                         }
120                         else // methodName is not listed in methods
121                         if ( Strict )
122                                 Assert.Fail( "Unexpected call to " + methodName );
123                         
124                         // not listed but Strict is not specified
125                         return null;
126                 }
127
128                 #endregion
129         
130                 #region Helper Methods
131
132                 private void AddExpectedCall( string methodName, object returnVal, Exception exception, object[] args )
133                 {
134                         IMethod method = (IMethod)methods[methodName];
135                         if ( method == null )
136                         {
137                                 method = new MockMethod( methodName );
138                                 methods[methodName] = method;
139                         }
140
141                         Type[] argTypes = MethodSignature.GetArgTypes( args );
142                         MethodSignature signature = new MethodSignature( this.Name, methodName, argTypes );
143
144                         method.Expect( new MockCall( signature, returnVal, exception, args ) );
145                 }
146
147                 #endregion
148         }
149 }