Skip to content

Commit

Permalink
Merge pull request #54 from vmachacek/master
Browse files Browse the repository at this point in the history
Add method override for WithBaseCall
  • Loading branch information
dansiegel authored Dec 15, 2024
2 parents 9133bcf + 75bfda0 commit fbdd547
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
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);
}
}

0 comments on commit fbdd547

Please sign in to comment.