201 lines
4.8 KiB
C#
201 lines
4.8 KiB
C#
|
using System.Linq;
|
||
|
using Shouldly;
|
||
|
using Xunit;
|
||
|
using Xunit.Abstractions;
|
||
|
|
||
|
namespace MapToTests
|
||
|
{
|
||
|
public class Tests
|
||
|
{
|
||
|
private readonly ITestOutputHelper _output;
|
||
|
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; }
|
||
|
}
|
||
|
}
|
||
|
";
|
||
|
|
||
|
public Tests(ITestOutputHelper output)
|
||
|
{
|
||
|
_output = output;
|
||
|
}
|
||
|
|
||
|
[Fact]
|
||
|
public void VerifyMapToAttribute()
|
||
|
{
|
||
|
// Arrange
|
||
|
const string source = "";
|
||
|
|
||
|
// Act
|
||
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
||
|
|
||
|
// Assert
|
||
|
diagnostics.ShouldBeSuccessful();
|
||
|
compilation.SyntaxTrees.ShouldContain(c => c.ToString() == ExpectedAttribute);
|
||
|
}
|
||
|
|
||
|
[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]
|
||
|
public void When_MapToAttributeFound_Should_GenerateTheClass()
|
||
|
{
|
||
|
// Arrange
|
||
|
const string source = @"
|
||
|
namespace Test
|
||
|
{
|
||
|
[MapFrom(typeof(Baz)]
|
||
|
public partial class Foo
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
public class Baz
|
||
|
{
|
||
|
public int Prop1 { get; set; }
|
||
|
public int Prop2 { get; }
|
||
|
public int Prop3 { get; set; }
|
||
|
}
|
||
|
}
|
||
|
";
|
||
|
|
||
|
const string expectedResult = @"
|
||
|
// <auto-generated />
|
||
|
|
||
|
namespace Test
|
||
|
{
|
||
|
public partial class Foo
|
||
|
{
|
||
|
public Foo(Baz baz)
|
||
|
{
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
";
|
||
|
|
||
|
// Act
|
||
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
||
|
|
||
|
// Assert
|
||
|
diagnostics.ShouldBeSuccessful();
|
||
|
compilation.SyntaxTrees.Count().ShouldBe(3);
|
||
|
compilation.SyntaxTrees.Last().ToString().ShouldBe(expectedResult.Trim());
|
||
|
}
|
||
|
|
||
|
[Fact]
|
||
|
public void When_MapToAttributeWithNamespaceFound_Should_GenerateTheClass()
|
||
|
{
|
||
|
// Arrange
|
||
|
const string source = @"
|
||
|
namespace Test
|
||
|
{
|
||
|
[MapTo.MapFrom(typeof(Baz)]
|
||
|
public partial class Foo
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
public class Baz
|
||
|
{
|
||
|
|
||
|
}
|
||
|
}
|
||
|
";
|
||
|
|
||
|
const string expectedResult = @"
|
||
|
// <auto-generated />
|
||
|
|
||
|
namespace Test
|
||
|
{
|
||
|
public partial class Foo
|
||
|
{
|
||
|
public Foo(Baz baz)
|
||
|
{
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
";
|
||
|
|
||
|
// Act
|
||
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
||
|
|
||
|
// Assert
|
||
|
diagnostics.ShouldBeSuccessful();
|
||
|
compilation.SyntaxTrees.Count().ShouldBe(3);
|
||
|
compilation.SyntaxTrees.Last().ToString().ShouldBe(expectedResult.Trim());
|
||
|
}
|
||
|
|
||
|
[Fact]
|
||
|
public void When_SourceTypeHasMatchingProperties_Should_CreateConstructorAndAssignSrcToDest()
|
||
|
{
|
||
|
// Arrange
|
||
|
var source = GetSourceText();
|
||
|
|
||
|
const string expectedResult = @"
|
||
|
public partial class Foo
|
||
|
{
|
||
|
public Foo(Baz baz)
|
||
|
{
|
||
|
Prop1 = baz.Prop1;
|
||
|
Prop2 = baz.Prop2;
|
||
|
Prop3 = baz.Prop3;
|
||
|
}
|
||
|
";
|
||
|
|
||
|
// Act
|
||
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
||
|
|
||
|
// Assert
|
||
|
diagnostics.ShouldBeSuccessful();
|
||
|
compilation.SyntaxTrees.Count().ShouldBe(3);
|
||
|
compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedResult.Trim());
|
||
|
}
|
||
|
|
||
|
private static string GetSourceText(bool includeAttributeNamespace = false)
|
||
|
{
|
||
|
return @$"
|
||
|
namespace Test
|
||
|
{{
|
||
|
{(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; }}
|
||
|
}}
|
||
|
|
||
|
public class Baz
|
||
|
{{
|
||
|
public int Prop1 {{ get; set; }}
|
||
|
public int Prop2 {{ get; }}
|
||
|
public int Prop3 {{ get; set; }}
|
||
|
}}
|
||
|
}}
|
||
|
";
|
||
|
}
|
||
|
}
|
||
|
}
|