< Summary

Information
Class: Chronicis.Client.Services.PrefixGroupDetector
Assembly: Chronicis.Client
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Client/Services/PrefixGroupDetector.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 69
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DetectGroups(...)100%1212100%
IsAbilityScoreGroup(...)100%22100%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Client/Services/PrefixGroupDetector.cs

#LineLine coverage
 1using static Chronicis.Client.Services.RenderDefinitionHelpers;
 2
 3namespace Chronicis.Client.Services;
 4
 5/// <summary>
 6/// Detects prefix-based field groups and identifies ability score groups.
 7/// </summary>
 8public static class PrefixGroupDetector
 9{
 10    public static List<PrefixGroup> DetectGroups(List<FieldInfo> fields)
 11    {
 2112        var candidates = new Dictionary<string, List<FieldInfo>>(StringComparer.OrdinalIgnoreCase);
 13
 17814        foreach (var fi in fields)
 15        {
 6816            var lastUnderscore = fi.Name.LastIndexOf('_');
 6817            if (lastUnderscore <= 0)
 18                continue;
 19
 5620            var prefix = fi.Name[..lastUnderscore];
 5621            if (!candidates.ContainsKey(prefix))
 1822                candidates[prefix] = new List<FieldInfo>();
 5623            candidates[prefix].Add(fi);
 24        }
 25
 2126        var validPrefixes = candidates
 2127            .Where(kv => kv.Value.Count >= 3)
 2128            .ToList();
 2129        validPrefixes.Sort((a, b) => b.Key.Length.CompareTo(a.Key.Length));
 30
 2131        var claimed = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
 2132        var groups = new List<PrefixGroup>();
 33
 7034        foreach (var kv in validPrefixes)
 35        {
 1436            var unclaimed = kv.Value.Where(f => !claimed.Contains(f.Name)).ToList();
 1437            groups.Add(new PrefixGroup
 1438            {
 1439                Prefix = kv.Key,
 1440                Label = FormatGroupLabel(kv.Key),
 1441                Fields = unclaimed
 1442            });
 13043            foreach (var f in unclaimed)
 5144                claimed.Add(f.Name);
 45        }
 46
 2147        return groups;
 48    }
 49
 50    public static bool IsAbilityScoreGroup(PrefixGroup group)
 51    {
 1352        if (group.Fields.Count != 6)
 953            return false;
 454        var suffixes = group.Fields
 455            .Select(f => StripPrefix(f.Name, group.Prefix).ToLowerInvariant())
 456            .ToHashSet();
 457        return AbilitySuffixes.All(s => suffixes.Contains(s));
 58    }
 59}
 60
 61/// <summary>
 62/// A group of fields sharing a common prefix.
 63/// </summary>
 64public class PrefixGroup
 65{
 66    public string Prefix { get; set; } = "";
 67    public string Label { get; set; } = "";
 68    public List<FieldInfo> Fields { get; set; } = new();
 69}