Skip to content

Instantly share code, notes, and snippets.

@jakesays-old
Created March 1, 2017 16:27
Show Gist options
  • Save jakesays-old/48db0aecbc96404daf423fad27fe1b6c to your computer and use it in GitHub Desktop.
Save jakesays-old/48db0aecbc96404daf423fad27fe1b6c to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Linq;
using NUnit.Framework;
// ReSharper disable InconsistentNaming
namespace Std.TestingSupport
{
[TestFixture]
public abstract class TestBase
{
class SuccessException : Exception
{
}
public TestContext TestContext { get; set; }
protected TestBase()
{
}
public delegate void TestActor();
private bool _endTest = false;
public Exception CaughtException { get; private set; }
private string _exceptionStack;
public void AssertSuccess()
{
throw new SuccessException();
}
[SetUp]
public void Prep()
{
_endTest = false;
CaughtException = null;
_exceptionStack = null;
}
public void AssertNoException()
{
if (CaughtException != null)
{
throw new AssertionException("No exceptions expected, received: " + CaughtException.Message, CaughtException);
}
}
private void InternalAssertException(Type expectedType, string message = null, params object[] args)
{
InternalAssertException(new[]{expectedType}, message, args);
}
private void InternalAssertException(Type[] expectedTypes, string message = null, params object[] args)
{
if (message == null)
{
var exTypes = string.Join(" or ", expectedTypes.Select(t => t.Name));
message = "Expected exception of type: " + exTypes + ", received " +
(CaughtException == null
? "none"
: CaughtException.GetType().FullName);
}
throw new AssertionException(string.Format(message, args), CaughtException);
}
private bool HaveException(Type expectedType)
{
return CaughtException != null &&
CaughtException.GetType() == expectedType;
}
public void AssertExpectException<TException>(string message = null, params object[] args)
where TException : Exception
{
if (!HaveException(typeof(TException)))
{
InternalAssertException(typeof(TException), message, args);
}
}
public void AssertExpectException<TException1, TException2>(string message = null, params object[] args)
where TException1 : Exception
where TException2 : Exception
{
if (!HaveException(typeof(TException1)) &&
!HaveException(typeof(TException2)))
{
InternalAssertException(new[]{typeof(TException1), typeof(TException2)}, message, args);
}
}
public void AssertExpectException<TException1, TException2, TException3>(string message = null, params object[] args)
where TException1 : Exception
where TException2 : Exception
where TException3 : Exception
{
if (!HaveException(typeof(TException1)) &&
!HaveException(typeof(TException2)) &&
!HaveException(typeof(TException3)))
{
InternalAssertException(
new[] { typeof(TException1), typeof(TException2), typeof(TException3) }, message, args);
}
}
public void AssertExpectException<TException1, TException2, TException3, TException4>(string message = null, params object[] args)
where TException1 : Exception
where TException2 : Exception
where TException3 : Exception
where TException4 : Exception
{
if (!HaveException(typeof(TException1)) &&
!HaveException(typeof(TException2)) &&
!HaveException(typeof(TException3)) &&
!HaveException(typeof(TException3)))
{
InternalAssertException(
new[] { typeof(TException1), typeof(TException2), typeof(TException3), typeof(TException4) }, message, args);
}
}
private void InternalValidateException(IExceptionContentValidator exceptionValidator,
string message = null, params object[] args)
{
if (exceptionValidator == null)
{
throw new InconclusiveException("Invalid test setup - expected an exception validator");
}
if (CaughtException == null)
{
return;
}
if (!exceptionValidator.Validate(CaughtException))
{
var logText = string.Format(exceptionValidator.MessageTemplate, CaughtException.Message);
if (!string.IsNullOrEmpty(message))
{
logText += Environment.NewLine + string.Format(message, args);
}
throw new AssertionException(logText, CaughtException);
}
}
public void AssertExpectException<TException>(IExceptionContentValidator exceptionValidator,
string message = null, params object[] args)
where TException : Exception
{
if (!HaveException(typeof(TException)))
{
InternalAssertException(typeof(TException), message, args);
}
InternalValidateException(exceptionValidator, message, args);
}
public void AssertExpectException<TException1, TException2>(IExceptionContentValidator exceptionValidator,
string message = null, params object[] args)
where TException1 : Exception
where TException2 : Exception
{
if (!HaveException(typeof(TException1)) &&
!HaveException(typeof(TException2)))
{
InternalAssertException(new[] { typeof(TException1), typeof(TException2) }, message, args);
}
InternalValidateException(exceptionValidator, message, args);
}
public void AssertExpectException<TException1, TException2, TException3>(IExceptionContentValidator exceptionValidator,
string message = null, params object[] args)
where TException1 : Exception
where TException2 : Exception
where TException3 : Exception
{
if (!HaveException(typeof(TException1)) &&
!HaveException(typeof(TException2)) &&
!HaveException(typeof(TException3)))
{
InternalAssertException(
new[] { typeof(TException1), typeof(TException2), typeof(TException3) }, message, args);
}
InternalValidateException(exceptionValidator, message, args);
}
public void AssertExpectException<TException1, TException2, TException3, TException4>(IExceptionContentValidator exceptionValidator,
string message = null, params object[] args)
where TException1 : Exception
where TException2 : Exception
where TException3 : Exception
where TException4 : Exception
{
if (!HaveException(typeof(TException1)) &&
!HaveException(typeof(TException2)) &&
!HaveException(typeof(TException3)) &&
!HaveException(typeof(TException3)))
{
InternalAssertException(
new[] { typeof(TException1), typeof(TException2), typeof(TException3), typeof(TException4) }, message, args);
}
InternalValidateException(exceptionValidator, message, args);
}
public static void NullAction()
{
}
private Action _cleanupActor;
[DebuggerStepThrough]
private void Cleanup()
{
if (_cleanupActor != null)
{
try
{
_cleanupActor();
}
catch (Exception)
{
//ignore - best effort
}
}
}
[DebuggerStepThrough]
public virtual void arrange(TestActor actor, Action cleanupActor = null)
{
_cleanupActor = cleanupActor;
try
{
actor();
}
catch (SuccessException)
{
Cleanup();
_endTest = true;
}
catch (Exception ex)
{
Cleanup();
throw new InconclusiveException("Test Arrange failed: " + ex.Message, ex);
}
}
[DebuggerStepThrough]
public virtual void act(TestActor actor)
{
if (_endTest)
{
return;
}
try
{
actor();
}
catch (SuccessException)
{
Cleanup();
_endTest = true;
}
catch (Exception ex)
{
Cleanup();
CaughtException = ex;
_exceptionStack = ex.StackTrace;
}
}
[DebuggerStepThrough]
public virtual void assertSetup(TestActor actor)
{
if (_endTest)
{
return;
}
try
{
actor();
}
catch (SuccessException)
{
Cleanup();
_endTest = true;
}
catch (Exception ex)
{
Cleanup();
throw new InconclusiveException("Test Assert Setup failed", ex);
}
}
[DebuggerStepThrough]
public virtual void assert(TestActor actor)
{
if (_endTest)
{
return;
}
if (CaughtException != null)
{
throw new AssertionException(CaughtException.Message, CaughtException);
}
try
{
actor();
}
catch (SuccessException)
{
//does nothing
}
Cleanup();
}
[DebuggerStepThrough]
public virtual void assert<TExpectedException>(TestActor actor)
where TExpectedException : Exception
{
if (_endTest)
{
return;
}
AssertExpectException<TExpectedException>();
try
{
actor();
}
catch (SuccessException)
{
//does nothing
}
Cleanup();
}
[DebuggerStepThrough]
public virtual void assertWithException(TestActor actor)
{
if (_endTest)
{
return;
}
try
{
actor();
}
catch (SuccessException)
{
//does nothing
}
Cleanup();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment