generated from dailydevops/dotnet-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Create integration test project for NetEvolve.CodeBuilder library #13
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e8fed51
chore: Create integration test project with TUnit framework and basic…
Copilot d5f2b48
chore: Complete integration test project with documentation and snaps…
Copilot 1df8e44
chore(test): Updated tests
samtrion bae46c7
style: Reformatted code
samtrion d2ddbac
fix: Warnings from the Code Review
samtrion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
tests/NetEvolve.CodeBuilder.Tests.Integration/CSharpCodeBuilderTests.ComplexGeneration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
165 changes: 165 additions & 0 deletions
165
...s/NetEvolve.CodeBuilder.Tests.Integration/CSharpCodeBuilderTests.ConditionalGeneration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.