< Summary

Information
Class: Intentum.Versioning.PolicyVersionTracker
Assembly: Intentum.Versioning
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.Versioning/PolicyVersionTracker.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 68
Line coverage: 100%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
Add(...)100%22100%
Rollback()100%22100%
Rollforward()75%44100%
SetCurrent(...)100%44100%
CompareVersions(...)100%11100%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.Versioning/PolicyVersionTracker.cs

#LineLine coverage
 1namespace Intentum.Versioning;
 2
 3/// <summary>
 4/// Tracks policy versions for rollback and comparison.
 5/// </summary>
 6public sealed class PolicyVersionTracker
 7{
 98    private readonly List<IVersionedPolicy> _versions = [];
 99    private int _currentIndex = -1;
 10
 11    /// <summary>
 12    /// Registers a versioned policy. Latest is used as current.
 13    /// </summary>
 14    public PolicyVersionTracker Add(IVersionedPolicy versionedPolicy)
 1215    {
 1216        _versions.Add(versionedPolicy ?? throw new ArgumentNullException(nameof(versionedPolicy)));
 1117        _currentIndex = _versions.Count - 1;
 1118        return this;
 1119    }
 20
 21    /// <summary>
 22    /// Gets the current (active) versioned policy.
 23    /// </summary>
 824    public IVersionedPolicy? Current => _currentIndex >= 0 && _currentIndex < _versions.Count ? _versions[_currentIndex]
 25
 26    /// <summary>
 27    /// Gets all registered versions (oldest to newest).
 28    /// </summary>
 129    public IReadOnlyList<IVersionedPolicy> Versions => _versions;
 30
 31    /// <summary>
 32    /// Rolls back to the previous version. Returns true if rollback was performed.
 33    /// </summary>
 34    public bool Rollback()
 335    {
 436        if (_currentIndex <= 0) return false;
 237        _currentIndex--;
 238        return true;
 339    }
 40
 41    /// <summary>
 42    /// Rolls forward to the next version (after a rollback). Returns true if rollforward was performed.
 43    /// </summary>
 44    public bool Rollforward()
 245    {
 346        if (_currentIndex < 0 || _currentIndex >= _versions.Count - 1) return false;
 147        _currentIndex++;
 148        return true;
 249    }
 50
 51    /// <summary>
 52    /// Sets the current version by index (0 = oldest).
 53    /// </summary>
 54    public void SetCurrent(int index)
 355    {
 356        if (index < 0 || index >= _versions.Count)
 257            throw new ArgumentOutOfRangeException(nameof(index));
 158        _currentIndex = index;
 159    }
 60
 61    /// <summary>
 62    /// Compares two version identifiers (e.g. semantic or string).
 63    /// </summary>
 64    public static int CompareVersions(string a, string b)
 365    {
 366        return string.Compare(a, b, StringComparison.OrdinalIgnoreCase);
 367    }
 68}