Skip to content

Commit fbdd547

Browse files
authoredDec 15, 2024··
Merge pull request #54 from vmachacek/master
Add method override for WithBaseCall
2 parents 9133bcf + 75bfda0 commit fbdd547

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed
 

‎src/CodeGenHelpers/ConstructorBuilder.cs

+21-9
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,35 @@ public ConstructorBuilder WithThisCall()
101101
}
102102

103103
public ConstructorBuilder WithThisCall(Dictionary<string, string> parameters)
104+
{
105+
WithThisCall(parameters.Select(f => (f.Key, f.Value)));
106+
return this;
107+
}
108+
109+
public ConstructorBuilder WithThisCall(IEnumerable<(string Type, string Name)> parameters)
104110
{
105111
foreach(var parameter in parameters)
106112
{
107-
this.AddParameter(parameter.Key, parameter.Value);
113+
this.AddParameter(parameter.Type, parameter.Name);
108114
}
109115

110116
_baseCall = () =>
111117
{
112-
var output = parameters.Select(x => x.Value);
118+
var output = parameters.Select(x => x.Name);
113119
return $": this({string.Join(", ", output)})";
114120
};
115121
return this;
116122
}
117123

118124
public ConstructorBuilder WithThisCall(IEnumerable<IParameterSymbol> parameters)
119125
{
120-
var dict = new Dictionary<string, string>();
126+
var list = new List<(string Type, string Name)>();
121127
foreach (var parameter in parameters ?? Array.Empty<IParameterSymbol>())
122128
{
123129
AddNamespaceImport(parameter.Type);
124-
dict.Add(parameter.Type.Name, parameter.Name);
130+
list.Add((parameter.Type.Name, parameter.Name));
125131
}
126-
return WithThisCall(dict);
132+
return WithThisCall(list);
127133
}
128134

129135
public ConstructorBuilder WithThisCall(IMethodSymbol baseConstructor)
@@ -138,27 +144,33 @@ public ConstructorBuilder WithBaseCall()
138144
}
139145

140146
public ConstructorBuilder WithBaseCall(Dictionary<string, string> parameters)
147+
{
148+
WithBaseCall(parameters.Select(f => (f.Value, f.Key)));
149+
return this;
150+
}
151+
152+
public ConstructorBuilder WithBaseCall(IEnumerable<(string Type, string Name)> parameters)
141153
{
142154
foreach (var parameter in parameters)
143155
{
144-
this.AddParameter(parameter.Key, parameter.Value);
156+
this.AddParameter(parameter.Type, parameter.Name);
145157
}
146158

147159
_baseCall = () =>
148160
{
149-
var output = parameters.Select(x => x.Value);
161+
var output = parameters.Select(x => x.Name);
150162
return $": base({string.Join(", ", output)})";
151163
};
152164
return this;
153165
}
154166

155167
public ConstructorBuilder WithBaseCall(IEnumerable<IParameterSymbol> parameters)
156168
{
157-
var dict = new Dictionary<string, string>();
169+
var dict = new List<(string Type, string Name)>();
158170
foreach (var parameter in parameters ?? Array.Empty<IParameterSymbol>())
159171
{
160172
AddNamespaceImport(parameter.Type);
161-
dict.Add(parameter.Type.Name, parameter.Name);
173+
dict.Add((parameter.Type.Name, parameter.Name));
162174
}
163175
return WithBaseCall(dict);
164176
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
namespace CodeGenHelpers.SampleCode
11+
{
12+
partial class CanPassMultipleParametersOfSameType
13+
{
14+
CanPassMultipleParametersOfSameType(string str1, string str2)
15+
: base(str1, str2)
16+
{
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Xunit;
2+
using Xunit.Abstractions;
3+
4+
namespace CodeGenHelpers.Tests;
5+
6+
public class ConstructorBuilderTests : TestBase
7+
{
8+
public ConstructorBuilderTests(ITestOutputHelper testOutput)
9+
: base(testOutput)
10+
{
11+
}
12+
13+
[Fact]
14+
public void CanPassMultipleParametersOfSameType()
15+
{
16+
var builder = CodeBuilder.Create(Namespace)
17+
.AddClass("CanPassMultipleParametersOfSameType");
18+
19+
builder.AddConstructor().WithBaseCall(new[]
20+
{
21+
("string", "str1"),
22+
("string", "str2"),
23+
});
24+
25+
MakeAssertion(builder);
26+
}
27+
}

0 commit comments

Comments
 (0)
Please sign in to comment.