PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/mocks/Mock.cs

#
C# | 163 lines | 115 code | 38 blank | 10 comment | 6 complexity | 0ce57299995fb48e273a61f4b25b720c MD5 | raw file
Possible License(s): GPL-2.0
  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
  5. // ****************************************************************
  6. using System;
  7. using System.Collections;
  8. using System.Runtime.Remoting.Proxies;
  9. using System.Runtime.Remoting.Messaging;
  10. using NUnit.Framework;
  11. namespace NUnit.Mocks
  12. {
  13. /// <summary>
  14. /// Summary description for MockObject.
  15. /// </summary>
  16. public class Mock : IMock
  17. {
  18. #region Private Fields
  19. private string name;
  20. private bool strict;
  21. private IDictionary methods = new Hashtable();
  22. private Exception lastException;
  23. private ArrayList unexpected = new ArrayList();
  24. #endregion
  25. #region Properties
  26. public Exception LastException
  27. {
  28. get { return lastException; }
  29. }
  30. #endregion
  31. #region Constructors
  32. public Mock() : this( "Mock" ) { }
  33. public Mock( string name )
  34. {
  35. this.name = name;
  36. }
  37. #endregion
  38. #region IMock Members
  39. public string Name
  40. {
  41. get { return name; }
  42. }
  43. public bool Strict
  44. {
  45. get { return strict; }
  46. set { strict = value; }
  47. }
  48. public void Expect( string methodName, params object[] args )
  49. {
  50. ExpectAndReturn( methodName, null, args );
  51. }
  52. public void Expect( string methodName )
  53. {
  54. ExpectAndReturn( methodName, null, null );
  55. }
  56. public void ExpectNoCall( string methodName )
  57. {
  58. methods[methodName] = new MockMethod( methodName, null,
  59. new AssertionException("Unexpected call to method " + methodName) );
  60. }
  61. public void ExpectAndReturn( string methodName, object returnVal, params object[] args )
  62. {
  63. AddExpectedCall( methodName, returnVal, null, args );
  64. }
  65. public void ExpectAndThrow( string methodName, Exception exception, params object[] args )
  66. {
  67. AddExpectedCall( methodName, null, exception, args );
  68. }
  69. public void SetReturnValue( string methodName, object returnVal )
  70. {
  71. methods[methodName] = new MockMethod( methodName, returnVal );
  72. }
  73. #endregion
  74. #region IVerify Members
  75. public virtual void Verify()
  76. {
  77. foreach (IMethod method in methods.Values)
  78. method.Verify();
  79. if (unexpected.Count > 0)
  80. Assert.Fail("Unexpected call to " + (string)unexpected[0]);
  81. }
  82. #endregion
  83. #region ICallHandler Members
  84. public virtual object Call( string methodName, params object[] args )
  85. {
  86. if ( methods.Contains( methodName ) )
  87. {
  88. try
  89. {
  90. IMethod method = (IMethod)methods[methodName];
  91. return method.Call( args );
  92. }
  93. catch( Exception exception )
  94. {
  95. // Save exception in case MO is running on a separate thread
  96. lastException = exception;
  97. throw;
  98. }
  99. }
  100. else // methodName is not listed in methods
  101. if (Strict)
  102. {
  103. unexpected.Add(methodName);
  104. Assert.Fail("Unexpected call to " + methodName);
  105. }
  106. // not listed but Strict is not specified
  107. return null;
  108. }
  109. #endregion
  110. #region Helper Methods
  111. private void AddExpectedCall( string methodName, object returnVal, Exception exception, object[] args )
  112. {
  113. IMethod method = (IMethod)methods[methodName];
  114. if ( method == null )
  115. {
  116. method = new MockMethod( methodName );
  117. methods[methodName] = method;
  118. }
  119. Type[] argTypes = MethodSignature.GetArgTypes( args );
  120. MethodSignature signature = new MethodSignature( this.Name, methodName, argTypes );
  121. method.Expect( new MockCall( signature, returnVal, exception, args ) );
  122. }
  123. #endregion
  124. }
  125. }