< Summary

Information
Class: AsiBackbone.Testing.Contracts.GovernanceConstraintContract<T>
Assembly: AsiBackbone.Testing
File(s): /home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.Testing/Contracts/GovernanceConstraintContract.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 94
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
VerifyConstraintReturnsSafeResultAsync()100%66100%
VerifyConstraintResult(...)100%1616100%

File(s)

/home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.Testing/Contracts/GovernanceConstraintContract.cs

#LineLine coverage
 1using AsiBackbone.Core.Constraints;
 2using AsiBackbone.Core.Results;
 3
 4namespace AsiBackbone.Testing.Contracts;
 5
 6/// <summary>
 7/// Reusable contract fixture for <see cref="IGovernanceConstraint{TContext}" /> implementations.
 8/// </summary>
 9/// <typeparam name="TContext">The framework-neutral evaluation context type.</typeparam>
 10public abstract class GovernanceConstraintContract<TContext>
 11{
 12    /// <summary>
 13    /// Creates the constraint implementation under test.
 14    /// </summary>
 15    /// <returns>The constraint implementation to validate.</returns>
 16    protected abstract IGovernanceConstraint<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    {
 1431        IGovernanceConstraint<TContext> constraint = CreateConstraint()
 1432            ?? throw new GovernanceContractViolationException("Constraint contract must provide a constraint instance.")
 1333        TContext context = CreateEvaluationContext()
 1334            ?? throw new GovernanceContractViolationException("Constraint contract must provide an evaluation context.")
 35
 1236        if (string.IsNullOrWhiteSpace(constraint.Name))
 37        {
 138            throw new GovernanceContractViolationException("Constraint implementations must expose a stable, non-empty c
 39        }
 40
 41        try
 42        {
 1143            ConstraintEvaluationResult result = await constraint.EvaluateAsync(context, cancellationToken).ConfigureAwai
 844            return VerifyConstraintResult(result, constraint.Name);
 45        }
 146        catch (OperationCanceledException)
 47        {
 148            throw;
 49        }
 750        catch (GovernanceContractViolationException)
 51        {
 752            throw;
 53        }
 154        catch (Exception exception)
 55        {
 156            throw new GovernanceContractViolationException(
 157                $"Constraint '{constraint.Name}' must not throw during normal contract validation; return Deny, Warning,
 158                exception);
 59        }
 260    }
 61
 62    private static ConstraintEvaluationResult VerifyConstraintResult(ConstraintEvaluationResult? result, string constrai
 63    {
 864        if (result is null)
 65        {
 166            throw new GovernanceContractViolationException($"Constraint '{constraintName}' must return a result and must
 67        }
 68
 769        if (result.IsDenied || result.IsWarning)
 70        {
 671            if (result.Reasons.Count == 0)
 72            {
 273                throw new GovernanceContractViolationException($"Constraint '{constraintName}' denied or warned without 
 74            }
 75
 1076            for (int index = 0; index < result.Reasons.Count; index++)
 77            {
 478                OperationReason reason = result.Reasons[index] ?? throw new GovernanceContractViolationException($"Const
 79
 380                if (string.IsNullOrWhiteSpace(reason.Code))
 81                {
 182                    throw new GovernanceContractViolationException($"Constraint '{constraintName}' returned an empty rea
 83                }
 84
 285                if (string.IsNullOrWhiteSpace(reason.Message))
 86                {
 187                    throw new GovernanceContractViolationException($"Constraint '{constraintName}' returned an empty rea
 88                }
 89            }
 90        }
 91
 292        return result;
 93    }
 94}