Skip to content

Instantly share code, notes, and snippets.

@flakey-bit
Created September 2, 2021 00:26
Show Gist options
  • Save flakey-bit/68368fd8b65e0c72e3e68003f1439fa7 to your computer and use it in GitHub Desktop.
Save flakey-bit/68368fd8b65e0c72e3e68003f1439fa7 to your computer and use it in GitHub Desktop.
xUnit data atrtibute for generating theory cases based on an Enum
[AttributeUsage(AttributeTargets.Method)]
public class EnumDataAttribute : DataAttribute
{
private readonly Type _tEnum;
public EnumDataAttribute(Type tEnum) => this._tEnum = tEnum;
/// <summary>Returns the data to be used to test the theory.</summary>
/// <param name="methodUnderTest">The method that is being tested</param>
/// <param name="parameterTypes">The types of the parameters for the test method</param>
/// <returns>The theory data, in table form</returns>
public override IEnumerable<object[]> GetData(
MethodInfo methodUnderTest,
Type[] parameterTypes)
{
foreach (var value in Enum.GetValues(_tEnum))
{
yield return new object[] {value};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment