< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Build(...)100%44100%
AddOverviewSection(...)100%22100%
AddGroupedSections(...)100%44100%
BuildAbilityScoreSection(...)100%11100%
AddComplexFieldsSection(...)100%22100%

File(s)

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

#LineLine coverage
 1using Chronicis.Client.Models;
 2using static Chronicis.Client.Services.RenderDefinitionHelpers;
 3
 4namespace Chronicis.Client.Services;
 5
 6/// <summary>
 7/// Builds RenderSection lists from classified fields and prefix groups.
 8/// </summary>
 9public static class SectionBuilder
 10{
 11    public static List<RenderSection> Build(List<FieldInfo> remaining, List<PrefixGroup> prefixGroups)
 12    {
 2013        var groupedNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
 6014        foreach (var group in prefixGroups)
 9815            foreach (var fi in group.Fields)
 3916                groupedNames.Add(fi.Name);
 17
 2018        var ungrouped = remaining.Where(f => !groupedNames.Contains(f.Name)).ToList();
 2019        var sections = new List<RenderSection>();
 20
 2021        AddOverviewSection(sections, ungrouped);
 2022        AddGroupedSections(sections, prefixGroups);
 2023        AddComplexFieldsSection(sections, ungrouped);
 24
 2025        return sections;
 26    }
 27
 28    private static void AddOverviewSection(List<RenderSection> sections, List<FieldInfo> ungrouped)
 29    {
 2030        var overviewFields = ungrouped
 2031            .Where(f => !f.IsComplex)
 2032            .OrderBy(f => IsDescriptionField(f.Name) ? 1 : 0)
 2033            .ToList();
 34
 2035        if (overviewFields.Count > 0)
 36        {
 1037            sections.Add(new RenderSection
 1038            {
 1039                Label = "Overview",
 1040                Render = "fields",
 1041                Fields = overviewFields.Select(f => new RenderField
 1042                {
 1043                    Path = f.Name,
 1044                    Label = FormatFieldName(f.Name),
 1045                    Render = IsDescriptionField(f.Name) ? "richtext" : "text"
 1046                }).ToList()
 1047            });
 48        }
 2049    }
 50
 51    private static void AddGroupedSections(List<RenderSection> sections, List<PrefixGroup> prefixGroups)
 52    {
 6053        foreach (var group in prefixGroups.OrderBy(g => g.Label))
 54        {
 1055            if (PrefixGroupDetector.IsAbilityScoreGroup(group))
 56            {
 257                sections.Add(BuildAbilityScoreSection(group));
 58            }
 59            else
 60            {
 861                var mostlyNull = group.Fields.Count(f => f.IsNull) > group.Fields.Count / 2;
 862                sections.Add(new RenderSection
 863                {
 864                    Label = FormatGroupLabel(group.Prefix),
 865                    Render = "fields",
 866                    Collapsed = mostlyNull,
 867                    Fields = group.Fields
 868                        .OrderBy(f => f.IsNull ? 1 : 0)
 869                        .Select(f => new RenderField
 870                        {
 871                            Path = f.Name,
 872                            Label = FormatFieldName(StripPrefix(f.Name, group.Prefix))
 873                        }).ToList()
 874                });
 75            }
 76        }
 2077    }
 78
 79    private static RenderSection BuildAbilityScoreSection(PrefixGroup group)
 80    {
 281        return new RenderSection
 282        {
 283            Label = "Ability Scores",
 284            Render = "stat-row",
 285            Fields = AbilitySuffixes.Select((suffix, i) =>
 286            {
 287                var match = group.Fields.FirstOrDefault(f =>
 288                    f.Name.EndsWith(suffix, StringComparison.OrdinalIgnoreCase));
 289                return new RenderField
 290                {
 291                    Path = match!.Name,
 292                    Label = AbilityLabels[i]
 293                };
 294            }).ToList()
 295        };
 96    }
 97
 98    private static void AddComplexFieldsSection(List<RenderSection> sections, List<FieldInfo> ungrouped)
 99    {
 20100        var complexFields = ungrouped.Where(f => f.IsComplex).ToList();
 20101        if (complexFields.Count > 0)
 102        {
 2103            sections.Add(new RenderSection
 2104            {
 2105                Label = "Additional Data",
 2106                Render = "fields",
 2107                Collapsed = true,
 2108                Fields = complexFields.Select(f => new RenderField
 2109                {
 2110                    Path = f.Name,
 2111                    Label = FormatFieldName(f.Name)
 2112                }).ToList()
 2113            });
 114        }
 20115    }
 116}