< Summary

Information
Class: Intentum.Runtime.Policy.IntentPolicy
Assembly: Intentum.Runtime
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.Runtime/Policy/IntentPolicy.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 55
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
.ctor(...)100%11100%
AddRule(...)100%11100%
WithBase(...)100%11100%
Merge(...)100%22100%
Validate()100%44100%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.Runtime/Policy/IntentPolicy.cs

#LineLine coverage
 1namespace Intentum.Runtime.Policy;
 2
 3/// <summary>
 4/// Ruleset for evaluating intent decisions.
 5/// </summary>
 6public sealed class IntentPolicy
 7{
 1098    private readonly List<PolicyRule> _rules = [];
 9
 12510    public IReadOnlyCollection<PolicyRule> Rules => _rules;
 11
 12    /// <summary>Creates an empty policy.</summary>
 27913    public IntentPolicy() { }
 14
 15    /// <summary>Creates a policy with the given rules (e.g. for composition).</summary>
 1616    public IntentPolicy(IEnumerable<PolicyRule> rules)
 1617    {
 1618        _rules.AddRange(rules);
 1619    }
 20
 21    public IntentPolicy AddRule(PolicyRule rule)
 17822    {
 17823        _rules.Add(rule);
 17824        return this;
 17825    }
 26
 27    /// <summary>
 28    /// Returns a new policy that evaluates this policy's rules after the base policy's rules (base first, then this). F
 29    /// </summary>
 30    public IntentPolicy WithBase(IntentPolicy basePolicy)
 231    {
 232        ArgumentNullException.ThrowIfNull(basePolicy);
 233        return new IntentPolicy(basePolicy.Rules.Concat(_rules));
 234    }
 35
 36    /// <summary>
 37    /// Returns a new policy that merges all given policies: rules from the first policy first, then the second, etc. Fi
 38    /// </summary>
 39    public static IntentPolicy Merge(params IntentPolicy[] policies)
 340    {
 341        if (policies.Length == 0)
 142            return new IntentPolicy();
 543        return new IntentPolicy(policies.SelectMany(p => p.Rules));
 344    }
 45
 46    public void Validate()
 347    {
 348        if (Rules.Count == 0)
 149            throw new InvalidOperationException("IntentPolicy must contain at least one rule.");
 50
 1051        var duplicateNames = Rules.GroupBy(r => r.Name).Where(g => g.Count() > 1);
 252        if (duplicateNames.Any())
 253            throw new InvalidOperationException($"Duplicate rule names found: {string.Join(", ", duplicateNames.Select(g
 154    }
 55}