Skip to content
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
1 change: 1 addition & 0 deletions CodeBuilder.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
</Folder>
<Folder Name="/tests/">
<Project Path="tests/NetEvolve.CodeBuilder.Tests.Unit/NetEvolve.CodeBuilder.Tests.Unit.csproj" />
<Project Path="tests/NetEvolve.CodeBuilder.Tests.Integration/NetEvolve.CodeBuilder.Tests.Integration.csproj" />
</Folder>
</Solution>
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<CopyrightYearStart>2024</CopyrightYearStart>
<_DefaultTargetFrameworks>net8.0;net9.0;net10.0</_DefaultTargetFrameworks>
<_ProjectTargetFrameworks>netstandard2.0;netstandard2.1;$(_DefaultTargetFrameworks)</_ProjectTargetFrameworks>
<_TestTargetFrameworks>net6.0;net7.0;$(_DefaultTargetFrameworks)</_TestTargetFrameworks>
<_TestTargetFrameworks>$(_DefaultTargetFrameworks)</_TestTargetFrameworks>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
</PropertyGroup>
<PropertyGroup>
Expand Down
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
<PackageVersion Include="NetEvolve.Extensions.TUnit" Version="2.7.0" />
<PackageVersion Include="System.Memory" Version="4.6.3" />
<PackageVersion Include="TUnit" Version="0.57.1" />
<PackageVersion Include="Verify.ParametersHashing" Version="1.0.0" />
<PackageVersion Include="Verify.TUnit" Version="30.7.3" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace NetEvolve.CodeBuilder;

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
namespace NetEvolve.CodeBuilder.Tests.Integration;

public partial class CSharpCodeBuilderTests
{
[Test]
public async Task GenerateCompleteClass_Should_ProduceCorrectOutput()
{
// Build a complete class with using statements, namespace, and methods
var builder = new CSharpCodeBuilder()
.AppendLine("using System;")
.AppendLine("using System.Collections.Generic;")
.AppendLine()
.AppendLine("namespace MyApplication.Models")
.Append("{")
.AppendLine("/// <summary>")
.AppendLine("/// Represents a customer entity.")
.AppendLine("/// </summary>")
.AppendLine("public class Customer")
.Append("{")
.AppendLine("private readonly string _id;")
.AppendLine()
.AppendLine("/// <summary>")
.AppendLine("/// Initializes a new instance of the Customer class.")
.AppendLine("/// </summary>")
.AppendLine("/// <param name=\"id\">The customer identifier.</param>")
.AppendLine("public Customer(string id)")
.Append("{")
.AppendLine("_id = id ?? throw new ArgumentNullException(nameof(id));")
.Append("}")
.AppendLine()
.AppendLine("/// <summary>")
.AppendLine("/// Gets the customer identifier.")
.AppendLine("/// </summary>")
.AppendLine("public string Id => _id;")
.AppendLine()
.AppendLine("/// <summary>")
.AppendLine("/// Gets or sets the customer name.")
.AppendLine("/// </summary>")
.AppendLine("public string? Name { get; set; }")
.AppendLine()
.AppendLine("/// <summary>")
.AppendLine("/// Gets or sets the customer email address.")
.AppendLine("/// </summary>")
.Append("public string? Email { get; set; }")
.Append("}")
.Append("}");

var result = builder.ToString();

_ = await Verify(result);
}

[Test]
public async Task GenerateInterface_WithMultipleMethods_Should_ProduceCorrectOutput()
{
var builder = new CSharpCodeBuilder()
.AppendLine("using System;")
.AppendLine("using System.Threading.Tasks;")
.AppendLine()
.AppendLine("namespace MyApplication.Services")
.Append("{")
.AppendXmlDocSummary("Defines the contract for customer service operations.")
.AppendLine("public interface ICustomerService")
.Append("{")
.AppendXmlDocSummary("Gets a customer by their identifier.")
.AppendXmlDocParam("id", "The customer identifier.")
.AppendXmlDocReturns("The customer if found; otherwise, null.")
.AppendLine("Task<Customer?> GetCustomerAsync(string id);")
.AppendLine()
.AppendXmlDocSummary("Creates a new customer.")
.AppendXmlDocParam("customer", "The customer to create.")
.AppendXmlDocReturns("A task representing the asynchronous operation.")
.AppendLine("Task CreateCustomerAsync(Customer customer);")
.AppendLine()
.AppendXmlDocSummary("Updates an existing customer.")
.AppendXmlDocParam("customer", "The customer to update.")
.AppendXmlDocReturns("A task representing the asynchronous operation.")
.Append("Task UpdateCustomerAsync(Customer customer);")
.Append("}")
.Append("}");

var result = builder.ToString();

_ = await Verify(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}
}
Loading