Fixes issue #6. Move usings inside namespace declaration.

This commit is contained in:
Mohammadreza Taikandi 2021-06-23 09:13:19 +01:00
parent 9e662c7e90
commit e474ab86d0
3 changed files with 57 additions and 27 deletions

View File

@ -10,13 +10,12 @@ namespace MapTo.Sources
using var builder = new SourceBuilder()
.WriteLine(GeneratedFilesHeader)
.WriteNullableContextOptionIf(model.Options.SupportNullableReferenceTypes)
.WriteLine()
.WriteUsings(model.Usings)
.WriteLine()
// Namespace declaration
.WriteLine($"namespace {model.Namespace}")
.WriteOpeningBracket()
.WriteUsings(model.Usings)
.WriteLine()
// Class declaration
.WriteLine($"partial class {model.ClassName}")

View File

@ -122,12 +122,11 @@ namespace Test
const string expectedResult = @"
// <auto-generated />
using MapTo;
using System;
namespace Test
{
using MapTo;
using System;
partial class Foo
{
public Foo(Baz baz)
@ -199,12 +198,11 @@ namespace Test
const string expectedResult = @"
// <auto-generated />
using MapTo;
using System;
namespace Test
{
using MapTo;
using System;
partial class Foo
{
public Foo(Baz baz)
@ -262,12 +260,11 @@ namespace Test
const string expectedResult = @"
// <auto-generated />
using Bazaar;
using MapTo;
using System;
namespace Test
{
using Bazaar;
using MapTo;
using System;
";
// Act
@ -529,13 +526,15 @@ private protected ManagerViewModel(MappingContext context, Manager manager) : ba
var sources = GetEmployeeManagerSourceText();
const string expectedResult = @"
using System;
using System.Collections.Generic;
using System.Linq;
using Test.Data.Models;
// <auto-generated />
namespace Test.ViewModels
{
using MapTo;
using System;
using System.Collections.Generic;
using System.Linq;
using Test.Data.Models;
partial class ManagerViewModel
{
public ManagerViewModel(Manager manager)
@ -568,14 +567,15 @@ namespace Test.ViewModels
var sources = GetEmployeeManagerSourceText(useDifferentViewModelNamespace: true);
const string expectedResult = @"
using System;
using System.Collections.Generic;
using System.Linq;
using Test.Data.Models;
using Test.ViewModels;
namespace Test.ViewModels2
{
using MapTo;
using System;
using System.Collections.Generic;
using System.Linq;
using Test.Data.Models;
using Test.ViewModels;
partial class ManagerViewModel
{
public ManagerViewModel(Manager manager)

View File

@ -96,6 +96,37 @@ namespace Test.Data.Models
diagnostics.ShouldNotBeSuccessful(DiagnosticsFactory.MissingConstructorArgument(constructorSyntax));
}
[Fact]
public void When_PropertyNameIsTheSameAsClassName_Should_MapAccordingly()
{
// Arrange
var source = @"
namespace Sale
{
public class Sale { public Sale Prop1 { get; set; } }
}
namespace SaleModel
{
using MapTo;
using Sale;
[MapFrom(typeof(Sale))]
public partial class SaleModel
{
[MapProperty(SourcePropertyName = nameof(global::Sale.Sale.Prop1))]
public Sale Sale { get; set; }
}
}
".Trim();
// Act
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
// Assert
diagnostics.ShouldBeSuccessful();
}
private static string NestedSourceClass => @"
namespace Test.Data.Models
{