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

loresoft / EntityFrameworkCore.Generator / 27730465225

18 Jun 2026 01:21AM UTC coverage: 74.693% (+19.8%) from 54.885%
27730465225

push

github

pwelter34
update tests

922 of 1609 branches covered (57.3%)

Branch coverage included in aggregate %.

7 of 7 new or added lines in 2 files covered. (100.0%)

230 existing lines in 23 files now uncovered.

4007 of 4990 relevant lines covered (80.3%)

1258.69 hits per line

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

73.74
/src/EntityFrameworkCore.Generator.Core/Extensions/GenerationExtensions.cs
1
using System.Diagnostics.CodeAnalysis;
2
using System.Text;
3

4
namespace EntityFrameworkCore.Generator.Extensions;
5

6
public static class GenerationExtensions
7
{
8
    #region Data
9
    private static readonly HashSet<string> _csharpKeywords = new(StringComparer.Ordinal)
5✔
10
    {
5✔
11
        "as", "do", "if", "in", "is",
5✔
12
        "for", "int", "new", "out", "ref", "try",
5✔
13
        "base", "bool", "byte", "case", "char", "else", "enum", "goto", "lock", "long", "null", "this", "true", "uint", "void",
5✔
14
        "break", "catch", "class", "const", "event", "false", "fixed", "float", "sbyte", "short", "throw", "ulong", "using", "while",
5✔
15
        "double", "extern", "object", "params", "public", "return", "sealed", "sizeof", "static", "string", "struct", "switch", "typeof", "unsafe", "ushort",
5✔
16
        "checked", "decimal", "default", "finally", "foreach", "private", "virtual",
5✔
17
        "abstract", "continue", "delegate", "explicit", "implicit", "internal", "operator", "override", "readonly", "volatile",
5✔
18
        "__arglist", "__makeref", "__reftype", "interface", "namespace", "protected", "unchecked",
5✔
19
        "__refvalue", "stackalloc"
5✔
20
    };
5✔
21

22
    private static readonly HashSet<string> _defaultNamespaces =
5✔
23
    [
5✔
24
        "System",
5✔
25
        "System.Collections.Generic",
5✔
26
    ];
5✔
27

28
    private static readonly Dictionary<Type, string> _csharpTypeAlias = new(16)
5✔
29
    {
5✔
30
        { typeof(bool), "bool" },
5✔
31
        { typeof(byte), "byte" },
5✔
32
        { typeof(char), "char" },
5✔
33
        { typeof(decimal), "decimal" },
5✔
34
        { typeof(double), "double" },
5✔
35
        { typeof(float), "float" },
5✔
36
        { typeof(int), "int" },
5✔
37
        { typeof(long), "long" },
5✔
38
        { typeof(object), "object" },
5✔
39
        { typeof(sbyte), "sbyte" },
5✔
40
        { typeof(short), "short" },
5✔
41
        { typeof(string), "string" },
5✔
42
        { typeof(uint), "uint" },
5✔
43
        { typeof(ulong), "ulong" },
5✔
44
        { typeof(ushort), "ushort" },
5✔
45
        { typeof(void), "void" }
5✔
46
    };
5✔
47
    #endregion
48

49
    public static string ToFieldName(this string name)
50
    {
UNCOV
51
        ArgumentException.ThrowIfNullOrEmpty(name);
×
52

53
        return "_" + name.ToCamelCase();
×
54
    }
55

56
    public static string MakeUnique(this string name, Func<string, bool> exists)
57
    {
UNCOV
58
        ArgumentException.ThrowIfNullOrEmpty(name);
×
UNCOV
59
        ArgumentNullException.ThrowIfNull(exists);
×
60

61
        string uniqueName = name;
×
UNCOV
62
        int count = 1;
×
63

64
        while (exists(uniqueName))
×
UNCOV
65
            uniqueName = string.Concat(name, count++);
×
66

67
        return uniqueName;
×
68
    }
69

70
    public static bool IsKeyword(this string text)
71
    {
72
        ArgumentException.ThrowIfNullOrEmpty(text);
13,922✔
73

74
        return _csharpKeywords.Contains(text);
13,922✔
75
    }
76

77
    [return: NotNullIfNotNull(nameof(name))]
78
    public static string? ToSafeName(this string? name)
79
    {
80
        if (string.IsNullOrEmpty(name))
13,922!
UNCOV
81
            return name;
×
82

83
        if (!name.IsKeyword())
13,922✔
84
            return name;
13,890✔
85

86
        return "@" + name;
32✔
87
    }
88

89
    public static string ToType(this Type type)
90
    {
91
        ArgumentNullException.ThrowIfNull(type);
1,702✔
92

93
        var stringBuilder = new StringBuilder();
1,702✔
94
        ProcessType(stringBuilder, type);
1,702✔
95
        return stringBuilder.ToString();
1,702✔
96
    }
97

98
    public static string? ToNullableType(this Type type, bool isNullable = false)
99
    {
100
        bool isValueType = type.IsValueType;
399✔
101

102
        var typeString = type.ToType();
399✔
103

104
        if (!isValueType || !isNullable)
399✔
105
            return typeString;
382✔
106

107
        return typeString.EndsWith('?') ? typeString : typeString + "?";
17!
108
    }
109

110
    public static bool IsValueType(this string? type)
111
    {
UNCOV
112
        if (string.IsNullOrEmpty(type))
×
UNCOV
113
            return false;
×
114

115
        if (!type.StartsWith("System."))
×
UNCOV
116
            return false;
×
117

118
        var t = Type.GetType(type, false);
×
UNCOV
119
        return t != null && t.IsValueType;
×
120
    }
121

122
    public static string ToLiteral(this string value)
123
    {
124
        ArgumentException.ThrowIfNullOrEmpty(value);
3,979✔
125

126
        return value.Contains('\n') || value.Contains('\r')
3,979!
127
            ? "@\"" + value.Replace("\"", "\"\"") + "\""
3,979✔
128
            : "\"" + value.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"";
3,979✔
129
    }
130

131

132

133
    private static void ProcessType(StringBuilder builder, Type type)
134
    {
135
        if (type.IsGenericType)
1,765✔
136
        {
137
            var genericArguments = type.GetGenericArguments();
7✔
138
            ProcessGenericType(builder, type, genericArguments, genericArguments.Length);
7✔
139
        }
140
        else if (type.IsArray)
1,758✔
141
        {
142
            ProcessArrayType(builder, type);
54✔
143
        }
144
        else if (_csharpTypeAlias.TryGetValue(type, out var builtInName))
1,704✔
145
        {
146
            builder.Append(builtInName);
1,393✔
147
        }
148
        else if (type.Namespace.HasValue() && _defaultNamespaces.Contains(type.Namespace))
311✔
149
        {
150
            builder.Append(type.Name);
310✔
151
        }
152
        else
153
        {
154
            builder.Append(type.FullName ?? type.Name);
1!
155
        }
156
    }
1✔
157

158
    private static void ProcessArrayType(StringBuilder builder, Type type)
159
    {
160
        var innerType = type;
54✔
161
        while (innerType.IsArray)
109✔
162
        {
163
            innerType = innerType.GetElementType()!;
55✔
164
        }
165

166
        ProcessType(builder, innerType);
54✔
167

168
        while (type.IsArray)
109✔
169
        {
170
            builder.Append('[');
55✔
171
            builder.Append(',', type.GetArrayRank() - 1);
55✔
172
            builder.Append(']');
55✔
173
            type = type.GetElementType()!;
55✔
174
        }
175
    }
54✔
176

177
    private static void ProcessGenericType(StringBuilder builder, Type type, Type[] genericArguments, int length)
178
    {
179
        if (type.IsConstructedGenericType
7!
180
            && type.GetGenericTypeDefinition() == typeof(Nullable<>))
7✔
181
        {
UNCOV
182
            ProcessType(builder, type.GetUnderlyingType());
×
UNCOV
183
            builder.Append('?');
×
184
            return;
×
185
        }
186

187
        var offset = type.DeclaringType != null ? type.DeclaringType.GetGenericArguments().Length : 0;
7!
188
        var genericPartIndex = type.Name.IndexOf('`');
7✔
189
        if (genericPartIndex <= 0)
7!
190
        {
UNCOV
191
            if (type.Namespace.HasValue() && _defaultNamespaces.Contains(type.Namespace))
×
192
            {
193
                builder.Append(type.Name);
×
194
            }
195
            else
196
            {
UNCOV
197
                builder.Append(type.FullName ?? type.Name);
×
198
            }
199
            return;
×
200
        }
201

202
        if (type.Namespace.HasValue() && !_defaultNamespaces.Contains(type.Namespace))
7!
203
        {
204
            builder.Append(type.Namespace);
1✔
205
            builder.Append(".");
1✔
206
        }
207
        builder.Append(type.Name, 0, genericPartIndex);
7✔
208
        builder.Append('<');
7✔
209

210
        for (var i = offset; i < length; i++)
32✔
211
        {
212
            ProcessType(builder, genericArguments[i]);
9✔
213
            if (i + 1 == length)
9✔
214
            {
215
                continue;
216
            }
217

218
            builder.Append(',');
2✔
219
            if (!genericArguments[i + 1].IsGenericParameter)
2!
220
            {
221
                builder.Append(' ');
2✔
222
            }
223
        }
224

225
        builder.Append('>');
7✔
226
    }
7✔
227
}
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

© 2026 Coveralls, Inc