| | | 1 | | using ArturRios.Configuration.Enums; |
| | | 2 | | using Xunit; |
| | | 3 | | using Xunit.Sdk; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Util.Test.Attributes; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Base <see cref="TheoryAttribute"/> that can skip a test based on the active <c>ASPNETCORE_ENVIRONMENT</c> |
| | | 9 | | /// or an explicit condition, and stamps its <see cref="TestType"/> as the <c>Category</c> trait. |
| | | 10 | | /// Derive from it to create environment-aware theory attributes filterable by test type. |
| | | 11 | | /// </summary> |
| | | 12 | | [TraitDiscoverer("ArturRios.Util.Test.Attributes.TestTypeTraitDiscoverer", "ArturRios.Util.Test")] |
| | | 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> |
| | 6 | 19 | | protected CustomTheoryAttribute(TestType testType, EnvironmentType[]? environments = null, bool skipCondition = fals |
| | 6 | 20 | | { |
| | 6 | 21 | | TestType = testType; |
| | 6 | 22 | | Environments = environments; |
| | | 23 | | |
| | 6 | 24 | | var reason = EnvironmentSkip.GetReason(environments, skipCondition); |
| | | 25 | | |
| | 6 | 26 | | if (reason is not null) |
| | 1 | 27 | | { |
| | 1 | 28 | | Skip = reason; |
| | 1 | 29 | | } |
| | 6 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary>The kind of test this attribute marks, published as the <c>Category</c> trait.</summary> |
| | 4 | 33 | | public TestType TestType { get; } |
| | | 34 | | |
| | | 35 | | /// <summary>The environments in which the test must not run, or <c>null</c> when unrestricted.</summary> |
| | 0 | 36 | | public EnvironmentType[]? Environments { get; } |
| | | 37 | | } |