Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Query Kind="Program">
- <Reference>C:\tools\typemock-linqpad\nunit.framework.dll</Reference>
- <Reference>C:\tools\typemock-linqpad\Typemock.ArrangeActAssert.dll</Reference>
- <Reference>C:\tools\typemock-linqpad\TypeMock.dll</Reference>
- <NuGetReference>ImageResizer</NuGetReference>
- <Namespace>NUnit.Framework</Namespace>
- <Namespace>TypeMock.ArrangeActAssert</Namespace>
- </Query>
- void Main()
- {
- try
- {
- var runner = new NUnitTestRunner();
- runner.TextReceived += new EventHandler<OutputEventArgs>( WriteOutput );
- runner.Run();
- }
- catch( Exception e )
- {
- e.Dump();
- }
- }
- // Define other methods and classes here
- [TestFixture, Isolated]
- public class MyTestFixture
- {
- [Test]
- public void TestMock()
- {
- Isolate.WhenCalled( () => DateTime.Now ).WillReturn( DateTime.Today );
- var dt = DateTime.Now;
- Assert.AreEqual( DateTime.Today, dt );
- }
- }
- public class Configuration
- {
- // Set this to the folder that contains NUnit.
- public static readonly string NUnitLocation = @"C:\tools\typemock-linqpad\NUnit-console-2.5.1\nunit-console.exe";
- }
- #region Test Runner
- public static void WriteOutput(Object sender, OutputEventArgs e)
- {
- if( e.OutputType == OutputType.StandardOutput )
- {
- Console.Write( e.Text );
- }
- else
- {
- Console.Error.Write( e.Text );
- }
- }
- public class NUnitTestRunner
- {
- private Process _process;
- private StreamReader _standardOutput;
- private StreamReader _standardError;
- private byte[] _errorBuffer = new byte[512];
- private byte[] _outputBuffer = new byte[512];
- private AsyncCallback _outputReady;
- private OutputAsyncState _outputState;
- private AsyncCallback _errorReady;
- private OutputAsyncState _errorState;
- public event EventHandler<OutputEventArgs> TextReceived;
- public void Run()
- {
- // Set up TypeMock to run
- setupTypeMock();
- _process = new Process();
- _process.StartInfo = setupProcessStartInfo();
- _process.Start();
- _standardOutput = _process.StandardOutput;
- _standardError = _process.StandardError;
- _outputReady = new AsyncCallback( OutputCallback );
- _outputState = new OutputAsyncState( _standardOutput, _outputBuffer, OutputType.StandardOutput );
- _errorReady = new AsyncCallback( OutputCallback );
- _errorState = new OutputAsyncState( _standardError, _errorBuffer, OutputType.StandardError );
- _standardOutput.BaseStream.BeginRead( _outputBuffer, 0, _outputBuffer.Length, _outputReady, _outputState );
- _standardError.BaseStream.BeginRead( _errorBuffer, 0, _errorBuffer.Length, _errorReady, _errorState );
- _process.WaitForExit();
- }
- private ProcessStartInfo setupProcessStartInfo()
- {
- var startInfo = new ProcessStartInfo
- {
- Arguments = String.Format( "-nologo \"{0}\"", System.Reflection.Assembly.GetExecutingAssembly().Location ),
- FileName = Configuration.NUnitLocation,
- UseShellExecute = false,
- RedirectStandardError = true,
- RedirectStandardOutput = true,
- CreateNoWindow = true
- };
- // startInfo.EnvironmentVariables.Add("Cor_Enable_Profiling", "0x1");
- // startInfo.EnvironmentVariables.Add("COR_PROFILER", "{B146457E-9AED-4624-B1E5-968D274416EC}");
- return startInfo;
- }
- private void setupTypeMock()
- {
- if ( Environment.GetEnvironmentVariable( "Cor_Enable_Profiling" ).IsNullOrEmpty() )
- Environment.SetEnvironmentVariable( "Cor_Enable_Profiling", "0x1" );
- if ( Environment.GetEnvironmentVariable( "COR_PROFILER" ).IsNullOrEmpty() )
- Environment.SetEnvironmentVariable( "COR_PROFILER", "{B146457E-9AED-4624-B1E5-968D274416EC}" );
- }
- private void OutputCallback(IAsyncResult ar)
- {
- var state = (OutputAsyncState) ar.AsyncState;
- int count = state.Stream.BaseStream.EndRead( ar );
- if ( count > 0 )
- {
- if ( TextReceived != null )
- {
- string text = System.Text.Encoding.ASCII.GetString( state.Buffer, 0, count );
- TextReceived( this, new OutputEventArgs( text, state.OutputType ) );
- }
- state.Stream.BaseStream.BeginRead( state.Buffer, 0, state.Buffer.Length, _outputReady, state );
- }
- }
- }
- public class OutputEventArgs : EventArgs
- {
- private string _text;
- private OutputType _outputType;
- public OutputEventArgs(string text, OutputType outputType)
- {
- _text = text;
- _outputType = outputType;
- }
- public string Text
- {
- get { return _text; }
- set { _text = value; }
- }
- public OutputType OutputType
- {
- get { return _outputType; }
- set { _outputType = value; }
- }
- }
- public enum OutputType
- {
- StandardOutput,
- StandardError
- }
- internal class OutputAsyncState
- {
- private StreamReader _stream;
- private byte[] _buffer;
- private OutputType _outputType;
- public OutputAsyncState(StreamReader stream, byte[] buffer, OutputType outputType)
- {
- _stream = stream;
- _buffer = buffer;
- _outputType = outputType;
- }
- public StreamReader Stream
- {
- get { return _stream; }
- }
- public byte[] Buffer
- {
- get { return _buffer; }
- }
- public OutputType OutputType
- {
- get { return _outputType; }
- }
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement