diff --git a/docs/index.md b/docs/index.md index c81d9ca..dd6cfdf 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,6 +1,6 @@ # BuilderGenerator -This NuGet package automates the generation of object builders for testing. It generates the repetitive part of creating builders, leaving only the more interesting, hand-curated parts for you to create as partial classes. +This NuGet package automates the generation of object builders for testing. It generates the repetitive part of creating builders, leaving only the more interesting, hand-curated parts for you to implement as partial classes. With the v2.0 release, the mechanism has changed. Rather than decorating entity classes with the "GenerateBuilder" attribute, as was done in v1.x, you now create a partial builder class and decorate it with the "BuilderFor" attribute. This makes it possible to generate builders anywhere you want rather than always creating them in the same assembly as the entities. Builders can be used as part of normal development, but are most commonly used in testing scenarios and many development teams would prefer not to "pollute" their domain assembly with knowledge of classes created for the benefit of their test assemblies. Of course, if you want to use builders as part of normal business logic, you can simply create and decorate the partial builder classes in the domain assembly and achieve the same effect as in the v1.x releases. diff --git a/src/.editorconfig b/src/.editorconfig index bff6dc9..8509c26 100644 --- a/src/.editorconfig +++ b/src/.editorconfig @@ -155,14 +155,6 @@ csharp_prefer_braces = true:suggestion csharp_style_namespace_declarations = file_scoped:silent csharp_style_prefer_method_group_conversion = true:silent csharp_style_prefer_top_level_statements = true:silent -csharp_style_expression_bodied_methods = when_on_single_line:none -csharp_style_expression_bodied_constructors = false:none -csharp_style_expression_bodied_operators = false:none -csharp_style_expression_bodied_properties = true:none -csharp_style_expression_bodied_indexers = true:none -csharp_style_expression_bodied_accessors = true:none -csharp_style_expression_bodied_lambdas = true:silent -csharp_style_expression_bodied_local_functions = false:silent csharp_indent_labels = no_change csharp_style_throw_expression = true:suggestion csharp_style_prefer_null_check_over_type_check = true:suggestion @@ -422,12 +414,14 @@ csharp_style_var_elsewhere=true:suggestion # Expression-bodied members -csharp_style_expression_bodied_methods=when_on_single_line:none -csharp_style_expression_bodied_constructors=false:none -csharp_style_expression_bodied_operators=false:none -csharp_style_expression_bodied_properties=true:none -csharp_style_expression_bodied_indexers=true:none -csharp_style_expression_bodied_accessors=true:none +csharp_style_expression_bodied_methods = false:none +csharp_style_expression_bodied_constructors = false:none +csharp_style_expression_bodied_operators = false:none +csharp_style_expression_bodied_properties = false:none +csharp_style_expression_bodied_indexers = false:none +csharp_style_expression_bodied_accessors = false:none +csharp_style_expression_bodied_lambdas = true:silent +csharp_style_expression_bodied_local_functions = false:silent # Pattern matching: diff --git a/src/BuilderGenerator.IntegrationTests.Net60/Tests/BuilderTests.cs b/src/BuilderGenerator.IntegrationTests.Net60/Tests/BuilderTests.cs index a0a9369..eee3435 100644 --- a/src/BuilderGenerator.IntegrationTests.Net60/Tests/BuilderTests.cs +++ b/src/BuilderGenerator.IntegrationTests.Net60/Tests/BuilderTests.cs @@ -24,7 +24,10 @@ public void Simple_does_not_populate_Orders() } [Test] - public void Simple_returns_a_UserBuilder() => UserBuilder.Simple().ShouldBeOfType(); + public void Simple_returns_a_UserBuilder() + { + UserBuilder.Simple().ShouldBeOfType(); + } [Test] public void Typical_populates_Orders() @@ -35,7 +38,10 @@ public void Typical_populates_Orders() } [Test] - public void Typical_returns_a_UserBuilder() => UserBuilder.Typical().ShouldBeOfType(); + public void Typical_returns_a_UserBuilder() + { + UserBuilder.Typical().ShouldBeOfType(); + } [Test] public void UserBuilder_can_set_properties() diff --git a/src/BuilderGenerator.IntegrationTests.Net60/Tests/OrderBuilderTests.cs b/src/BuilderGenerator.IntegrationTests.Net60/Tests/OrderBuilderTests.cs index 4cdb390..f0c5a6b 100644 --- a/src/BuilderGenerator.IntegrationTests.Net60/Tests/OrderBuilderTests.cs +++ b/src/BuilderGenerator.IntegrationTests.Net60/Tests/OrderBuilderTests.cs @@ -34,7 +34,10 @@ public void Simple_does_not_populate_Items() } [Test] - public void Simple_returns_an_OrderBuilder() => OrderBuilder.Simple().ShouldBeOfType(); + public void Simple_returns_an_OrderBuilder() + { + OrderBuilder.Simple().ShouldBeOfType(); + } [Test] public void Typical_populates_Items() @@ -45,7 +48,10 @@ public void Typical_populates_Items() } [Test] - public void Typical_returns_an_OrderBuilder() => OrderBuilder.Typical().ShouldBeOfType(); + public void Typical_returns_an_OrderBuilder() + { + OrderBuilder.Typical().ShouldBeOfType(); + } [OneTimeSetUp] public void SetUp() diff --git a/src/BuilderGenerator.IntegrationTests.Net60/Tests/UserBuilderTests.cs b/src/BuilderGenerator.IntegrationTests.Net60/Tests/UserBuilderTests.cs index 6b8de27..44f74a0 100644 --- a/src/BuilderGenerator.IntegrationTests.Net60/Tests/UserBuilderTests.cs +++ b/src/BuilderGenerator.IntegrationTests.Net60/Tests/UserBuilderTests.cs @@ -24,7 +24,10 @@ public void Simple_does_not_populate_Orders() } [Test] - public void Simple_returns_a_UserBuilder() => UserBuilder.Simple().ShouldBeOfType(); + public void Simple_returns_a_UserBuilder() + { + UserBuilder.Simple().ShouldBeOfType(); + } [Test] public void Typical_populates_Orders() @@ -35,7 +38,10 @@ public void Typical_populates_Orders() } [Test] - public void Typical_returns_a_UserBuilder() => UserBuilder.Typical().ShouldBeOfType(); + public void Typical_returns_a_UserBuilder() + { + UserBuilder.Typical().ShouldBeOfType(); + } [Test] public void UserBuilder_can_set_properties() diff --git a/src/BuilderGenerator.UnitTests/BuilderGenerator.UnitTests.csproj b/src/BuilderGenerator.UnitTests/BuilderGenerator.UnitTests.csproj index 9ca1ac3..ec324ce 100644 --- a/src/BuilderGenerator.UnitTests/BuilderGenerator.UnitTests.csproj +++ b/src/BuilderGenerator.UnitTests/BuilderGenerator.UnitTests.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -8,13 +8,17 @@ - - + + + + - - + + + + @@ -42,4 +46,4 @@ - + \ No newline at end of file diff --git a/src/BuilderGenerator.UnitTests/Examples/Input.cs b/src/BuilderGenerator.UnitTests/Examples/InputWithInternals.cs similarity index 58% rename from src/BuilderGenerator.UnitTests/Examples/Input.cs rename to src/BuilderGenerator.UnitTests/Examples/InputWithInternals.cs index fe361b2..969264e 100644 --- a/src/BuilderGenerator.UnitTests/Examples/Input.cs +++ b/src/BuilderGenerator.UnitTests/Examples/InputWithInternals.cs @@ -6,10 +6,11 @@ public class Person { public string FirstName { get; set; } public string LastName { get; set; } + internal string InternalString { get; set; } } - [BuilderFor(typeof(Person))] - public partial class PersonBuilder + [BuilderFor(typeof(Person), true)] + public partial class PersonBuilderWithInternals { } } diff --git a/src/BuilderGenerator.UnitTests/Examples/InputWithoutInternals.cs b/src/BuilderGenerator.UnitTests/Examples/InputWithoutInternals.cs new file mode 100644 index 0000000..8013ea0 --- /dev/null +++ b/src/BuilderGenerator.UnitTests/Examples/InputWithoutInternals.cs @@ -0,0 +1,16 @@ +using BuilderGenerator; + +namespace BuilderGenerator.UnitTests +{ + public class Person + { + public string FirstName { get; set; } + public string LastName { get; set; } + internal string InternalString { get; set; } + } + + [BuilderFor(typeof(Person), false)] + public partial class PersonBuilderWithoutInternals + { + } +} diff --git a/src/BuilderGenerator.UnitTests/Examples/OutputWithInternals.cs b/src/BuilderGenerator.UnitTests/Examples/OutputWithInternals.cs new file mode 100644 index 0000000..33df781 --- /dev/null +++ b/src/BuilderGenerator.UnitTests/Examples/OutputWithInternals.cs @@ -0,0 +1,87 @@ +#nullable disable + +using System.CodeDom.Compiler; +using BuilderGenerator; + +namespace BuilderGenerator.UnitTests +{ + public partial class PersonBuilderWithInternals : BuilderGenerator.Builder + { + public System.Lazy FirstName = new System.Lazy(() => default(string)); + public System.Lazy InternalString = new System.Lazy(() => default(string)); + public System.Lazy LastName = new System.Lazy(() => default(string)); + + public override BuilderGenerator.UnitTests.Person Build() + { + if (Object?.IsValueCreated != true) + { + Object = new System.Lazy(() => + { + var result = new BuilderGenerator.UnitTests.Person + { + FirstName = FirstName.Value, + InternalString = InternalString.Value, + LastName = LastName.Value, + }; + + return result; + }); + + PostProcess(Object.Value); + } + + return Object.Value; + } + + public PersonBuilderWithInternals WithFirstName(string value) + { + return WithFirstName(() => value); + } + + public PersonBuilderWithInternals WithFirstName(System.Func func) + { + FirstName = new System.Lazy(func); + return this; + } + + public PersonBuilderWithInternals WithoutFirstName() + { + FirstName = new System.Lazy(() => default(string)); + return this; + } + + public PersonBuilderWithInternals WithInternalString(string value) + { + return WithInternalString(() => value); + } + + public PersonBuilderWithInternals WithInternalString(System.Func func) + { + InternalString = new System.Lazy(func); + return this; + } + + public PersonBuilderWithInternals WithoutInternalString() + { + InternalString = new System.Lazy(() => default(string)); + return this; + } + + public PersonBuilderWithInternals WithLastName(string value) + { + return WithLastName(() => value); + } + + public PersonBuilderWithInternals WithLastName(System.Func func) + { + LastName = new System.Lazy(func); + return this; + } + + public PersonBuilderWithInternals WithoutLastName() + { + LastName = new System.Lazy(() => default(string)); + return this; + } + } +} diff --git a/src/BuilderGenerator.UnitTests/Examples/Output.cs b/src/BuilderGenerator.UnitTests/Examples/OutputWithoutInternals.cs similarity index 72% rename from src/BuilderGenerator.UnitTests/Examples/Output.cs rename to src/BuilderGenerator.UnitTests/Examples/OutputWithoutInternals.cs index fde33cb..afca88c 100644 --- a/src/BuilderGenerator.UnitTests/Examples/Output.cs +++ b/src/BuilderGenerator.UnitTests/Examples/OutputWithoutInternals.cs @@ -5,7 +5,7 @@ namespace BuilderGenerator.UnitTests { - public partial class PersonBuilder : BuilderGenerator.Builder + public partial class PersonBuilderWithoutInternals : BuilderGenerator.Builder { public System.Lazy FirstName = new System.Lazy(() => default(string)); public System.Lazy LastName = new System.Lazy(() => default(string)); @@ -31,35 +31,35 @@ public override BuilderGenerator.UnitTests.Person Build() return Object.Value; } - public PersonBuilder WithFirstName(string value) + public PersonBuilderWithoutInternals WithFirstName(string value) { return WithFirstName(() => value); } - public PersonBuilder WithFirstName(System.Func func) + public PersonBuilderWithoutInternals WithFirstName(System.Func func) { FirstName = new System.Lazy(func); return this; } - public PersonBuilder WithoutFirstName() + public PersonBuilderWithoutInternals WithoutFirstName() { FirstName = new System.Lazy(() => default(string)); return this; } - public PersonBuilder WithLastName(string value) + public PersonBuilderWithoutInternals WithLastName(string value) { return WithLastName(() => value); } - public PersonBuilder WithLastName(System.Func func) + public PersonBuilderWithoutInternals WithLastName(System.Func func) { LastName = new System.Lazy(func); return this; } - public PersonBuilder WithoutLastName() + public PersonBuilderWithoutInternals WithoutLastName() { LastName = new System.Lazy(() => default(string)); return this; diff --git a/src/BuilderGenerator.UnitTests/Given_a_BuilderGenerator.cs b/src/BuilderGenerator.UnitTests/Given_a_BuilderGenerator.cs index 6709cc0..3ba4ffd 100644 --- a/src/BuilderGenerator.UnitTests/Given_a_BuilderGenerator.cs +++ b/src/BuilderGenerator.UnitTests/Given_a_BuilderGenerator.cs @@ -4,43 +4,13 @@ using System.Reflection; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; -using NUnit.Framework; -using Shouldly; + +// ReSharper disable InconsistentNaming namespace BuilderGenerator.UnitTests; -[TestFixture] -public class GeneratorTests +public abstract class Given_a_BuilderGenerator { - [Test] - public void SimpleGeneratorTest() - { - var assembly = GetType().Assembly; - var inputCompilation = CreateCompilation(GetResourceAsString(assembly, "Input.cs")); - var expectedOutput = GetResourceAsString(assembly, "Output.cs"); - GeneratorDriver driver = CSharpGeneratorDriver.Create(new BuilderGenerator()); - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); - diagnostics.ShouldBeEmpty(); - outputCompilation.SyntaxTrees.Count().ShouldBe(4); - - var runResult = driver.GetRunResult(); - runResult.Diagnostics.ShouldBeEmpty(); - runResult.GeneratedTrees.Length.ShouldBe(3); // The Builder base class, the BuilderFor attribute, and the generated builder. - - // TODO: Check for the presence of the Builder base class. - // TODO: Check for the presence of the BuilderForAttribute class. - - var generatorResult = runResult.Results[0]; - generatorResult.Generator.GetGeneratorType().ShouldBe(new BuilderGenerator().GetType()); - generatorResult.Exception.ShouldBeNull(); - generatorResult.GeneratedSources.Length.ShouldBe(3); - - var sourceText = generatorResult.GeneratedSources[2].SourceText.ToString(); - - // Since the generation time will keep changing, we'll just compare everything after the first instance of the word "using". - sourceText[sourceText.IndexOf("using", StringComparison.OrdinalIgnoreCase)..].ShouldBe(expectedOutput[expectedOutput.IndexOf("using", StringComparison.OrdinalIgnoreCase)..]); - } - public static string GetResourceAsString(Assembly assembly, string resourceName) { var manifestResourceNames = assembly.GetManifestResourceNames(); @@ -60,10 +30,12 @@ public static string GetResourceAsString(Assembly assembly, string resourceName) } } - private static Compilation CreateCompilation(string source) - => CSharpCompilation.Create( + protected static Compilation CreateCompilation(string source) + { + return CSharpCompilation.Create( "compilation", new[] { CSharpSyntaxTree.ParseText(source) }, new[] { MetadataReference.CreateFromFile(typeof(Binder).GetTypeInfo().Assembly.Location) }, new CSharpCompilationOptions(OutputKind.ConsoleApplication)); + } } diff --git a/src/BuilderGenerator.UnitTests/Properties/Resources.Designer.cs b/src/BuilderGenerator.UnitTests/Properties/Resources.Designer.cs index 8aa2909..9e017bf 100644 --- a/src/BuilderGenerator.UnitTests/Properties/Resources.Designer.cs +++ b/src/BuilderGenerator.UnitTests/Properties/Resources.Designer.cs @@ -59,52 +59,5 @@ internal Resources() { resourceCulture = value; } } - - /// - /// Looks up a localized string similar to using BuilderGenerator; - /// - ///namespace BuilderGenerator.UnitTests - ///{ - /// public class Person - /// { - /// public string FirstName { get; set; } - /// public string LastName { get; set; } - /// } - /// - /// [BuilderFor(typeof(Person))] - /// public partial class PersonBuilder - /// { - /// } - ///} - ///. - /// - internal static string Input { - get { - return ResourceManager.GetString("Input", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to #nullable disable - /// - ///using System.CodeDom.Compiler; - ///using BuilderGenerator; - /// - ///namespace BuilderGenerator.UnitTests - ///{ - /// public partial class PersonBuilder : BuilderGenerator.Builder<BuilderGenerator.UnitTests.Person> - /// { - /// public System.Lazy<string> FirstName = new System.Lazy<string>(() => default(string)); - /// public System.Lazy<string> LastName = new System.Lazy<string>(() => default(string)); - /// - /// public override BuilderGenerator.UnitTests.Person Build() - /// { - /// [rest of string was truncated]";. - /// - internal static string Output { - get { - return ResourceManager.GetString("Output", resourceCulture); - } - } } } diff --git a/src/BuilderGenerator.UnitTests/Properties/Resources.resx b/src/BuilderGenerator.UnitTests/Properties/Resources.resx index b298613..1af7de1 100644 --- a/src/BuilderGenerator.UnitTests/Properties/Resources.resx +++ b/src/BuilderGenerator.UnitTests/Properties/Resources.resx @@ -117,11 +117,4 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - ..\Examples\Input.cs;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 - - - ..\Examples\Output.cs;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 - \ No newline at end of file diff --git a/src/BuilderGenerator.UnitTests/When_generating_a_Builder_with_internals.cs b/src/BuilderGenerator.UnitTests/When_generating_a_Builder_with_internals.cs new file mode 100644 index 0000000..311ccd6 --- /dev/null +++ b/src/BuilderGenerator.UnitTests/When_generating_a_Builder_with_internals.cs @@ -0,0 +1,43 @@ +using System; +using System.Linq; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using NUnit.Framework; +using Shouldly; + +// ReSharper disable InconsistentNaming + +namespace BuilderGenerator.UnitTests; + +[TestFixture] +public class When_generating_a_Builder_with_internals : Given_a_BuilderGenerator +{ + [Test] + public void SimpleGeneratorTest() + { + var assembly = GetType().Assembly; + var inputCompilation = CreateCompilation(GetResourceAsString(assembly, "InputWithInternals.cs")); + var expectedOutput = GetResourceAsString(assembly, "OutputWithInternals.cs"); + GeneratorDriver driver = CSharpGeneratorDriver.Create(new BuilderGenerator()); + driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); + diagnostics.ShouldBeEmpty(); + outputCompilation.SyntaxTrees.Count().ShouldBe(4); + + var runResult = driver.GetRunResult(); + runResult.Diagnostics.ShouldBeEmpty(); + runResult.GeneratedTrees.Length.ShouldBe(3); // The Builder base class, the BuilderFor attribute, and the generated builder + + // TODO: Check for the presence of the Builder base class. + // TODO: Check for the presence of the BuilderForAttribute class. + + var generatorResult = runResult.Results[0]; + generatorResult.Generator.GetGeneratorType().ShouldBe(new BuilderGenerator().GetType()); + generatorResult.Exception.ShouldBeNull(); + generatorResult.GeneratedSources.Length.ShouldBe(3); + + var sourceText = generatorResult.GeneratedSources[2].SourceText.ToString(); + + // Since the generation time will keep changing, we'll just compare everything after the first instance of the word "using". + sourceText[sourceText.IndexOf("using", StringComparison.OrdinalIgnoreCase)..].ShouldBe(expectedOutput[expectedOutput.IndexOf("using", StringComparison.OrdinalIgnoreCase)..]); + } +} diff --git a/src/BuilderGenerator.UnitTests/When_generating_a_Builder_without_internals.cs b/src/BuilderGenerator.UnitTests/When_generating_a_Builder_without_internals.cs new file mode 100644 index 0000000..eda3b42 --- /dev/null +++ b/src/BuilderGenerator.UnitTests/When_generating_a_Builder_without_internals.cs @@ -0,0 +1,43 @@ +using System; +using System.Linq; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using NUnit.Framework; +using Shouldly; + +// ReSharper disable InconsistentNaming + +namespace BuilderGenerator.UnitTests; + +[TestFixture] +public class When_generating_a_Builder_without_internals : Given_a_BuilderGenerator +{ + [Test] + public void SimpleGeneratorTest() + { + var assembly = GetType().Assembly; + var inputCompilation = CreateCompilation(GetResourceAsString(assembly, "InputWithoutInternals.cs")); + var expectedOutput = GetResourceAsString(assembly, "OutputWithoutInternals.cs"); + GeneratorDriver driver = CSharpGeneratorDriver.Create(new BuilderGenerator()); + driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); + diagnostics.ShouldBeEmpty(); + outputCompilation.SyntaxTrees.Count().ShouldBe(4); + + var runResult = driver.GetRunResult(); + runResult.Diagnostics.ShouldBeEmpty(); + runResult.GeneratedTrees.Length.ShouldBe(3); // The Builder base class, the BuilderFor attribute, and the generated builder + + // TODO: Check for the presence of the Builder base class. + // TODO: Check for the presence of the BuilderForAttribute class. + + var generatorResult = runResult.Results[0]; + generatorResult.Generator.GetGeneratorType().ShouldBe(new BuilderGenerator().GetType()); + generatorResult.Exception.ShouldBeNull(); + generatorResult.GeneratedSources.Length.ShouldBe(3); + + var sourceText = generatorResult.GeneratedSources[2].SourceText.ToString(); + + // Since the generation time will keep changing, we'll just compare everything after the first instance of the word "using". + sourceText[sourceText.IndexOf("using", StringComparison.OrdinalIgnoreCase)..].ShouldBe(expectedOutput[expectedOutput.IndexOf("using", StringComparison.OrdinalIgnoreCase)..]); + } +} diff --git a/src/BuilderGenerator/BuilderGenerator.cs b/src/BuilderGenerator/BuilderGenerator.cs index 0aaf4ac..816d943 100644 --- a/src/BuilderGenerator/BuilderGenerator.cs +++ b/src/BuilderGenerator/BuilderGenerator.cs @@ -177,7 +177,10 @@ private static IEnumerable GetPropertySymbols(INamedTypeSymbol /// The syntax node being examined. /// A cancellation token (currently unused). /// A indicating whether or not might possibly represent a builder class. - private static bool Predicate(SyntaxNode node, CancellationToken _) => node is TypeDeclarationSyntax { AttributeLists.Count: > 0 }; + private static bool Predicate(SyntaxNode node, CancellationToken _) + { + return node is TypeDeclarationSyntax { AttributeLists.Count: > 0 }; + } /// Transforms the syntax node into a containing the information needed to generate a Builder. /// The , which contains a reference to the node. diff --git a/src/BuilderGenerator/BuilderGenerator.csproj b/src/BuilderGenerator/BuilderGenerator.csproj index f2bb486..403bfd5 100644 --- a/src/BuilderGenerator/BuilderGenerator.csproj +++ b/src/BuilderGenerator/BuilderGenerator.csproj @@ -24,7 +24,8 @@ https://github.com/MelGrubb/BuilderGenerator README.md BDD;TDD;Testing;Builders;Code Generation;Source Generators - v2.3.0 + + v2.3.0 - Major caching and performance improvements - Internal code cleanup - Conversion of templates to embedded resources diff --git a/src/BuilderGenerator/Diagnostics/DiagnosticDescriptors.cs b/src/BuilderGenerator/Diagnostics/DiagnosticDescriptors.cs index cfb4c14..2f637f2 100644 --- a/src/BuilderGenerator/Diagnostics/DiagnosticDescriptors.cs +++ b/src/BuilderGenerator/Diagnostics/DiagnosticDescriptors.cs @@ -5,7 +5,13 @@ namespace BuilderGenerator.Diagnostics; internal class DiagnosticDescriptors { - internal static Diagnostic TargetClassIsAbstract(Location location, string identifier) => Diagnostic.Create(new DiagnosticDescriptor("BGN002", "Target class cannot be abstract", $"Cannot generate a builder for '{identifier}' because it is abstract", "BuilderGenerator", DiagnosticSeverity.Error, true), location); + internal static Diagnostic TargetClassIsAbstract(Location location, string identifier) + { + return Diagnostic.Create(new DiagnosticDescriptor("BGN002", "Target class cannot be abstract", $"Cannot generate a builder for '{identifier}' because it is abstract", "BuilderGenerator", DiagnosticSeverity.Error, true), location); + } - internal static Diagnostic UnexpectedErrorDiagnostic(Exception exception, Location location, string identifier) => Diagnostic.Create(new DiagnosticDescriptor("BGN001", "Unexpected error", $"An error occurred while generating a builder for '{identifier}'\n{exception.Message}", "BuilderGenerator", DiagnosticSeverity.Error, true), location); + internal static Diagnostic UnexpectedErrorDiagnostic(Exception exception, Location location, string identifier) + { + return Diagnostic.Create(new DiagnosticDescriptor("BGN001", "Unexpected error", $"An error occurred while generating a builder for '{identifier}'\n{exception.Message}", "BuilderGenerator", DiagnosticSeverity.Error, true), location); + } } diff --git a/src/BuilderGenerator/TemplateParser.cs b/src/BuilderGenerator/TemplateParser.cs index 754c41f..84b9bb6 100644 --- a/src/BuilderGenerator/TemplateParser.cs +++ b/src/BuilderGenerator/TemplateParser.cs @@ -66,10 +66,16 @@ public string ParseString(string template) /// Defines a tag, and the value that should replace it. /// The tag name. /// The tag value. - public void SetTag(string tag, object value) => TagValues[tag] = new Lazy(() => value); + public void SetTag(string tag, object value) + { + TagValues[tag] = new Lazy(() => value); + } /// Defines a tag, and the value that should replace it. /// The tag name. /// A that returns the tag value. - public void SetTag(string tag, Func func) => TagValues[tag] = new Lazy(func); + public void SetTag(string tag, Func func) + { + TagValues[tag] = new Lazy(func); + } }