< Summary

Information
Class: ArturRios.Util.FlowControl.Condition
Assembly: ArturRios.Util
File(s): D:\Repositories\dotnet-util\src\FlowControl\Condition.cs
Line coverage
78%
Covered lines: 26
Uncovered lines: 7
Coverable lines: 33
Total lines: 112
Line coverage: 78.7%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage
77%
Covered methods: 7
Fully covered methods: 3
Total methods: 9
Method coverage: 77.7%
Full method coverage: 33.3%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
get_Create()100%11100%
get_FailedConditions()100%210%
get_IsSatisfied()100%11100%
True(...)100%11100%
False(...)100%11100%
FailsWith(...)100%22100%
ThrowIfNotSatisfied()0%620%
ToProcessOutput()100%22100%

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{
 414    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>
 420    public static Condition Create => new();
 21
 22    /// <summary>
 23    /// Gets an array of all failure messages collected by <see cref="FailsWith"/>.
 24    /// </summary>
 025    public string[] FailedConditions => _failedConditions.ToArray();
 26
 27    /// <summary>
 28    /// Indicates whether all evaluated conditions have succeeded (i.e. there are no failures).
 29    /// </summary>
 430    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)
 538    {
 539        _expression = expression;
 40
 541        return this;
 542    }
 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)
 250    {
 251        _expression = !expression;
 52
 253        return this;
 254    }
 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)
 762    {
 763        if (!_expression)
 264        {
 265            _failedConditions.Add(error);
 266        }
 67
 768        return this;
 769    }
 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()
 076    {
 077        if (IsSatisfied)
 078        {
 079            return;
 80        }
 81
 082        throw new ConditionFailedException(FailedConditions);
 083    }
 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()
 490    {
 491        var output = new ProcessOutput();
 92
 493        if (!IsSatisfied)
 294        {
 295            output.AddErrors(_failedConditions.ToList());
 296        }
 97
 498        return output;
 499    }
 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>
 106public 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>
 111    public readonly string[] Errors = errors;
 112}