| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using ArturRios.Configuration.Enums; |
| | | 3 | | using Xunit; |
| | | 4 | | using Xunit.v3; |
| | | 5 | | |
| | | 6 | | namespace ArturRios.Util.Test.Attributes; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Base <see cref="TheoryAttribute"/> that can skip a test based on the active <c>ASPNETCORE_ENVIRONMENT</c> |
| | | 10 | | /// or an explicit condition, and stamps its <see cref="TestType"/> as the <c>Category</c> trait. |
| | | 11 | | /// Derive from it to create environment-aware theory attributes filterable by test type. |
| | | 12 | | /// </summary> |
| | | 13 | | public class CustomTheoryAttribute : TheoryAttribute, ITraitAttribute |
| | | 14 | | { |
| | | 15 | | /// <summary>Creates the attribute and applies the skip reason, if any.</summary> |
| | | 16 | | /// <param name="testType">The kind of test this attribute marks, published as the <c>Category</c> trait.</param> |
| | | 17 | | /// <param name="environments">Environments in which the test must not run. <c>null</c> imposes no restriction.</par |
| | | 18 | | /// <param name="skipCondition">When <c>true</c>, the test is skipped regardless of the environment.</param> |
| | | 19 | | /// <param name="sourceFilePath">Supplied by the compiler; xUnit reports it as the test's source location.</param> |
| | | 20 | | /// <param name="sourceLineNumber">Supplied by the compiler; xUnit reports it as the test's source location.</param> |
| | | 21 | | protected CustomTheoryAttribute(TestType testType, EnvironmentType[]? environments = null, bool skipCondition = fals |
| | | 22 | | [CallerFilePath] string? sourceFilePath = null, [CallerLineNumber] int sourceLineNumber = -1) |
| | 6 | 23 | | : base(sourceFilePath, sourceLineNumber) |
| | | 24 | | { |
| | 6 | 25 | | TestType = testType; |
| | 6 | 26 | | Environments = environments; |
| | | 27 | | |
| | 6 | 28 | | var reason = EnvironmentSkip.GetReason(environments, skipCondition); |
| | | 29 | | |
| | 6 | 30 | | if (reason is not null) |
| | | 31 | | { |
| | 1 | 32 | | Skip = reason; |
| | | 33 | | } |
| | 6 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary>The kind of test this attribute marks, published as the <c>Category</c> trait.</summary> |
| | 4 | 37 | | public TestType TestType { get; } |
| | | 38 | | |
| | | 39 | | /// <summary>The environments in which the test must not run, or <c>null</c> when unrestricted.</summary> |
| | 0 | 40 | | public EnvironmentType[]? Environments { get; } |
| | | 41 | | |
| | | 42 | | /// <summary>Publishes <see cref="TestType"/> as the <c>Category</c> trait.</summary> |
| | 2 | 43 | | public IReadOnlyCollection<KeyValuePair<string, string>> GetTraits() => TestTypeTrait.For(TestType); |
| | | 44 | | } |