Skip to content

Commit

Permalink
refactor: Added indicator for auto-generated code and nullability+
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Dec 28, 2023
1 parent 3adb978 commit 58f7180
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public InterceptsLocationAttribute(string filePath, int line, int column)
""";

// Generate the interceptor
var interceptorSource = new StringBuilder();
var interceptorSource = new StringBuilder(1000);
interceptorSource.AppendLine(HeaderProvider.Header);
interceptorSource.AppendLine(attribute);
interceptorSource.AppendLine();
interceptorSource.AppendLine("namespace Bunit");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public static bool GenerateStubComponent(AddStubClassInfo classInfo, SourceProdu
{
var hasSomethingToStub = false;
var targetTypeSymbol = (INamedTypeSymbol)classInfo!.TargetType;
var sourceBuilder = new StringBuilder();
var sourceBuilder = new StringBuilder(1000);

sourceBuilder.AppendLine(HeaderProvider.Header);
sourceBuilder.AppendLine($"namespace {classInfo.TargetTypeNamespace};");
sourceBuilder.AppendLine();
sourceBuilder.AppendLine($"internal partial class {classInfo.StubClassName} : global::Microsoft.AspNetCore.Components.ComponentBase");
Expand All @@ -39,7 +40,7 @@ public static bool GenerateStubComponent(AddStubClassInfo classInfo, SourceProdu
var attributeLine = GetAttributeLine(member);
sourceBuilder.AppendLine(attributeLine);

sourceBuilder.AppendLine($"\tpublic {propertyType} {propertyName} {{ get; set; }}");
sourceBuilder.AppendLine($"\tpublic {propertyType} {propertyName} {{ get; set; }} = default!;");
}

sourceBuilder.AppendLine("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ namespace Bunit.Web.Stubs.AttributeStubGenerator;

internal static class ComponentStubAttribute
{
public static string ComponentStubAttributeSource = """
public static string ComponentStubAttributeSource = $$"""
{{HeaderProvider.Header}}
#if NET5_0_OR_GREATER
namespace Bunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ private static void Execute(StubClassInfo classInfo, SourceProductionContext con
{
var hasSomethingToStub = false;
var targetTypeSymbol = (INamedTypeSymbol)classInfo!.TargetType;
var sourceBuilder = new StringBuilder();
var sourceBuilder = new StringBuilder(1000);

sourceBuilder.AppendLine(HeaderProvider.Header);
sourceBuilder.AppendLine($"namespace {classInfo.Namespace};");

sourceBuilder.AppendLine(
Expand Down Expand Up @@ -104,7 +105,7 @@ private static void Execute(StubClassInfo classInfo, SourceProductionContext con
: "\t[global::Microsoft.AspNetCore.Components.CascadingParameter]";

sourceBuilder.AppendLine(attributeLine);
sourceBuilder.AppendLine($"\tpublic {propertyType} {propertyName} {{ get; set; }}");
sourceBuilder.AppendLine($"\tpublic {propertyType} {propertyName} {{ get; set; }} = default!;");
}

sourceBuilder.AppendLine("}");
Expand Down
13 changes: 13 additions & 0 deletions src/bunit.generators/Web.Stubs/HeaderProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Bunit.Web.Stubs;

internal static class HeaderProvider
{
public const string Header = """
// <auto-generated>
// This code was generated by bunit.generators.
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
#nullable enable
""";
}

0 comments on commit 58f7180

Please sign in to comment.