2020-12-19 10:55:47 +03:00
|
|
|
using System.Linq;
|
2020-12-19 12:05:49 +03:00
|
|
|
using System.Text;
|
2020-12-22 20:35:20 +03:00
|
|
|
using MapToTests;
|
|
|
|
using Microsoft.CodeAnalysis;
|
2020-12-19 10:55:47 +03:00
|
|
|
using Shouldly;
|
|
|
|
using Xunit;
|
|
|
|
using Xunit.Abstractions;
|
|
|
|
|
2020-12-22 20:35:20 +03:00
|
|
|
namespace MapTo.Tests
|
2020-12-19 10:55:47 +03:00
|
|
|
{
|
|
|
|
public class Tests
|
|
|
|
{
|
2020-12-21 19:34:53 +03:00
|
|
|
public Tests(ITestOutputHelper output)
|
|
|
|
{
|
|
|
|
_output = output;
|
|
|
|
}
|
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
private readonly ITestOutputHelper _output;
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
private const string ExpectedAttribute = @"
|
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace MapTo
|
|
|
|
{
|
|
|
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
|
|
|
public sealed class MapFromAttribute : Attribute
|
|
|
|
{
|
|
|
|
public MapFromAttribute(Type sourceType)
|
|
|
|
{
|
|
|
|
SourceType = sourceType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Type SourceType { get; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
";
|
2020-12-21 19:34:53 +03:00
|
|
|
|
|
|
|
private static string GetSourceText(bool includeAttributeNamespace = false, string sourceClassNamespace = "Test.Models")
|
2020-12-19 10:55:47 +03:00
|
|
|
{
|
2020-12-21 19:34:53 +03:00
|
|
|
var builder = new StringBuilder();
|
|
|
|
builder.AppendLine($@"
|
|
|
|
{(includeAttributeNamespace ? string.Empty : "using MapTo;")}
|
|
|
|
namespace Test
|
|
|
|
{{
|
|
|
|
{(sourceClassNamespace != "Test" && !includeAttributeNamespace ? $"using {sourceClassNamespace};" : string.Empty)}
|
2020-12-19 10:55:47 +03:00
|
|
|
|
2020-12-21 19:34:53 +03:00
|
|
|
{(includeAttributeNamespace ? "[MapTo.MapFrom(typeof(Baz))]" : "[MapFrom(typeof(Baz))]")}
|
|
|
|
public partial class Foo
|
|
|
|
{{
|
|
|
|
public int Prop1 {{ get; set; }}
|
|
|
|
public int Prop2 {{ get; }}
|
|
|
|
public int Prop3 {{ get; }}
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
");
|
|
|
|
|
|
|
|
builder.AppendLine($@"
|
|
|
|
namespace {sourceClassNamespace}
|
|
|
|
{{
|
|
|
|
public class Baz
|
|
|
|
{{
|
|
|
|
public int Prop1 {{ get; set; }}
|
|
|
|
public int Prop2 {{ get; }}
|
|
|
|
public int Prop3 {{ get; set; }}
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
");
|
|
|
|
|
|
|
|
return builder.ToString();
|
2020-12-19 10:55:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2020-12-21 19:34:53 +03:00
|
|
|
public void VerifyMapToAttribute()
|
2020-12-19 10:55:47 +03:00
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
const string source = "";
|
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
2020-12-21 19:34:53 +03:00
|
|
|
compilation.SyntaxTrees.ShouldContain(c => c.ToString() == ExpectedAttribute);
|
2020-12-19 10:55:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void When_MapToAttributeFound_Should_GenerateTheClass()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
const string source = @"
|
2020-12-21 13:20:29 +03:00
|
|
|
using MapTo;
|
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
namespace Test
|
|
|
|
{
|
2020-12-21 13:20:29 +03:00
|
|
|
[MapFrom(typeof(Baz))]
|
2020-12-19 10:55:47 +03:00
|
|
|
public partial class Foo
|
|
|
|
{
|
2020-12-22 20:35:20 +03:00
|
|
|
public int Prop1 { get; set; }
|
2020-12-19 10:55:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public class Baz
|
|
|
|
{
|
|
|
|
public int Prop1 { get; set; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
";
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
const string expectedResult = @"
|
|
|
|
// <auto-generated />
|
2020-12-19 11:08:19 +03:00
|
|
|
using System;
|
2020-12-19 10:55:47 +03:00
|
|
|
|
|
|
|
namespace Test
|
|
|
|
{
|
|
|
|
public partial class Foo
|
|
|
|
{
|
2020-12-21 18:53:44 +03:00
|
|
|
public Foo(Test.Baz baz)
|
2020-12-19 10:55:47 +03:00
|
|
|
{
|
2020-12-19 11:08:19 +03:00
|
|
|
if (baz == null) throw new ArgumentNullException(nameof(baz));
|
2020-12-22 20:35:20 +03:00
|
|
|
|
|
|
|
Prop1 = baz.Prop1;
|
2020-12-19 10:55:47 +03:00
|
|
|
}
|
|
|
|
";
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.Count().ShouldBe(3);
|
2020-12-19 11:08:19 +03:00
|
|
|
compilation.SyntaxTrees.Last().ToString().ShouldStartWith(expectedResult.Trim());
|
2020-12-19 10:55:47 +03:00
|
|
|
}
|
2020-12-22 20:35:20 +03:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void When_MapToAttributeFoundWithoutMatchingProperties_Should_ReportError()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
var expectedDiagnostic = Diagnostics.NoMatchingPropertyFoundError(Location.None, "Foo", "Baz");
|
|
|
|
const string source = @"
|
|
|
|
using MapTo;
|
|
|
|
|
|
|
|
namespace Test
|
|
|
|
{
|
|
|
|
[MapFrom(typeof(Baz))]
|
|
|
|
public partial class Foo { }
|
|
|
|
|
|
|
|
public class Baz { public int Prop1 { get; set; } }
|
|
|
|
}
|
|
|
|
";
|
|
|
|
|
|
|
|
// Act
|
|
|
|
var (_, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
var error = diagnostics.FirstOrDefault(d => d.Id == expectedDiagnostic.Id);
|
|
|
|
error.ShouldNotBeNull();
|
|
|
|
}
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
[Fact]
|
|
|
|
public void When_MapToAttributeWithNamespaceFound_Should_GenerateTheClass()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
const string source = @"
|
|
|
|
namespace Test
|
|
|
|
{
|
2020-12-21 13:20:29 +03:00
|
|
|
[MapTo.MapFrom(typeof(Baz))]
|
2020-12-22 20:35:20 +03:00
|
|
|
public partial class Foo { public int Prop1 { get; set; } }
|
2020-12-19 10:55:47 +03:00
|
|
|
|
2020-12-22 20:35:20 +03:00
|
|
|
public class Baz { public int Prop1 { get; set; } }
|
2020-12-19 10:55:47 +03:00
|
|
|
}
|
|
|
|
";
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
const string expectedResult = @"
|
|
|
|
// <auto-generated />
|
2020-12-19 11:08:19 +03:00
|
|
|
using System;
|
2020-12-19 10:55:47 +03:00
|
|
|
|
|
|
|
namespace Test
|
|
|
|
{
|
|
|
|
public partial class Foo
|
|
|
|
{
|
2020-12-21 18:53:44 +03:00
|
|
|
public Foo(Test.Baz baz)
|
2020-12-19 10:55:47 +03:00
|
|
|
{
|
2020-12-19 11:08:19 +03:00
|
|
|
if (baz == null) throw new ArgumentNullException(nameof(baz));
|
2020-12-22 20:35:20 +03:00
|
|
|
|
|
|
|
Prop1 = baz.Prop1;
|
2020-12-19 10:55:47 +03:00
|
|
|
}
|
|
|
|
";
|
2020-12-21 19:34:53 +03:00
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.Count().ShouldBe(3);
|
|
|
|
compilation.SyntaxTrees.Last().ToString().ShouldStartWith(expectedResult.Trim());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void When_NoMapToAttributeFound_Should_GenerateOnlyTheAttribute()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
const string source = "";
|
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.ShouldContain(s => s.ToString() == ExpectedAttribute);
|
|
|
|
compilation.SyntaxTrees.Select(s => s.ToString()).Where(s => s != string.Empty && s != ExpectedAttribute).ShouldBeEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2020-12-22 20:35:20 +03:00
|
|
|
public void When_SourceTypeHasDifferentNamespace_Should_NotAddToUsings()
|
2020-12-21 19:34:53 +03:00
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
var source = GetSourceText(sourceClassNamespace: "Bazaar");
|
|
|
|
|
|
|
|
const string expectedResult = @"
|
|
|
|
// <auto-generated />
|
|
|
|
using System;
|
2020-12-22 20:35:20 +03:00
|
|
|
|
|
|
|
namespace Test
|
2020-12-21 19:34:53 +03:00
|
|
|
";
|
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.Count().ShouldBe(3);
|
2020-12-19 11:08:19 +03:00
|
|
|
compilation.SyntaxTrees.Last().ToString().ShouldStartWith(expectedResult.Trim());
|
2020-12-19 10:55:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void When_SourceTypeHasMatchingProperties_Should_CreateConstructorAndAssignSrcToDest()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
var source = GetSourceText();
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
const string expectedResult = @"
|
|
|
|
public partial class Foo
|
|
|
|
{
|
2020-12-21 18:53:44 +03:00
|
|
|
public Foo(Test.Models.Baz baz)
|
2020-12-19 10:55:47 +03:00
|
|
|
{
|
2020-12-19 11:08:19 +03:00
|
|
|
if (baz == null) throw new ArgumentNullException(nameof(baz));
|
2020-12-22 20:35:20 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
Prop1 = baz.Prop1;
|
|
|
|
Prop2 = baz.Prop2;
|
|
|
|
Prop3 = baz.Prop3;
|
|
|
|
}
|
|
|
|
";
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.Count().ShouldBe(3);
|
|
|
|
compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedResult.Trim());
|
|
|
|
}
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 11:08:19 +03:00
|
|
|
[Fact]
|
|
|
|
public void When_SourceTypeHasMatchingProperties_Should_CreateFromStaticMethod()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
var source = GetSourceText();
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 11:08:19 +03:00
|
|
|
const string expectedResult = @"
|
2020-12-21 18:53:44 +03:00
|
|
|
public static Foo From(Test.Models.Baz baz)
|
2020-12-19 11:08:19 +03:00
|
|
|
{
|
|
|
|
return baz == null ? null : new Foo(baz);
|
|
|
|
}
|
|
|
|
";
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 11:08:19 +03:00
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 11:08:19 +03:00
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.Count().ShouldBe(3);
|
|
|
|
compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedResult.Trim());
|
|
|
|
}
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 12:53:31 +03:00
|
|
|
[Fact]
|
|
|
|
public void When_SourceTypeHasMatchingProperties_Should_GenerateToExtensionMethodOnSourceType()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
var source = GetSourceText();
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 12:53:31 +03:00
|
|
|
const string expectedResult = @"
|
2020-12-22 20:35:20 +03:00
|
|
|
public static partial class BazToFooExtensions
|
2020-12-19 12:53:31 +03:00
|
|
|
{
|
2020-12-21 18:53:44 +03:00
|
|
|
public static Foo ToFoo(this Test.Models.Baz baz)
|
2020-12-19 12:53:31 +03:00
|
|
|
{
|
2020-12-21 18:53:44 +03:00
|
|
|
return baz == null ? null : new Foo(baz);
|
2020-12-19 12:53:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
";
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 12:53:31 +03:00
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 12:53:31 +03:00
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.Count().ShouldBe(3);
|
|
|
|
compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedResult.Trim());
|
|
|
|
}
|
2020-12-19 10:55:47 +03:00
|
|
|
}
|
2020-12-21 19:34:53 +03:00
|
|
|
}
|