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

loresoft / AssemblyMetadata.Generators / 10747239677

07 Sep 2024 12:54AM UTC coverage: 89.381% (-0.09%) from 89.474%
10747239677

push

github

pwelter34
fix tests

60 of 72 branches covered (83.33%)

Branch coverage included in aggregate %.

142 of 154 relevant lines covered (92.21%)

41.22 hits per line

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

87.16
/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(EquatableArray<AssemblyConstant> constants, string? assemblyName = null, string? thisNamespace = null)
15
    {
16
        if (constants == null)
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
        if (!string.IsNullOrEmpty(assemblyName))
2✔
52
        {
53
            codeBuilder
2✔
54
                .Append("public const string AssemblyName = \"")
2✔
55
                .Append(assemblyName)
2✔
56
                .AppendLine("\";")
2✔
57
                .AppendLine();
2✔
58
        }
59

60
        foreach (var constant in constants)
64✔
61
        {
62
            var name = SafeName(constant.Name);
30✔
63

64
            codeBuilder
30✔
65
                .Append("public const string ")
30✔
66
                .Append(name)
30✔
67
                .Append(" = ")
30✔
68
                .Append(constant.Value)
30✔
69
                .AppendLine(";")
30✔
70
                .AppendLine();
30✔
71
        }
72

73
        codeBuilder
2✔
74
            .DecrementIndent()
2✔
75
            .AppendLine("}"); // class
2✔
76

77
        if (!string.IsNullOrEmpty(thisNamespace))
2!
78
        {
79
            codeBuilder
×
80
                .DecrementIndent()
×
81
                .AppendLine("}"); // namespace
×
82
        }
83

84
        return codeBuilder.ToString();
2✔
85
    }
86

87
    public static string SafeName(string name)
88
    {
89
        return ToPropertyName(name.AsSpan());
30✔
90
    }
91

92
    public static string ToPropertyName(ReadOnlySpan<char> span)
93
    {
94
        if (span.IsEmpty)
38!
95
            return string.Empty;
×
96

97
        // find the new string size
98
        var resultSize = 0;
38✔
99
        for (int i = 0; i < span.Length; i++)
998✔
100
        {
101
            // first char can only be a letter
102
            if (resultSize == 0 && char.IsLetter(span[i]))
461✔
103
                resultSize++;
38✔
104
            else if (resultSize > 0 && char.IsLetterOrDigit(span[i]))
423✔
105
                resultSize++;
405✔
106
        }
107

108
        Span<char> result = stackalloc char[resultSize];
38✔
109

110
        var written = 0;
38✔
111
        var nextUpper = true;
38✔
112

113
        for (int read = 0; read < span.Length; read++)
998✔
114
        {
115
            if ((written == 0 && !char.IsLetter(span[read])) || !char.IsLetterOrDigit(span[read]))
461✔
116
            {
117
                nextUpper = true;
18✔
118
                continue;
18✔
119
            }
120

121
            if (nextUpper)
443✔
122
                result[written++] = char.ToUpper(span[read]);
49✔
123
            else
124
                result[written++] = span[read];
394✔
125

126
            nextUpper = false;
443✔
127
        }
128

129
        return result.ToString();
38✔
130
    }
131

132
}
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