From 450de3dedce0be16e01eac133db867c64f77f4a1 Mon Sep 17 00:00:00 2001 From: Mohammadreza Taikandi Date: Sat, 19 Dec 2020 09:05:49 +0000 Subject: [PATCH] Add source type namespace to usings if needed. --- MapTo/MapToGenerator.cs | 1 + MapTo/Models/MapModel.cs | 5 ++++- MapTo/SourceProvider.cs | 21 +++++++++++++-------- MapToTests/Tests.cs | 40 ++++++++++++++++++++++++++++++++++++---- 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/MapTo/MapToGenerator.cs b/MapTo/MapToGenerator.cs index 36b8d14..e83bc82 100644 --- a/MapTo/MapToGenerator.cs +++ b/MapTo/MapToGenerator.cs @@ -66,6 +66,7 @@ namespace MapTo classModifiers: classSyntax.GetClassModifier(), className: classSyntax.GetClassName(), properties: classSymbol.GetAllMembersOfType(), + destinationNamespace: destinationTypeSymbol.ContainingNamespace.Name, destinationClassName: destinationTypeSymbol.Name, destinationTypeProperties: destinationTypeSymbol.GetAllMembersOfType()); } diff --git a/MapTo/Models/MapModel.cs b/MapTo/Models/MapModel.cs index da5bc26..2ccf530 100644 --- a/MapTo/Models/MapModel.cs +++ b/MapTo/Models/MapModel.cs @@ -6,12 +6,13 @@ namespace MapTo.Models { public class MapModel { - public MapModel(string? ns, string classModifiers, string className, IEnumerable properties, string destinationClassName, IEnumerable destinationTypeProperties) + public MapModel(string? ns, string classModifiers, string className, IEnumerable properties, string destinationNamespace, string destinationClassName, IEnumerable destinationTypeProperties) { Namespace = ns; ClassModifiers = classModifiers; ClassName = className; Properties = properties; + DestinationNamespace = destinationNamespace; DestinationClassName = destinationClassName; DestinationTypeProperties = destinationTypeProperties; } @@ -24,6 +25,8 @@ namespace MapTo.Models public IEnumerable Properties { get; } + public string DestinationNamespace { get; } + public string DestinationClassName { get; } public IEnumerable DestinationTypeProperties { get; } diff --git a/MapTo/SourceProvider.cs b/MapTo/SourceProvider.cs index 5670a1d..39a1c51 100644 --- a/MapTo/SourceProvider.cs +++ b/MapTo/SourceProvider.cs @@ -47,7 +47,7 @@ namespace MapTo builder .AppendFileHeader() - .GenerateUsings(); + .GenerateUsings(model); // Namespace declaration builder @@ -75,17 +75,22 @@ namespace MapTo return (builder.ToString(), $"{model.ClassName}.cs"); } - private static StringBuilder GenerateUsings(this StringBuilder builder) + private static StringBuilder GenerateUsings(this StringBuilder builder, MapModel model) { - return builder - .AppendLine("using System;") - .AppendLine(); + builder.AppendLine("using System;"); + + if (!string.IsNullOrWhiteSpace(model.DestinationNamespace) && model.Namespace != model.DestinationNamespace) + { + builder.AppendFormat("using {0};", model.DestinationNamespace).AppendLine(); + } + + return builder.AppendLine(); } - + private static StringBuilder GenerateConstructor(this StringBuilder builder, MapModel model, out List mappedProperties) { var destinationClassParameterName = model.DestinationClassName.ToCamelCase(); - + builder .PadLeft(Indent2) .AppendFormat("public {0}({1} {2})", model.ClassName, model.DestinationClassName, destinationClassParameterName) @@ -120,7 +125,7 @@ namespace MapTo .AppendFormat("public static {0} From({1} {2})", model.ClassName, model.DestinationClassName, destinationClassParameterName) .AppendOpeningBracket(Indent2) .PadLeft(Indent3) - .AppendFormat("return {0} == null ? null : new {1}({0});", destinationClassParameterName, model.ClassName ) + .AppendFormat("return {0} == null ? null : new {1}({0});", destinationClassParameterName, model.ClassName) .AppendClosingBracket(Indent2); } diff --git a/MapToTests/Tests.cs b/MapToTests/Tests.cs index f223308..a942461 100644 --- a/MapToTests/Tests.cs +++ b/MapToTests/Tests.cs @@ -1,4 +1,5 @@ using System.Linq; +using System.Text; using Shouldly; using Xunit; using Xunit.Abstractions; @@ -196,12 +197,36 @@ namespace Test compilation.SyntaxTrees.Count().ShouldBe(3); compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedResult.Trim()); } - - private static string GetSourceText(bool includeAttributeNamespace = false) + + [Fact] + public void When_SourceTypeHasDifferentNamespace_Should_AddToUsings() { - return @$" + // Arrange + var source = GetSourceText(sourceClassNamespace: "Bazaar"); + + const string expectedResult = @" +// +using System; +using Bazaar; +"; + + // Act + var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source); + + // Assert + diagnostics.ShouldBeSuccessful(); + compilation.SyntaxTrees.Count().ShouldBe(3); + compilation.SyntaxTrees.Last().ToString().ShouldStartWith(expectedResult.Trim()); + } + + private static string GetSourceText(bool includeAttributeNamespace = false, string sourceClassNamespace = "Test") + { + var builder = new StringBuilder(); + builder.AppendLine($@" namespace Test {{ + {(sourceClassNamespace != "Test" && !includeAttributeNamespace ? $"using {sourceClassNamespace};": string.Empty)} + {(includeAttributeNamespace ? "[MapTo.MapFrom(typeof(Baz))]" : "[MapFrom(typeof(Baz))]")} public partial class Foo {{ @@ -209,7 +234,12 @@ namespace Test public int Prop2 {{ get; }} public int Prop3 {{ get; }} }} +}} +"); + builder.AppendLine($@" +namespace {sourceClassNamespace} +{{ public class Baz {{ public int Prop1 {{ get; set; }} @@ -217,7 +247,9 @@ namespace Test public int Prop3 {{ get; set; }} }} }} -"; +"); + + return builder.ToString(); } } }