From c7d6d3ff275dec10564335975e908babe1e5010a Mon Sep 17 00:00:00 2001 From: Mohammadreza Taikandi Date: Sun, 3 Jan 2021 15:31:18 +0000 Subject: [PATCH] Add option to set generated methods access modifier. --- src/MapTo/Models/SourceGenerationOptions.cs | 4 +-- src/MapTo/SourceBuilder.cs | 15 +++++---- test/MapTo.Tests/Tests.cs | 35 ++++++++++++++++++++- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/MapTo/Models/SourceGenerationOptions.cs b/src/MapTo/Models/SourceGenerationOptions.cs index 3ba0cb7..d65c6a2 100644 --- a/src/MapTo/Models/SourceGenerationOptions.cs +++ b/src/MapTo/Models/SourceGenerationOptions.cs @@ -5,12 +5,12 @@ namespace MapTo.Models { internal record SourceGenerationOptions( AccessModifier ConstructorAccessModifier, - AccessModifier MappingsAccessModifier, + AccessModifier GeneratedMethodsAccessModifier, bool GenerateXmlDocument) { internal static SourceGenerationOptions From(GeneratorExecutionContext context) => new( context.GetBuildGlobalOption(nameof(ConstructorAccessModifier)), - context.GetBuildGlobalOption(nameof(MappingsAccessModifier)), + context.GetBuildGlobalOption(nameof(GeneratedMethodsAccessModifier)), context.GetBuildGlobalOption(nameof(GenerateXmlDocument), defaultValue: true) ); } diff --git a/src/MapTo/SourceBuilder.cs b/src/MapTo/SourceBuilder.cs index 68e95ac..3e6198a 100644 --- a/src/MapTo/SourceBuilder.cs +++ b/src/MapTo/SourceBuilder.cs @@ -1,5 +1,4 @@ -using System; -using System.Linq; +using System.Linq; using System.Text; using MapTo.Extensions; using MapTo.Models; @@ -26,7 +25,7 @@ namespace MapTo .AppendLine() .AppendFormat("namespace {0}", NamespaceName) .AppendOpeningBracket(); - + if (options.GenerateXmlDocument) { builder @@ -52,7 +51,7 @@ namespace MapTo .PadLeft(Indent2).AppendFormat("public {0}Attribute(Type sourceType)", MapFromAttributeName) .AppendOpeningBracket(Indent2) .PadLeft(Indent3).AppendLine("SourceType = sourceType;") - .AppendClosingBracket(Indent2, padNewLine: false) + .AppendClosingBracket(Indent2, false) .AppendLine() .AppendLine(); @@ -66,7 +65,7 @@ namespace MapTo builder .PadLeft(Indent2).AppendLine("public Type SourceType { get; }") - .AppendClosingBracket(Indent1, padNewLine: false) + .AppendClosingBracket(Indent1, false) .AppendClosingBracket(); return (builder.ToString(), $"{MapFromAttributeName}Attribute.g.cs"); @@ -187,7 +186,7 @@ namespace MapTo return builder .AppendLine() .AppendConvertorMethodsXmlDocs(model, sourceClassParameterName) - .PadLeft(Indent2).AppendFormat("public static {0} From({1} {2})", model.ClassName, model.SourceClassFullName, sourceClassParameterName) + .PadLeft(Indent2).AppendFormat("{0} static {1} From({2} {3})", model.Options.GeneratedMethodsAccessModifier.ToLowercaseString(), model.ClassName, model.SourceClassFullName, sourceClassParameterName) .AppendOpeningBracket(Indent2) .PadLeft(Indent3).AppendFormat("return {0} == null ? null : new {1}({0});", sourceClassParameterName, model.ClassName) .AppendClosingBracket(Indent2); @@ -199,7 +198,7 @@ namespace MapTo return builder .AppendConvertorMethodsXmlDocs(model, sourceClassParameterName) - .PadLeft(Indent2).AppendFormat("public static {0} To{0}(this {1} {2})", model.ClassName, model.SourceClassFullName, sourceClassParameterName) + .PadLeft(Indent2).AppendFormat("{0} static {1} To{1}(this {2} {3})", model.Options.GeneratedMethodsAccessModifier.ToLowercaseString(), model.ClassName, model.SourceClassFullName, sourceClassParameterName) .AppendOpeningBracket(Indent2) .PadLeft(Indent3).AppendFormat("return {0} == null ? null : new {1}({0});", sourceClassParameterName, model.ClassName) .AppendClosingBracket(Indent2); @@ -214,7 +213,7 @@ namespace MapTo { return builder; } - + return builder .PadLeft(Indent2).AppendLine("/// ") .PadLeft(Indent2).AppendFormat("/// Creates a new instance of and sets its participating properties", model.ClassName).AppendLine() diff --git a/test/MapTo.Tests/Tests.cs b/test/MapTo.Tests/Tests.cs index a1ad52e..8f9fd10 100644 --- a/test/MapTo.Tests/Tests.cs +++ b/test/MapTo.Tests/Tests.cs @@ -3,11 +3,11 @@ using System.Collections.Generic; using System.Linq; using System.Text; using MapTo.Extensions; +using MapTo.Models; using MapTo.Tests.Infrastructure; using Microsoft.CodeAnalysis; using Shouldly; using Xunit; -using Xunit.Abstractions; namespace MapTo.Tests { @@ -446,5 +446,38 @@ namespace MapTo diagnostics.ShouldBeSuccessful(); compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedResult); } + + [Fact] + public void When_MappingsModifierOptionIsSetToInternal_Should_GenerateThoseMethodsWithInternalAccessModifier() + { + // Arrange + var source = GetSourceText(); + var configOptions = new Dictionary + { + [$"build_property.MapTo_{nameof(SourceGenerationOptions.GeneratedMethodsAccessModifier)}"] = "Internal" + }; + + var expectedExtension = @" + internal static Foo ToFoo(this Test.Models.Baz baz) + { + return baz == null ? null : new Foo(baz); + }".Trim(); + + var expectedFactory = @" + internal static Foo From(Test.Models.Baz baz) + { + return baz == null ? null : new Foo(baz); + }".Trim(); + + // Act + var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: configOptions); + + // Assert + diagnostics.ShouldBeSuccessful(); + + var syntaxTree = compilation.SyntaxTrees.Last().ToString(); + syntaxTree.ShouldContain(expectedFactory); + syntaxTree.ShouldContain(expectedExtension); + } } } \ No newline at end of file