Skip to content

Instantly share code, notes, and snippets.

@jnm2
Created October 4, 2021 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnm2/99289cffa9ec74c1973559fe3ac19790 to your computer and use it in GitHub Desktop.
Save jnm2/99289cffa9ec74c1973559fe3ac19790 to your computer and use it in GitHub Desktop.
using System;
using System.Globalization;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
public sealed class EffectiveAttribute : NUnitAttribute, IWrapSetUpTearDown
{
private readonly DateTime starting;
public EffectiveAttribute(string starting)
{
this.starting = DateTime.Parse(starting, CultureInfo.InvariantCulture);
}
public TestCommand Wrap(TestCommand command)
{
return new Command(starting, command);
}
private sealed class Command : DelegatingTestCommand
{
private readonly DateTime starting;
public Command(DateTime starting, TestCommand innerCommand)
: base(innerCommand)
{
this.starting = starting;
}
public override TestResult Execute(TestExecutionContext context)
{
if (DateTime.Now < starting)
{
context.CurrentResult.RecordAssertion(
AssertionStatus.Inconclusive,
$"This test is not effective until {starting}.");
context.CurrentResult.RecordTestCompletion();
return context.CurrentResult;
}
context.CurrentResult.RecordAssertion(
AssertionStatus.Error,
$"Now that this test is effective, remove {nameof(EffectiveAttribute)} from the test method.");
return innerCommand.Execute(context);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment