Fix namespace conflict if part of it shared by the system namespace.

This commit is contained in:
Mohammadreza Taikandi 2021-07-03 07:22:54 +01:00
parent 9ab0e4eb25
commit 180bf22b0b
4 changed files with 55 additions and 22 deletions

View File

@ -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}")

View File

@ -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}")

View File

@ -122,11 +122,11 @@ namespace Test
const string expectedResult = @"
// <auto-generated />
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 = @"
// <auto-generated />
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 = @"
// <auto-generated />
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 = @"
// <auto-generated />
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)

View File

@ -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;