• 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

87.62
/src/EntityFrameworkCore.Generator.Core/VariableDictionary.cs
1
using System.Text;
2

3
namespace EntityFrameworkCore.Generator;
4

5
/// <summary>
6
/// Variable substitution dictionary
7
/// </summary>
8
public class VariableDictionary
9
{
10
    private readonly Dictionary<string, string?> _variables = new(StringComparer.OrdinalIgnoreCase);
24✔
11

12

13
    /// <summary>
14
    /// Gets or sets a value indicating whether <see cref="Get"/> should evaluate variable expressions.
15
    /// </summary>
16
    /// <value>
17
    ///   <c>true</c> if <see cref="Get"/> should evaluate; otherwise, <c>false</c>.
18
    /// </value>
19
    public bool ShouldEvaluate { get; set; } = true;
20

21
    /// <summary>
22
    /// Gets or sets a variable by name.
23
    /// </summary>
24
    /// <param name="name">The name of the variable to set.</param>
25
    /// <returns>The current (evaluated) value of the variable.</returns>
26
    public string? this[string name]
27
    {
UNCOV
28
        get => Get(name);
×
UNCOV
29
        set => Set(name, value);
×
30
    }
31

32

33
    /// <summary>
34
    /// Sets a variable value.
35
    /// </summary>
36
    /// <param name="name">The name of the variable.</param>
37
    /// <param name="value">The value of the variable.</param>
38
    public void Set(string name, string? value)
39
    {
40
        if (name == null)
5,095!
UNCOV
41
            return;
×
42

43
        _variables[name] = value;
5,095✔
44
    }
5,095✔
45

46
    /// <summary>
47
    /// Sets the specified option variable.
48
    /// </summary>
49
    /// <param name="optionVariable">The option variable.</param>
50
    public void Set(IOptionVariable optionVariable)
51
    {
52
        optionVariable.Set(this);
1,949✔
53
    }
1,949✔
54

55
    /// <summary>
56
    /// Gets the value of a variable, or returns <c>null</c> if the variable is not defined. If the variable contains an expression, it will be evaluated first.
57
    /// </summary>
58
    /// <param name="name">The name of the variable.</param>
59
    /// <returns>
60
    /// The value of the variable, or <c>null</c> if the variable is not defined.
61
    /// </returns>
62
    public string? Get(string name)
63
    {
64
        if (!_variables.TryGetValue(name, out var variable))
36,739✔
65
            return null;
148✔
66

67

68
        return ShouldEvaluate ? Evaluate(variable) : variable;
36,591!
69
    }
70

71
    /// <summary>
72
    /// Removes the variable with specified name.
73
    /// </summary>
74
    /// <param name="name">The variable name.</param>
75
    public void Remove(string name)
76
    {
77
        if (name == null)
3,368!
UNCOV
78
            return;
×
79

80
        _variables.Remove(name);
3,368✔
81
    }
3,368✔
82

83
    /// <summary>
84
    /// Removes the specified option variable.
85
    /// </summary>
86
    /// <param name="optionVariable">The option variable.</param>
87
    public void Remove(IOptionVariable optionVariable)
88
    {
89
        optionVariable.Remove(this);
1,812✔
90
    }
1,812✔
91

92
    /// <summary>
93
    /// Evaluates the specified variable or text.
94
    /// </summary>
95
    /// <param name="variableOrText">The variable or text.</param>
96
    /// <returns>The result of the variable.</returns>
97
    /// <exception cref="System.FormatException">Invalid variable format</exception>
98
    public string? Evaluate(string? variableOrText)
99
    {
100
        if (variableOrText == null)
36,591✔
101
            return null;
13,192✔
102

103
        var loop = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
23,399✔
104
        return Eval(variableOrText, loop);
23,399✔
105
    }
106

107

108
    private string? Eval(string? variableOrText, ISet<string> loop)
109
    {
110
        if (variableOrText == null)
28,343!
UNCOV
111
            return null;
×
112

113
        var result = new StringBuilder(variableOrText.Length * 2);
28,343✔
114
        var variable = new StringBuilder();
28,343✔
115
        var state = State.OutsideExpression;
28,343✔
116

117
        using (var reader = new StringReader(variableOrText))
28,343✔
118
        {
119
            do
120
            {
121
                int c = -1;
684,925!
122
                switch (state)
123
                {
124
                    case State.OutsideExpression:
125
                        c = reader.Read();
609,930✔
126
                        switch (c)
127
                        {
128
                            case -1:
129
                                state = State.End;
28,341✔
130
                                break;
28,341✔
131
                            case '{':
132
                                state = State.OnOpenBracket;
4,948✔
133
                                break;
4,948✔
134
                            case '}':
135
                                state = State.OnCloseBracket;
2✔
136
                                break;
2✔
137
                            default:
138
                                result.Append((char)c);
576,639✔
139
                                break;
576,639✔
140
                        }
141

142
                        break;
143
                    case State.OnOpenBracket:
144
                        c = reader.Read();
4,948!
145
                        switch (c)
146
                        {
147
                            case -1:
UNCOV
148
                                throw new FormatException();
×
149
                            case '{':
150
                                result.Append('{');
1✔
151
                                state = State.OutsideExpression;
1✔
152
                                break;
1✔
153
                            default:
154
                                variable.Append((char)c);
4,947✔
155
                                state = State.InsideExpression;
4,947✔
156
                                break;
4,947✔
157
                        }
158

159
                        break;
160
                    case State.InsideExpression:
161
                        c = reader.Read();
70,045✔
162
                        switch (c)
163
                        {
164
                            case -1:
165
                                throw new FormatException();
1✔
166
                            case '}':
167

168
                                var v = variable.ToString();
4,946✔
169
                                if (loop.Add(v) && _variables.TryGetValue(v, out string? value))
4,946✔
170
                                {
171
                                    value = Eval(value, loop);
4,944✔
172
                                    result.Append(value);
4,944✔
173
                                }
174

175
                                variable.Length = 0;
4,946✔
176
                                state = State.OutsideExpression;
4,946✔
177
                                break;
4,946✔
178
                            default:
179
                                variable.Append((char)c);
65,098✔
180
                                break;
65,098✔
181
                        }
182

183
                        break;
184
                    case State.OnCloseBracket:
185
                        c = reader.Read();
2✔
186
                        switch (c)
187
                        {
188
                            case '}':
189
                                result.Append('}');
1✔
190
                                state = State.OutsideExpression;
1✔
191
                                break;
1✔
192
                            default:
193
                                throw new FormatException();
1✔
194
                        }
195

196
                        break;
197
                    default:
UNCOV
198
                        throw new FormatException("Invalid parse state.");
×
199
                }
200
            } while (state != State.End);
684,923✔
201
        }
28,341✔
202

203
        return result.ToString();
28,341✔
204
    }
205

206

207
    private enum State
208
    {
209
        OutsideExpression,
210
        OnOpenBracket,
211
        InsideExpression,
212
        OnCloseBracket,
213
        End
214
    }
215

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