From 180bf22b0bc474bd2bff61c83f976e1820ca2dcd Mon Sep 17 00:00:00 2001 From: Mohammadreza Taikandi Date: Sat, 3 Jul 2021 07:22:54 +0100 Subject: [PATCH] Fix namespace conflict if part of it shared by the system namespace. --- src/MapTo/Sources/MapClassSource.cs | 4 +-- src/MapTo/Sources/MapRecordSource.cs | 4 +-- test/MapTo.Tests/MapToTests.cs | 37 +++++++++++++------------- test/MapTo.Tests/MappedClassesTests.cs | 32 ++++++++++++++++++++++ 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/src/MapTo/Sources/MapClassSource.cs b/src/MapTo/Sources/MapClassSource.cs index 803006a..edde823 100644 --- a/src/MapTo/Sources/MapClassSource.cs +++ b/src/MapTo/Sources/MapClassSource.cs @@ -10,12 +10,12 @@ namespace MapTo.Sources using var builder = new SourceBuilder() .WriteLine(GeneratedFilesHeader) .WriteNullableContextOptionIf(model.Options.SupportNullableReferenceTypes) + .WriteUsings(model.Usings) + .WriteLine() // Namespace declaration .WriteLine($"namespace {model.Namespace}") .WriteOpeningBracket() - .WriteUsings(model.Usings) - .WriteLine() // Class declaration .WriteLine($"partial class {model.TypeIdentifierName}") diff --git a/src/MapTo/Sources/MapRecordSource.cs b/src/MapTo/Sources/MapRecordSource.cs index 9614ab1..c2988c0 100644 --- a/src/MapTo/Sources/MapRecordSource.cs +++ b/src/MapTo/Sources/MapRecordSource.cs @@ -11,12 +11,12 @@ namespace MapTo.Sources using var builder = new SourceBuilder() .WriteLine(GeneratedFilesHeader) .WriteNullableContextOptionIf(model.Options.SupportNullableReferenceTypes) + .WriteUsings(model.Usings) + .WriteLine() // Namespace declaration .WriteLine($"namespace {model.Namespace}") .WriteOpeningBracket() - .WriteUsings(model.Usings) - .WriteLine() // Class declaration .WriteLine($"partial record {model.TypeIdentifierName}") diff --git a/test/MapTo.Tests/MapToTests.cs b/test/MapTo.Tests/MapToTests.cs index a919de3..e09009c 100644 --- a/test/MapTo.Tests/MapToTests.cs +++ b/test/MapTo.Tests/MapToTests.cs @@ -122,11 +122,11 @@ namespace Test const string expectedResult = @" // +using MapTo; +using System; + namespace Test { - using MapTo; - using System; - partial class Foo { public Foo(Test.Baz baz) @@ -198,11 +198,11 @@ namespace Test const string expectedResult = @" // +using MapTo; +using System; + namespace Test { - using MapTo; - using System; - partial class Foo { public Foo(Test.Baz baz) @@ -260,10 +260,11 @@ namespace Test const string expectedResult = @" // +using MapTo; +using System; + namespace Test { - using MapTo; - using System; "; // Act @@ -526,13 +527,13 @@ private protected ManagerViewModel(MappingContext context, Test.Data.Models.Mana const string expectedResult = @" // +using MapTo; +using System; +using System.Collections.Generic; +using System.Linq; + namespace Test.ViewModels { - using MapTo; - using System; - using System.Collections.Generic; - using System.Linq; - partial class ManagerViewModel { public ManagerViewModel(Test.Data.Models.Manager manager) @@ -565,13 +566,13 @@ namespace Test.ViewModels var sources = GetEmployeeManagerSourceText(useDifferentViewModelNamespace: true); const string expectedResult = @" +using MapTo; +using System; +using System.Collections.Generic; +using System.Linq; + namespace Test.ViewModels2 { - using MapTo; - using System; - using System.Collections.Generic; - using System.Linq; - partial class ManagerViewModel { public ManagerViewModel(Test.Data.Models.Manager manager) diff --git a/test/MapTo.Tests/MappedClassesTests.cs b/test/MapTo.Tests/MappedClassesTests.cs index e9d463d..009c440 100644 --- a/test/MapTo.Tests/MappedClassesTests.cs +++ b/test/MapTo.Tests/MappedClassesTests.cs @@ -407,6 +407,38 @@ namespace Tests.Data.ViewModels _output.WriteLine(compilation.PrintSyntaxTree()); } + [Fact] + public void VerifySystemNamespaceConflict() + { + // Arrange + var source = @" +namespace Test +{ + public record SomeRecord(int Id); +} + +namespace Test.Models +{ + using MapTo; + + [MapFrom(typeof(Test.SomeRecord))] + public partial record SomeRecordModel(int Id); +} + +namespace Test.System +{ + public interface IMyInterface { } +} +".Trim(); + + // Act + var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions, languageVersion: LanguageVersion.CSharp9); + + // Assert + diagnostics.ShouldBeSuccessful(); + _output.WriteLine(compilation.PrintSyntaxTree()); + } + private static string MainSourceClass => @" using System;