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

loresoft / EntityFrameworkCore.Generator / 15072048022

16 May 2025 03:31PM UTC coverage: 55.392% (-1.4%) from 56.772%
15072048022

push

github

pwelter34
enable nullable support

616 of 1271 branches covered (48.47%)

Branch coverage included in aggregate %.

233 of 397 new or added lines in 61 files covered. (58.69%)

17 existing lines in 11 files now uncovered.

1824 of 3134 relevant lines covered (58.2%)

88.56 hits per line

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

87.74
/src/EntityFrameworkCore.Generator.Core/VariableDictionary.cs
1
using System;
2
using System.Collections.Generic;
3
using System.IO;
4
using System.Text;
5

6
namespace EntityFrameworkCore.Generator;
7

8
/// <summary>
9
/// Variable substitution dictionary
10
/// </summary>
11
public class VariableDictionary
12
{
13
    private readonly Dictionary<string, string?> _variables = new(StringComparer.OrdinalIgnoreCase);
18✔
14

15

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

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

35

36
    /// <summary>
37
    /// Sets a variable value.
38
    /// </summary>
39
    /// <param name="name">The name of the variable.</param>
40
    /// <param name="value">The value of the variable.</param>
41
    public void Set(string name, string? value)
42
    {
43
        if (name == null)
1,026!
44
            return;
×
45

46
        _variables[name] = value;
1,026✔
47
    }
1,026✔
48

49
    /// <summary>
50
    /// Sets the specified option variable.
51
    /// </summary>
52
    /// <param name="optionVariable">The option variable.</param>
53
    public void Set(IOptionVariable optionVariable)
54
    {
55
        optionVariable.Set(this);
114✔
56
    }
114✔
57

58
    /// <summary>
59
    /// 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.
60
    /// </summary>
61
    /// <param name="name">The name of the variable.</param>
62
    /// <returns>
63
    /// The value of the variable, or <c>null</c> if the variable is not defined.
64
    /// </returns>
65
    public string? Get(string name)
66
    {
67
        if (!_variables.TryGetValue(name, out var variable))
352✔
68
            return null;
99✔
69

70

71
        return ShouldEvaluate ? Evaluate(variable) : variable;
253!
72
    }
73

74
    /// <summary>
75
    /// Removes the variable with specified name.
76
    /// </summary>
77
    /// <param name="name">The variable name.</param>
78
    public void Remove(string name)
79
    {
80
        if (name == null)
352!
81
            return;
×
82

83
        _variables.Remove(name);
352✔
84
    }
352✔
85

86
    /// <summary>
87
    /// Removes the specified option variable.
88
    /// </summary>
89
    /// <param name="optionVariable">The option variable.</param>
90
    public void Remove(IOptionVariable optionVariable)
91
    {
92
        optionVariable.Remove(this);
114✔
93
    }
114✔
94

95
    /// <summary>
96
    /// Evaluates the specified variable or text.
97
    /// </summary>
98
    /// <param name="variableOrText">The variable or text.</param>
99
    /// <returns>The result of the variable.</returns>
100
    /// <exception cref="System.FormatException">Invalid variable format</exception>
101
    public string? Evaluate(string? variableOrText)
102
    {
103
        if (variableOrText == null)
253✔
104
            return null;
6✔
105

106
        var loop = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
247✔
107
        return Eval(variableOrText, loop);
247✔
108
    }
109

110

111
    private string? Eval(string? variableOrText, ISet<string> loop)
112
    {
113
        if (variableOrText == null)
580!
NEW
114
            return null;
×
115

116
        var result = new StringBuilder(variableOrText.Length * 2);
580✔
117
        var variable = new StringBuilder();
580✔
118
        var state = State.OutsideExpression;
580✔
119

120
        using (var reader = new StringReader(variableOrText))
580✔
121
        {
122
            do
123
            {
124
                int c = -1;
11,482!
125
                switch (state)
126
                {
127
                    case State.OutsideExpression:
128
                        c = reader.Read();
6,075✔
129
                        switch (c)
130
                        {
131
                            case -1:
132
                                state = State.End;
578✔
133
                                break;
578✔
134
                            case '{':
135
                                state = State.OnOpenBracket;
337✔
136
                                break;
337✔
137
                            case '}':
138
                                state = State.OnCloseBracket;
2✔
139
                                break;
2✔
140
                            default:
141
                                result.Append((char) c);
5,158✔
142
                                break;
5,158✔
143
                        }
144

145
                        break;
146
                    case State.OnOpenBracket:
147
                        c = reader.Read();
337!
148
                        switch (c)
149
                        {
150
                            case -1:
151
                                throw new FormatException();
×
152
                            case '{':
153
                                result.Append('{');
1✔
154
                                state = State.OutsideExpression;
1✔
155
                                break;
1✔
156
                            default:
157
                                variable.Append((char) c);
336✔
158
                                state = State.InsideExpression;
336✔
159
                                break;
336✔
160
                        }
161

162
                        break;
163
                    case State.InsideExpression:
164
                        c = reader.Read();
5,068✔
165
                        switch (c)
166
                        {
167
                            case -1:
168
                                throw new FormatException();
1✔
169
                            case '}':
170

171
                                var v = variable.ToString();
335✔
172
                                if (loop.Add(v) && _variables.TryGetValue(v, out string? value))
335✔
173
                                {
174
                                    value = Eval(value, loop);
333✔
175
                                    result.Append(value);
333✔
176
                                }
177

178
                                variable.Length = 0;
335✔
179
                                state = State.OutsideExpression;
335✔
180
                                break;
335✔
181
                            default:
182
                                variable.Append((char) c);
4,732✔
183
                                break;
4,732✔
184
                        }
185

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

199
                        break;
200
                    default:
201
                        throw new FormatException("Invalid parse state.");
×
202
                }
203
            } while (state != State.End);
11,480✔
204
        }
578✔
205

206
        return result.ToString();
578✔
207
    }
208

209

210
    private enum State
211
    {
212
        OutsideExpression,
213
        OnOpenBracket,
214
        InsideExpression,
215
        OnCloseBracket,
216
        End
217
    }
218

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