< Summary

Information
Class: ArturRios.Util.FlowControl.ConditionFailedException
Assembly: ArturRios.Util
File(s): D:\Repositories\dotnet-util\src\FlowControl\Condition.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 2
Coverable lines: 2
Total lines: 112
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage
0%
Covered methods: 0
Fully covered methods: 0
Total methods: 1
Method coverage: 0%
Full method coverage: 0%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%

File(s)

D:\Repositories\dotnet-util\src\FlowControl\Condition.cs

#LineLine coverage
 1using ArturRios.Output;
 2
 3namespace ArturRios.Util.FlowControl;
 4
 5/// <summary>
 6/// A fluent helper to evaluate multiple boolean conditions and aggregate failures.
 7/// </summary>
 8/// <remarks>
 9/// Use <see cref="True"/> or <see cref="False"/> to set the result of an expression and chain <see cref="FailsWith"/> t
 10/// After building all conditions call <see cref="ThrowIfNotSatisfied"/> or inspect <see cref="IsSatisfied"/> / <see cre
 11/// </remarks>
 12public class Condition
 13{
 14    private readonly HashSet<string> _failedConditions = [];
 15    private bool _expression;
 16
 17    /// <summary>
 18    /// Creates a new <see cref="Condition"/> instance. Syntactic sugar for <c>new Condition()</c>.
 19    /// </summary>
 20    public static Condition Create => new();
 21
 22    /// <summary>
 23    /// Gets an array of all failure messages collected by <see cref="FailsWith"/>.
 24    /// </summary>
 25    public string[] FailedConditions => _failedConditions.ToArray();
 26
 27    /// <summary>
 28    /// Indicates whether all evaluated conditions have succeeded (i.e. there are no failures).
 29    /// </summary>
 30    public bool IsSatisfied => _failedConditions.Count == 0;
 31
 32    /// <summary>
 33    /// Sets the current expression result to <paramref name="expression"/>.
 34    /// </summary>
 35    /// <param name="expression">The boolean value representing the success of the condition.</param>
 36    /// <returns>The current <see cref="Condition"/> for fluent chaining.</returns>
 37    public Condition True(bool expression)
 38    {
 39        _expression = expression;
 40
 41        return this;
 42    }
 43
 44    /// <summary>
 45    /// Sets the current expression result to the negated value of <paramref name="expression"/>.
 46    /// </summary>
 47    /// <param name="expression">The boolean to negate.</param>
 48    /// <returns>The current <see cref="Condition"/> for fluent chaining.</returns>
 49    public Condition False(bool expression)
 50    {
 51        _expression = !expression;
 52
 53        return this;
 54    }
 55
 56    /// <summary>
 57    /// Adds an error message if the most recently set expression evaluated to <c>false</c>.
 58    /// </summary>
 59    /// <param name="error">The error message describing the failure.</param>
 60    /// <returns>The current <see cref="Condition"/> for fluent chaining.</returns>
 61    public Condition FailsWith(string error)
 62    {
 63        if (!_expression)
 64        {
 65            _failedConditions.Add(error);
 66        }
 67
 68        return this;
 69    }
 70
 71    /// <summary>
 72    /// Throws a <see cref="ConditionFailedException"/> if any condition failed.
 73    /// </summary>
 74    /// <exception cref="ConditionFailedException">Thrown when <see cref="IsSatisfied"/> is false.</exception>
 75    public void ThrowIfNotSatisfied()
 76    {
 77        if (IsSatisfied)
 78        {
 79            return;
 80        }
 81
 82        throw new ConditionFailedException(FailedConditions);
 83    }
 84
 85    /// <summary>
 86    /// Converts the condition failures into a <see cref="ProcessOutput"/> instance.
 87    /// </summary>
 88    /// <returns>A <see cref="ProcessOutput"/> with errors populated when the condition is not satisfied.</returns>
 89    public ProcessOutput ToProcessOutput()
 90    {
 91        var output = new ProcessOutput();
 92
 93        if (!IsSatisfied)
 94        {
 95            output.AddErrors(_failedConditions.ToList());
 96        }
 97
 98        return output;
 99    }
 100}
 101
 102/// <summary>
 103/// Exception raised when one or more conditions fail in a <see cref="Condition"/> chain.
 104/// </summary>
 105/// <param name="errors">The set of failure messages collected.</param>
 0106public class ConditionFailedException(string[] errors) : Exception($"A total of {errors.Length} conditions failed")
 107{
 108    /// <summary>
 109    /// The failure messages that caused this exception.
 110    /// </summary>
 0111    public readonly string[] Errors = errors;
 112}

Methods/Properties

.ctor(System.String[])