Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method override for WithBaseCall #54

Merged
merged 3 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/CodeGenHelpers/ConstructorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,35 @@ public ConstructorBuilder WithThisCall()
}

public ConstructorBuilder WithThisCall(Dictionary<string, string> parameters)
{
WithThisCall(parameters.Select(f => (f.Key, f.Value)));
return this;
}

public ConstructorBuilder WithThisCall(IEnumerable<(string Type, string Name)> parameters)
{
foreach(var parameter in parameters)
{
this.AddParameter(parameter.Key, parameter.Value);
this.AddParameter(parameter.Type, parameter.Name);
}

_baseCall = () =>
{
var output = parameters.Select(x => x.Value);
var output = parameters.Select(x => x.Name);
return $": this({string.Join(", ", output)})";
};
return this;
}

public ConstructorBuilder WithThisCall(IEnumerable<IParameterSymbol> parameters)
{
var dict = new Dictionary<string, string>();
var list = new List<(string Type, string Name)>();
foreach (var parameter in parameters ?? Array.Empty<IParameterSymbol>())
{
AddNamespaceImport(parameter.Type);
dict.Add(parameter.Type.Name, parameter.Name);
list.Add((parameter.Type.Name, parameter.Name));
}
return WithThisCall(dict);
return WithThisCall(list);
}

public ConstructorBuilder WithThisCall(IMethodSymbol baseConstructor)
Expand All @@ -138,27 +144,33 @@ public ConstructorBuilder WithBaseCall()
}

public ConstructorBuilder WithBaseCall(Dictionary<string, string> parameters)
{
WithBaseCall(parameters.Select(f => (f.Value, f.Key)));
return this;
}

public ConstructorBuilder WithBaseCall(IEnumerable<(string Type, string Name)> parameters)
{
foreach (var parameter in parameters)
{
this.AddParameter(parameter.Key, parameter.Value);
this.AddParameter(parameter.Type, parameter.Name);
}

_baseCall = () =>
{
var output = parameters.Select(x => x.Value);
var output = parameters.Select(x => x.Name);
return $": base({string.Join(", ", output)})";
};
return this;
}

public ConstructorBuilder WithBaseCall(IEnumerable<IParameterSymbol> parameters)
{
var dict = new Dictionary<string, string>();
var dict = new List<(string Type, string Name)>();
foreach (var parameter in parameters ?? Array.Empty<IParameterSymbol>())
{
AddNamespaceImport(parameter.Type);
dict.Add(parameter.Type.Name, parameter.Name);
dict.Add((parameter.Type.Name, parameter.Name));
}
return WithBaseCall(dict);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace CodeGenHelpers.SampleCode
{
partial class CanPassMultipleParametersOfSameType
{
CanPassMultipleParametersOfSameType(string str1, string str2)
: base(str1, str2)
{
}
}
}
27 changes: 27 additions & 0 deletions tests/CodeGenHelpers.Tests/Tests/ConstructorBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Xunit;
using Xunit.Abstractions;

namespace CodeGenHelpers.Tests;

public class ConstructorBuilderTests : TestBase
{
public ConstructorBuilderTests(ITestOutputHelper testOutput)
: base(testOutput)
{
}

[Fact]
public void CanPassMultipleParametersOfSameType()
{
var builder = CodeBuilder.Create(Namespace)
.AddClass("CanPassMultipleParametersOfSameType");

builder.AddConstructor().WithBaseCall(new[]
{
("string", "str1"),
("string", "str2"),
});

MakeAssertion(builder);
}
}
Loading