-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpCodeBuilderTests.ConditionalGeneration.cs
More file actions
165 lines (142 loc) · 4.94 KB
/
CSharpCodeBuilderTests.ConditionalGeneration.cs
File metadata and controls
165 lines (142 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
namespace NetEvolve.CodeBuilder.Tests.Integration;
using System.Globalization;
using System.Linq;
public partial class CSharpCodeBuilderTests
{
[Test]
public async Task GenerateMethodWithConditionalContent_Should_ProduceCorrectOutput()
{
var builder = new CSharpCodeBuilder();
var includeLogging = true;
var includeValidation = false;
var isAsync = true;
_ = builder.AppendLine("public class ServiceClass").Append("{");
if (isAsync)
{
_ = builder.Append("public async Task");
}
else
{
_ = builder.Append("public void");
}
_ = builder
.Append(" ProcessDataAsync(string input)")
.Append("{")
.AppendLineIf(includeValidation, "if (string.IsNullOrEmpty(input))")
.AppendLineIf(includeValidation, "{")
.AppendLineIf(
includeValidation,
" throw new ArgumentException(\"Input cannot be null or empty\", nameof(input));"
)
.AppendLineIf(includeValidation, "}")
.AppendLineIf(includeValidation, "")
.AppendLineIf(includeLogging, "Console.WriteLine($\"Processing input: {input}\");")
.AppendLine("var result = input.ToUpperInvariant();")
.AppendLineIf(includeLogging, "Console.WriteLine($\"Processing complete: {result}\");")
.AppendLineIf(isAsync, "await Task.CompletedTask;")
.AppendLine("return result;")
.Append("}")
.Append("}");
var result = builder.ToString();
_ = await Verify(result);
}
[Test]
public async Task GenerateReflectionBasedCode_Should_ProduceCorrectOutput()
{
var builder = new CSharpCodeBuilder();
var properties = new[]
{
new
{
Name = "Id",
Type = "int",
HasGetter = true,
HasSetter = false,
},
new
{
Name = "Name",
Type = "string?",
HasGetter = true,
HasSetter = true,
},
new
{
Name = "Email",
Type = "string?",
HasGetter = true,
HasSetter = true,
},
new
{
Name = "CreatedAt",
Type = "DateTime",
HasGetter = true,
HasSetter = false,
},
};
_ = builder.AppendLine("using System;").AppendLine().AppendLine("public class GeneratedEntity").Append("{");
// Generate backing fields for properties without setters
foreach (var prop in properties.Where(p => !p.HasSetter))
{
_ = builder
.AppendFormat(
CultureInfo.InvariantCulture,
"private readonly {0} _{1};",
prop.Type,
prop.Name.ToUpperInvariant()
)
.AppendLine();
}
if (properties.Any(p => !p.HasSetter))
{
_ = builder.AppendLine();
}
// Generate constructor
var readOnlyProps = properties.Where(p => !p.HasSetter).ToArray();
if (readOnlyProps.Length > 0)
{
_ = builder.Append("public GeneratedEntity(");
for (var i = 0; i < readOnlyProps.Length; i++)
{
if (i > 0)
{
_ = builder.Append(", ");
}
_ = builder.AppendFormat(
CultureInfo.InvariantCulture,
"{0} {1}",
readOnlyProps[i].Type,
readOnlyProps[i].Name.ToUpperInvariant()
);
}
_ = builder.AppendLine(")").Append("{");
foreach (var propertyName in readOnlyProps.Select(x => x.Name.ToUpperInvariant()))
{
_ = builder
.AppendFormat(CultureInfo.InvariantCulture, "_{0} = {1};", propertyName, propertyName)
.AppendLine();
}
_ = builder.Append("}").AppendLine();
}
// Generate properties
foreach (var prop in properties)
{
_ = builder.AppendFormat(CultureInfo.InvariantCulture, "public {0} {1}", prop.Type, prop.Name);
if (prop.HasGetter && prop.HasSetter)
{
_ = builder.AppendLine(" { get; set; }");
}
else if (prop.HasGetter && !prop.HasSetter)
{
_ = builder
.AppendFormat(CultureInfo.InvariantCulture, " => _{0};", prop.Name.ToUpperInvariant())
.AppendLine();
}
_ = builder.AppendLine();
}
_ = builder.Append("}");
var result = builder.ToString();
_ = await Verify(result);
}
}