• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / AssemblyMetadata.Generators / 11222516121

07 Oct 2024 07:23PM UTC coverage: 88.603% (-0.8%) from 89.381%
11222516121

push

github

pwelter34
add rootnamespace option

70 of 86 branches covered (81.4%)

Branch coverage included in aggregate %.

41 of 43 new or added lines in 5 files covered. (95.35%)

1 existing line in 1 file now uncovered.

171 of 186 relevant lines covered (91.94%)

37.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.76
/src/AssemblyMetadata.Generators/AssemblyMetadataWriter.cs
1
using System.Reflection;
2

3
namespace AssemblyMetadata.Generators;
4

5
public static class AssemblyMetadataWriter
6
{
7
    private static readonly Lazy<string> _informationVersion = new(() =>
1✔
8
    {
1✔
9
        var assembly = typeof(AssemblyMetadataWriter).Assembly;
1✔
10
        var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
1✔
11
        return attribute?.InformationalVersion ?? "1.0.0.0";
1!
12
    });
1✔
13

14
    public static string Generate(IEnumerable<AssemblyConstant> constants, string? thisNamespace = null)
15
    {
16
        if (constants is null)
2!
UNCOV
17
            throw new ArgumentNullException(nameof(constants));
×
18

19
        var codeBuilder = new IndentedStringBuilder();
2✔
20
        codeBuilder
2✔
21
            .AppendLine("// <auto-generated />")
2✔
22
            .AppendLine();
2✔
23

24
        if (!string.IsNullOrEmpty(thisNamespace))
2!
25
        {
26
            codeBuilder
×
27
                .Append("namespace ")
×
28
                .AppendLine(thisNamespace!)
×
29
                .AppendLine("{")
×
30
                .IncrementIndent();
×
31
        }
32

33
        codeBuilder
2✔
34
            .AppendLine("/// <summary>")
2✔
35
            .AppendLine("/// Assembly attributes exposed as public constants")
2✔
36
            .AppendLine("/// </summary>");
2✔
37

38
        codeBuilder
2✔
39
            .Append("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"")
2✔
40
            .Append("AssemblyMetadata.Generators")
2✔
41
            .Append("\", \"")
2✔
42
            .Append(_informationVersion.Value)
2✔
43
            .AppendLine("\")]");
2✔
44

45
        codeBuilder
2✔
46
            .AppendLine("internal static partial class ThisAssembly")
2✔
47
            .AppendLine("{")
2✔
48
            .IncrementIndent()
2✔
49
            .AppendLine();
2✔
50

51
        foreach (var constant in constants)
68✔
52
        {
53
            var name = SafeName(constant.Name);
32✔
54

55
            codeBuilder
32✔
56
                .Append("public const string ")
32✔
57
                .Append(name)
32✔
58
                .Append(" = ")
32✔
59
                .Append(constant.Value)
32✔
60
                .AppendLine(";")
32✔
61
                .AppendLine();
32✔
62
        }
63

64
        codeBuilder
2✔
65
            .DecrementIndent()
2✔
66
            .AppendLine("}"); // class
2✔
67

68
        if (!string.IsNullOrEmpty(thisNamespace))
2!
69
        {
70
            codeBuilder
×
71
                .DecrementIndent()
×
72
                .AppendLine("}"); // namespace
×
73
        }
74

75
        return codeBuilder.ToString();
2✔
76
    }
77

78
    public static string SafeName(string name)
79
    {
80
        return ToPropertyName(name.AsSpan());
32✔
81
    }
82

83
    public static string ToPropertyName(ReadOnlySpan<char> span)
84
    {
85
        if (span.IsEmpty)
40!
86
            return string.Empty;
×
87

88
        // find the new string size
89
        var resultSize = 0;
40✔
90
        for (int i = 0; i < span.Length; i++)
1,050✔
91
        {
92
            // first char can only be a letter
93
            if (resultSize == 0 && char.IsLetter(span[i]))
485✔
94
                resultSize++;
40✔
95
            else if (resultSize > 0 && char.IsLetterOrDigit(span[i]))
445✔
96
                resultSize++;
427✔
97
        }
98

99
        Span<char> result = stackalloc char[resultSize];
40✔
100

101
        var written = 0;
40✔
102
        var nextUpper = true;
40✔
103

104
        for (int read = 0; read < span.Length; read++)
1,050✔
105
        {
106
            if ((written == 0 && !char.IsLetter(span[read])) || !char.IsLetterOrDigit(span[read]))
485✔
107
            {
108
                nextUpper = true;
18✔
109
                continue;
18✔
110
            }
111

112
            if (nextUpper)
467✔
113
                result[written++] = char.ToUpper(span[read]);
51✔
114
            else
115
                result[written++] = span[read];
416✔
116

117
            nextUpper = false;
467✔
118
        }
119

120
        return result.ToString();
40✔
121
    }
122

123
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc