| | | 1 | | using AsiBackbone.Core.Constraints; |
| | | 2 | | using AsiBackbone.Core.Results; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.Testing.Contracts; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Reusable contract fixture for <see cref="IAsiBackboneConstraint{TContext}" /> implementations. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <typeparam name="TContext">The framework-neutral evaluation context type.</typeparam> |
| | | 10 | | public abstract class AsiBackboneConstraintContract<TContext> |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Creates the constraint implementation under test. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <returns>The constraint implementation to validate.</returns> |
| | | 16 | | protected abstract IAsiBackboneConstraint<TContext> CreateConstraint(); |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Creates the context supplied to the constraint implementation under test. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <returns>The evaluation context to validate with.</returns> |
| | | 22 | | protected abstract TContext CreateEvaluationContext(); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Verifies that the constraint returns a safe, non-null result shape. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="cancellationToken">A token that can cancel the contract validation.</param> |
| | | 28 | | /// <returns>The verified constraint result.</returns> |
| | | 29 | | public async ValueTask<ConstraintEvaluationResult> VerifyConstraintReturnsSafeResultAsync(CancellationToken cancella |
| | | 30 | | { |
| | 28 | 31 | | IAsiBackboneConstraint<TContext> constraint = CreateConstraint() |
| | 28 | 32 | | ?? throw new AsiBackboneContractViolationException("Constraint contract must provide a constraint instance." |
| | 26 | 33 | | TContext context = CreateEvaluationContext() |
| | 26 | 34 | | ?? throw new AsiBackboneContractViolationException("Constraint contract must provide an evaluation context." |
| | | 35 | | |
| | 24 | 36 | | if (string.IsNullOrWhiteSpace(constraint.Name)) |
| | | 37 | | { |
| | 2 | 38 | | throw new AsiBackboneContractViolationException("Constraint implementations must expose a stable, non-empty |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | try |
| | | 42 | | { |
| | 22 | 43 | | ConstraintEvaluationResult result = await constraint.EvaluateAsync(context, cancellationToken).ConfigureAwai |
| | 16 | 44 | | return VerifyConstraintResult(result, constraint.Name); |
| | | 45 | | } |
| | 2 | 46 | | catch (OperationCanceledException) |
| | | 47 | | { |
| | 2 | 48 | | throw; |
| | | 49 | | } |
| | 14 | 50 | | catch (AsiBackboneContractViolationException) |
| | | 51 | | { |
| | 14 | 52 | | throw; |
| | | 53 | | } |
| | 2 | 54 | | catch (Exception exception) |
| | | 55 | | { |
| | 2 | 56 | | throw new AsiBackboneContractViolationException( |
| | 2 | 57 | | $"Constraint '{constraint.Name}' must not throw during normal contract validation; return Deny, Warning, |
| | 2 | 58 | | exception); |
| | | 59 | | } |
| | 4 | 60 | | } |
| | | 61 | | |
| | | 62 | | private static ConstraintEvaluationResult VerifyConstraintResult(ConstraintEvaluationResult? result, string constrai |
| | | 63 | | { |
| | 16 | 64 | | if (result is null) |
| | | 65 | | { |
| | 2 | 66 | | throw new AsiBackboneContractViolationException($"Constraint '{constraintName}' must return a result and mus |
| | | 67 | | } |
| | | 68 | | |
| | 14 | 69 | | if (result.IsDenied || result.IsWarning) |
| | | 70 | | { |
| | 12 | 71 | | if (result.Reasons.Count == 0) |
| | | 72 | | { |
| | 4 | 73 | | throw new AsiBackboneContractViolationException($"Constraint '{constraintName}' denied or warned without |
| | | 74 | | } |
| | | 75 | | |
| | 20 | 76 | | for (int index = 0; index < result.Reasons.Count; index++) |
| | | 77 | | { |
| | 8 | 78 | | OperationReason reason = result.Reasons[index] ?? throw new AsiBackboneContractViolationException($"Cons |
| | | 79 | | |
| | 6 | 80 | | if (string.IsNullOrWhiteSpace(reason.Code)) |
| | | 81 | | { |
| | 2 | 82 | | throw new AsiBackboneContractViolationException($"Constraint '{constraintName}' returned an empty re |
| | | 83 | | } |
| | | 84 | | |
| | 4 | 85 | | if (string.IsNullOrWhiteSpace(reason.Message)) |
| | | 86 | | { |
| | 2 | 87 | | throw new AsiBackboneContractViolationException($"Constraint '{constraintName}' returned an empty re |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | 4 | 92 | | return result; |
| | | 93 | | } |
| | | 94 | | } |