< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.SharedState.SharedStateStore
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/SharedState/SharedState.cs
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
87%
Covered lines: 28
Uncovered lines: 4
Coverable lines: 32
Total lines: 94
Line coverage: 87.5%
Branch coverage
75%
Covered branches: 12
Total branches: 16
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Set(...)100%11100%
Contains(...)100%210%
TryGet(...)50%5460%
Get(...)0%620%
Snapshot()100%11100%
KeySnapshot()100%11100%
ValidateName(...)100%44100%
ValidateValue(...)100%66100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/SharedState/SharedState.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2using System.Text.RegularExpressions;
 3
 4namespace Kestrun.SharedState;
 5
 6/// <summary>
 7/// Thread‑safe, case‑insensitive global key/value store for reference‑type objects.
 8/// </summary>
 9public static partial class SharedStateStore
 10{
 11    // ── configuration ───────────────────────────────────────────────
 112    private static readonly Regex _validName =
 113        MyRegex();
 14
 15    // StringComparer.OrdinalIgnoreCase ⇒ 100 % case‑insensitive keys
 116    private static readonly ConcurrentDictionary<string, object?> _store =
 117        new(StringComparer.OrdinalIgnoreCase);
 18
 19    // ── public API ──────────────────────────────────────────────────
 20    /// <summary>Add or overwrite a value (reference‑types only).</summary>
 21    public static bool Set(string name, object? value, bool allowsValueType = false)
 22    {
 7323        ValidateName(name);
 7124        ValidateValue(name, value, allowsValueType);
 7025        _store[name] = value;
 7026        return true;
 27    }
 28
 29    /// <summary>
 30    /// Checks if a variable with the specified name exists in the shared state.
 31    /// </summary>
 32    /// <param name="name">The name of the variable to check.</param>
 33    /// <returns> <c>true</c> if the variable exists; otherwise, <c>false</c>.</returns>
 034    public static bool Contains(string name) => _store.ContainsKey(name);
 35
 36    /// <summary>
 37    /// Strongly‑typed fetch. Returns <c>false</c> if the key is missing
 38    /// or the stored value can’t be cast to <typeparamref name="T"/>.
 39    /// </summary>
 40    public static bool TryGet<T>(string name, out T? value)
 41    {
 242        if (_store.TryGetValue(name, out var raw) && raw is T cast)
 43        {
 244            value = cast;
 245            return true;
 46        }
 047        value = default;
 048        return false;
 49    }
 50
 51    /// <summary>Untyped fetch; <c>null</c> if absent.</summary>
 52    public static object? Get(string name) =>
 053        _store.TryGetValue(name, out var val) ? val : null;
 54
 55    /// <summary>Snapshot of *all* current variables (shallow copy).</summary>
 56    public static IReadOnlyDictionary<string, object?> Snapshot() =>
 75757        _store.ToDictionary(kvp => kvp.Key, kvp => kvp.Value,
 8158                            StringComparer.OrdinalIgnoreCase);
 59
 60    /// <summary>Snapshot of keys only—handy for quick listings.</summary>
 61    public static IReadOnlyCollection<string> KeySnapshot() =>
 1462        [.. _store.Keys];
 63
 64    // ── helpers ────────────────────────────────────────────────────
 65    private static void ValidateName(string name)
 66    {
 7367        if (string.IsNullOrWhiteSpace(name) || !_validName.IsMatch(name))
 68        {
 269            throw new ArgumentException(
 270                $"'{name}' is not a valid identifier for C# / PowerShell.",
 271                nameof(name));
 72        }
 7173    }
 74
 75    private static void ValidateValue(string name, object? value, bool allowsValueType = false)
 76    {
 7177        if (value is null)
 78        {
 6579            return;                       // null is allowed
 80        }
 81
 682        var t = value.GetType();
 683        if (t.IsValueType && !allowsValueType)
 84        {
 185            throw new ArgumentException(
 186                $"Cannot define global variable '{name}' of value type '{t.FullName}'. " +
 187                "Only reference types are allowed.",
 188                nameof(value));
 89        }
 590    }
 91
 92    [GeneratedRegex(@"^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.CultureInvariant)]
 93    private static partial Regex MyRegex();
 94}