Advertisement
hawksgrey

typemock-linqpad

Nov 3rd, 2012
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. <Query Kind="Program">
  2. <Reference>C:\tools\typemock-linqpad\nunit.framework.dll</Reference>
  3. <Reference>C:\tools\typemock-linqpad\Typemock.ArrangeActAssert.dll</Reference>
  4. <Reference>C:\tools\typemock-linqpad\TypeMock.dll</Reference>
  5. <NuGetReference>ImageResizer</NuGetReference>
  6. <Namespace>NUnit.Framework</Namespace>
  7. <Namespace>TypeMock.ArrangeActAssert</Namespace>
  8. </Query>
  9.  
  10. void Main()
  11. {
  12. try
  13. {
  14. var runner = new NUnitTestRunner();
  15. runner.TextReceived += new EventHandler<OutputEventArgs>( WriteOutput );
  16. runner.Run();
  17. }
  18. catch( Exception e )
  19. {
  20. e.Dump();
  21. }
  22. }
  23.  
  24. // Define other methods and classes here
  25. [TestFixture, Isolated]
  26. public class MyTestFixture
  27. {
  28. [Test]
  29. public void TestMock()
  30. {
  31. Isolate.WhenCalled( () => DateTime.Now ).WillReturn( DateTime.Today );
  32.  
  33. var dt = DateTime.Now;
  34.  
  35. Assert.AreEqual( DateTime.Today, dt );
  36. }
  37. }
  38.  
  39. public class Configuration
  40. {
  41. // Set this to the folder that contains NUnit.
  42. public static readonly string NUnitLocation = @"C:\tools\typemock-linqpad\NUnit-console-2.5.1\nunit-console.exe";
  43. }
  44.  
  45. #region Test Runner
  46.  
  47. public static void WriteOutput(Object sender, OutputEventArgs e)
  48. {
  49. if( e.OutputType == OutputType.StandardOutput )
  50. {
  51. Console.Write( e.Text );
  52. }
  53. else
  54. {
  55. Console.Error.Write( e.Text );
  56. }
  57. }
  58.  
  59. public class NUnitTestRunner
  60. {
  61. private Process _process;
  62. private StreamReader _standardOutput;
  63. private StreamReader _standardError;
  64. private byte[] _errorBuffer = new byte[512];
  65. private byte[] _outputBuffer = new byte[512];
  66. private AsyncCallback _outputReady;
  67. private OutputAsyncState _outputState;
  68. private AsyncCallback _errorReady;
  69. private OutputAsyncState _errorState;
  70.  
  71. public event EventHandler<OutputEventArgs> TextReceived;
  72.  
  73. public void Run()
  74. {
  75. // Set up TypeMock to run
  76. setupTypeMock();
  77.  
  78. _process = new Process();
  79. _process.StartInfo = setupProcessStartInfo();
  80. _process.Start();
  81.  
  82. _standardOutput = _process.StandardOutput;
  83. _standardError = _process.StandardError;
  84.  
  85. _outputReady = new AsyncCallback( OutputCallback );
  86. _outputState = new OutputAsyncState( _standardOutput, _outputBuffer, OutputType.StandardOutput );
  87. _errorReady = new AsyncCallback( OutputCallback );
  88. _errorState = new OutputAsyncState( _standardError, _errorBuffer, OutputType.StandardError );
  89.  
  90. _standardOutput.BaseStream.BeginRead( _outputBuffer, 0, _outputBuffer.Length, _outputReady, _outputState );
  91. _standardError.BaseStream.BeginRead( _errorBuffer, 0, _errorBuffer.Length, _errorReady, _errorState );
  92. _process.WaitForExit();
  93. }
  94.  
  95. private ProcessStartInfo setupProcessStartInfo()
  96. {
  97. var startInfo = new ProcessStartInfo
  98. {
  99. Arguments = String.Format( "-nologo \"{0}\"", System.Reflection.Assembly.GetExecutingAssembly().Location ),
  100. FileName = Configuration.NUnitLocation,
  101. UseShellExecute = false,
  102. RedirectStandardError = true,
  103. RedirectStandardOutput = true,
  104. CreateNoWindow = true
  105. };
  106.  
  107. // startInfo.EnvironmentVariables.Add("Cor_Enable_Profiling", "0x1");
  108. // startInfo.EnvironmentVariables.Add("COR_PROFILER", "{B146457E-9AED-4624-B1E5-968D274416EC}");
  109.  
  110. return startInfo;
  111. }
  112.  
  113. private void setupTypeMock()
  114. {
  115. if ( Environment.GetEnvironmentVariable( "Cor_Enable_Profiling" ).IsNullOrEmpty() )
  116. Environment.SetEnvironmentVariable( "Cor_Enable_Profiling", "0x1" );
  117.  
  118. if ( Environment.GetEnvironmentVariable( "COR_PROFILER" ).IsNullOrEmpty() )
  119. Environment.SetEnvironmentVariable( "COR_PROFILER", "{B146457E-9AED-4624-B1E5-968D274416EC}" );
  120. }
  121.  
  122. private void OutputCallback(IAsyncResult ar)
  123. {
  124. var state = (OutputAsyncState) ar.AsyncState;
  125. int count = state.Stream.BaseStream.EndRead( ar );
  126. if ( count > 0 )
  127. {
  128. if ( TextReceived != null )
  129. {
  130. string text = System.Text.Encoding.ASCII.GetString( state.Buffer, 0, count );
  131. TextReceived( this, new OutputEventArgs( text, state.OutputType ) );
  132. }
  133. state.Stream.BaseStream.BeginRead( state.Buffer, 0, state.Buffer.Length, _outputReady, state );
  134. }
  135. }
  136. }
  137.  
  138. public class OutputEventArgs : EventArgs
  139. {
  140. private string _text;
  141. private OutputType _outputType;
  142.  
  143. public OutputEventArgs(string text, OutputType outputType)
  144. {
  145. _text = text;
  146. _outputType = outputType;
  147. }
  148.  
  149. public string Text
  150. {
  151. get { return _text; }
  152. set { _text = value; }
  153. }
  154.  
  155. public OutputType OutputType
  156. {
  157. get { return _outputType; }
  158. set { _outputType = value; }
  159. }
  160. }
  161.  
  162. public enum OutputType
  163. {
  164. StandardOutput,
  165. StandardError
  166. }
  167.  
  168. internal class OutputAsyncState
  169. {
  170. private StreamReader _stream;
  171. private byte[] _buffer;
  172. private OutputType _outputType;
  173.  
  174. public OutputAsyncState(StreamReader stream, byte[] buffer, OutputType outputType)
  175. {
  176. _stream = stream;
  177. _buffer = buffer;
  178. _outputType = outputType;
  179. }
  180.  
  181. public StreamReader Stream
  182. {
  183. get { return _stream; }
  184. }
  185.  
  186. public byte[] Buffer
  187. {
  188. get { return _buffer; }
  189. }
  190.  
  191. public OutputType OutputType
  192. {
  193. get { return _outputType; }
  194. }
  195. }
  196.  
  197. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement