2020-12-24 13:05:29 +03:00
|
|
|
using System;
|
2021-01-03 12:26:07 +03:00
|
|
|
using System.Collections.Generic;
|
2020-12-19 10:55:47 +03:00
|
|
|
using System.Linq;
|
2020-12-19 12:05:49 +03:00
|
|
|
using System.Text;
|
2020-12-24 13:05:29 +03:00
|
|
|
using MapTo.Extensions;
|
2021-01-04 13:42:18 +03:00
|
|
|
using MapTo.Sources;
|
2021-01-06 11:52:50 +03:00
|
|
|
using MapTo.Tests.Extensions;
|
2021-01-03 12:26:07 +03:00
|
|
|
using MapTo.Tests.Infrastructure;
|
2020-12-22 20:35:20 +03:00
|
|
|
using Microsoft.CodeAnalysis;
|
2021-01-21 11:19:17 +03:00
|
|
|
using Microsoft.CodeAnalysis.CSharp;
|
|
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
2020-12-19 10:55:47 +03:00
|
|
|
using Shouldly;
|
|
|
|
using Xunit;
|
2021-01-03 18:44:59 +03:00
|
|
|
using static MapTo.Extensions.GeneratorExecutionContextExtensions;
|
2020-12-19 10:55:47 +03:00
|
|
|
|
2020-12-22 20:35:20 +03:00
|
|
|
namespace MapTo.Tests
|
2020-12-19 10:55:47 +03:00
|
|
|
{
|
|
|
|
public class Tests
|
|
|
|
{
|
2020-12-24 13:05:29 +03:00
|
|
|
private const int Indent1 = 4;
|
|
|
|
private const int Indent2 = Indent1 * 2;
|
|
|
|
private const int Indent3 = Indent1 * 3;
|
2021-01-21 11:19:17 +03:00
|
|
|
private static readonly Location IgnoreLocation = Location.None;
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2021-01-03 12:26:07 +03:00
|
|
|
private static readonly Dictionary<string, string> DefaultAnalyzerOptions = new()
|
|
|
|
{
|
2021-01-03 18:44:59 +03:00
|
|
|
[GetBuildPropertyName(nameof(SourceGenerationOptions.GenerateXmlDocument))] = "false"
|
2021-01-03 12:26:07 +03:00
|
|
|
};
|
2021-01-03 18:44:59 +03:00
|
|
|
|
2021-01-04 13:42:18 +03:00
|
|
|
private static readonly string ExpectedAttribute = $@"{Constants.GeneratedFilesHeader}
|
2020-12-19 10:55:47 +03:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace MapTo
|
2020-12-24 13:05:29 +03:00
|
|
|
{{
|
2020-12-19 10:55:47 +03:00
|
|
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
|
|
|
public sealed class MapFromAttribute : Attribute
|
2020-12-24 13:05:29 +03:00
|
|
|
{{
|
2020-12-19 10:55:47 +03:00
|
|
|
public MapFromAttribute(Type sourceType)
|
2020-12-24 13:05:29 +03:00
|
|
|
{{
|
2020-12-19 10:55:47 +03:00
|
|
|
SourceType = sourceType;
|
2020-12-24 13:05:29 +03:00
|
|
|
}}
|
2020-12-19 10:55:47 +03:00
|
|
|
|
2020-12-24 13:05:29 +03:00
|
|
|
public Type SourceType {{ get; }}
|
|
|
|
}}
|
|
|
|
}}";
|
|
|
|
|
|
|
|
private record SourceGeneratorOptions(
|
2021-01-03 18:44:59 +03:00
|
|
|
bool UseMapToNamespace = false,
|
|
|
|
string SourceClassNamespace = "Test.Models",
|
|
|
|
int ClassPropertiesCount = 3,
|
2020-12-24 13:05:29 +03:00
|
|
|
int SourceClassPropertiesCount = 3,
|
|
|
|
Action<StringBuilder> PropertyBuilder = null,
|
|
|
|
Action<StringBuilder> SourcePropertyBuilder = null);
|
2021-01-03 18:44:59 +03:00
|
|
|
|
2020-12-24 13:05:29 +03:00
|
|
|
private static string GetSourceText(SourceGeneratorOptions options = null)
|
2020-12-19 10:55:47 +03:00
|
|
|
{
|
2020-12-24 13:05:29 +03:00
|
|
|
const string ns = "Test";
|
|
|
|
options ??= new SourceGeneratorOptions();
|
|
|
|
var hasDifferentSourceNamespace = options.SourceClassNamespace != ns;
|
2020-12-21 19:34:53 +03:00
|
|
|
var builder = new StringBuilder();
|
2021-01-03 18:44:59 +03:00
|
|
|
|
2021-01-21 11:19:17 +03:00
|
|
|
builder.AppendLine("//");
|
|
|
|
builder.AppendLine("// Test source code.");
|
|
|
|
builder.AppendLine("//");
|
|
|
|
builder.AppendLine();
|
2021-01-23 14:05:58 +03:00
|
|
|
|
2020-12-24 13:05:29 +03:00
|
|
|
if (options.UseMapToNamespace)
|
|
|
|
{
|
2021-01-04 13:42:18 +03:00
|
|
|
builder.AppendFormat("using {0};", Constants.RootNamespace).AppendLine();
|
2020-12-24 13:05:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
builder
|
|
|
|
.AppendFormat("using {0};", options.SourceClassNamespace)
|
|
|
|
.AppendLine()
|
|
|
|
.AppendLine();
|
|
|
|
|
|
|
|
builder
|
|
|
|
.AppendFormat("namespace {0}", ns)
|
|
|
|
.AppendOpeningBracket();
|
|
|
|
|
|
|
|
if (hasDifferentSourceNamespace && options.UseMapToNamespace)
|
|
|
|
{
|
|
|
|
builder
|
|
|
|
.PadLeft(Indent1)
|
|
|
|
.AppendFormat("using {0};", options.SourceClassNamespace)
|
|
|
|
.AppendLine()
|
|
|
|
.AppendLine();
|
|
|
|
}
|
2021-01-03 18:44:59 +03:00
|
|
|
|
2020-12-24 13:05:29 +03:00
|
|
|
builder
|
|
|
|
.PadLeft(Indent1)
|
2021-01-23 14:05:58 +03:00
|
|
|
.AppendLine(options.UseMapToNamespace ? "[MapFrom(typeof(Baz))]" : "[MapTo.MapFrom(typeof(Baz))]")
|
2020-12-24 13:05:29 +03:00
|
|
|
.PadLeft(Indent1).Append("public partial class Foo")
|
|
|
|
.AppendOpeningBracket(Indent1);
|
|
|
|
|
|
|
|
for (var i = 1; i <= options.ClassPropertiesCount; i++)
|
|
|
|
{
|
|
|
|
builder
|
|
|
|
.PadLeft(Indent2)
|
|
|
|
.AppendLine(i % 2 == 0 ? $"public int Prop{i} {{ get; set; }}" : $"public int Prop{i} {{ get; }}");
|
|
|
|
}
|
2021-01-03 18:44:59 +03:00
|
|
|
|
2020-12-24 13:05:29 +03:00
|
|
|
options.PropertyBuilder?.Invoke(builder);
|
|
|
|
|
|
|
|
builder
|
2021-01-03 18:44:59 +03:00
|
|
|
.AppendClosingBracket(Indent1, false)
|
2020-12-24 13:05:29 +03:00
|
|
|
.AppendClosingBracket()
|
|
|
|
.AppendLine()
|
|
|
|
.AppendLine();
|
2021-01-03 18:44:59 +03:00
|
|
|
|
2020-12-24 13:05:29 +03:00
|
|
|
builder
|
|
|
|
.AppendFormat("namespace {0}", options.SourceClassNamespace)
|
|
|
|
.AppendOpeningBracket()
|
|
|
|
.PadLeft(Indent1).Append("public class Baz")
|
|
|
|
.AppendOpeningBracket(Indent1);
|
|
|
|
|
|
|
|
for (var i = 1; i <= options.SourceClassPropertiesCount; i++)
|
|
|
|
{
|
|
|
|
builder
|
|
|
|
.PadLeft(Indent2)
|
|
|
|
.AppendLine(i % 2 == 0 ? $"public int Prop{i} {{ get; set; }}" : $"public int Prop{i} {{ get; }}");
|
|
|
|
}
|
2021-01-03 18:44:59 +03:00
|
|
|
|
2020-12-24 13:05:29 +03:00
|
|
|
options.SourcePropertyBuilder?.Invoke(builder);
|
2021-01-03 18:44:59 +03:00
|
|
|
|
2020-12-24 13:05:29 +03:00
|
|
|
builder
|
2021-01-03 18:44:59 +03:00
|
|
|
.AppendClosingBracket(Indent1, false)
|
2020-12-24 13:05:29 +03:00
|
|
|
.AppendClosingBracket();
|
2020-12-21 19:34:53 +03:00
|
|
|
|
|
|
|
return builder.ToString();
|
2020-12-19 10:55:47 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 18:44:59 +03:00
|
|
|
[Fact]
|
|
|
|
public void VerifyIgnorePropertyAttribute()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
const string source = "";
|
|
|
|
var expectedAttribute = $@"
|
2021-01-04 13:42:18 +03:00
|
|
|
{Constants.GeneratedFilesHeader}
|
2021-01-03 18:44:59 +03:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace MapTo
|
|
|
|
{{
|
|
|
|
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
|
|
|
|
public sealed class IgnorePropertyAttribute : Attribute {{ }}
|
|
|
|
}}
|
|
|
|
".Trim();
|
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
2021-01-06 11:52:50 +03:00
|
|
|
compilation.SyntaxTrees.ShouldContainSource(IgnorePropertyAttributeSource.AttributeName, expectedAttribute);
|
2021-01-03 18:44:59 +03:00
|
|
|
}
|
|
|
|
|
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
|
2021-01-03 12:26:07 +03:00
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
2021-01-06 11:52:50 +03:00
|
|
|
compilation.SyntaxTrees.ShouldContainSource(MapFromAttributeSource.AttributeName, ExpectedAttribute);
|
2020-12-19 10:55:47 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 18:44:59 +03:00
|
|
|
[Fact]
|
2021-01-21 11:19:17 +03:00
|
|
|
public void When_FoundMatchingPropertyNameWithDifferentTypes_Should_ReportError()
|
2021-01-03 18:44:59 +03:00
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
var source = GetSourceText(new SourceGeneratorOptions(
|
|
|
|
true,
|
|
|
|
PropertyBuilder: builder =>
|
|
|
|
{
|
|
|
|
builder
|
|
|
|
.PadLeft(Indent2).AppendLine("public string Prop4 { get; set; }");
|
|
|
|
},
|
|
|
|
SourcePropertyBuilder: builder => builder.PadLeft(Indent2).AppendLine("public int Prop4 { get; set; }")));
|
2021-01-23 14:05:58 +03:00
|
|
|
|
2021-01-03 18:44:59 +03:00
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
2021-01-23 14:05:58 +03:00
|
|
|
|
2021-01-03 18:44:59 +03:00
|
|
|
// Assert
|
2021-01-21 11:19:17 +03:00
|
|
|
var expectedError = DiagnosticProvider.NoMatchingPropertyTypeFoundError(GetSourcePropertySymbol("Prop4", compilation));
|
|
|
|
|
|
|
|
diagnostics.ShouldBeUnsuccessful(expectedError);
|
2021-01-03 18:44:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void When_IgnorePropertyAttributeIsSpecified_Should_NotGenerateMappingsForThatProperty()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
var source = GetSourceText(new SourceGeneratorOptions(
|
|
|
|
true,
|
|
|
|
PropertyBuilder: builder =>
|
|
|
|
{
|
|
|
|
builder
|
|
|
|
.PadLeft(Indent2).AppendLine("[IgnoreProperty]")
|
|
|
|
.PadLeft(Indent2).AppendLine("public int Prop4 { get; set; }");
|
|
|
|
},
|
|
|
|
SourcePropertyBuilder: builder => builder.PadLeft(Indent2).AppendLine("public int Prop4 { get; set; }")));
|
|
|
|
|
|
|
|
var expectedResult = @"
|
|
|
|
partial class Foo
|
|
|
|
{
|
|
|
|
public Foo(Test.Models.Baz baz)
|
|
|
|
{
|
|
|
|
if (baz == null) throw new ArgumentNullException(nameof(baz));
|
|
|
|
|
|
|
|
Prop1 = baz.Prop1;
|
|
|
|
Prop2 = baz.Prop2;
|
|
|
|
Prop3 = baz.Prop3;
|
|
|
|
}
|
|
|
|
".Trim();
|
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
2021-01-23 14:05:58 +03:00
|
|
|
|
2021-01-03 18:44:59 +03:00
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void When_MappingsModifierOptionIsSetToInternal_Should_GenerateThoseMethodsWithInternalAccessModifier()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
var source = GetSourceText();
|
|
|
|
var configOptions = new Dictionary<string, string>
|
|
|
|
{
|
|
|
|
[GetBuildPropertyName(nameof(SourceGenerationOptions.GeneratedMethodsAccessModifier))] = "Internal",
|
|
|
|
[GetBuildPropertyName(nameof(SourceGenerationOptions.GenerateXmlDocument))] = "false"
|
|
|
|
};
|
|
|
|
|
|
|
|
var expectedExtension = @"
|
|
|
|
internal static partial class BazToFooExtensions
|
|
|
|
{
|
|
|
|
internal static Foo ToFoo(this Test.Models.Baz baz)
|
|
|
|
{
|
|
|
|
return baz == null ? null : new Foo(baz);
|
|
|
|
}
|
|
|
|
}".Trim();
|
|
|
|
|
|
|
|
var expectedFactory = @"
|
|
|
|
internal static Foo From(Test.Models.Baz baz)
|
|
|
|
{
|
|
|
|
return baz == null ? null : new Foo(baz);
|
|
|
|
}".Trim();
|
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: configOptions);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
|
|
|
|
var syntaxTree = compilation.SyntaxTrees.Last().ToString();
|
|
|
|
syntaxTree.ShouldContain(expectedFactory);
|
|
|
|
syntaxTree.ShouldContain(expectedExtension);
|
|
|
|
}
|
|
|
|
|
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 />
|
2021-01-27 10:53:29 +03:00
|
|
|
|
2020-12-19 11:08:19 +03:00
|
|
|
using System;
|
2020-12-19 10:55:47 +03:00
|
|
|
|
|
|
|
namespace Test
|
|
|
|
{
|
2021-01-03 18:44:59 +03:00
|
|
|
partial class Foo
|
2020-12-19 10:55:47 +03:00
|
|
|
{
|
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
|
2021-01-03 12:26:07 +03:00
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
2020-12-19 11:08:19 +03:00
|
|
|
compilation.SyntaxTrees.Last().ToString().ShouldStartWith(expectedResult.Trim());
|
2020-12-19 10:55:47 +03:00
|
|
|
}
|
2021-01-03 18:44:59 +03:00
|
|
|
|
2020-12-22 20:35:20 +03:00
|
|
|
[Fact]
|
|
|
|
public void When_MapToAttributeFoundWithoutMatchingProperties_Should_ReportError()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
const string source = @"
|
|
|
|
using MapTo;
|
|
|
|
|
|
|
|
namespace Test
|
|
|
|
{
|
|
|
|
[MapFrom(typeof(Baz))]
|
|
|
|
public partial class Foo { }
|
|
|
|
|
|
|
|
public class Baz { public int Prop1 { get; set; } }
|
|
|
|
}
|
|
|
|
";
|
|
|
|
|
|
|
|
// Act
|
2021-01-21 11:19:17 +03:00
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
2020-12-22 20:35:20 +03:00
|
|
|
|
|
|
|
// Assert
|
2021-01-21 11:19:17 +03:00
|
|
|
var fooType = compilation.GetTypeByMetadataName("Test.Foo");
|
|
|
|
fooType.ShouldNotBeNull();
|
2021-01-23 14:05:58 +03:00
|
|
|
|
2021-01-21 11:19:17 +03:00
|
|
|
var bazType = compilation.GetTypeByMetadataName("Test.Baz");
|
|
|
|
bazType.ShouldNotBeNull();
|
|
|
|
|
|
|
|
var expectedDiagnostic = DiagnosticProvider.NoMatchingPropertyFoundError(fooType.Locations.Single(), fooType, bazType);
|
2020-12-22 20:35:20 +03:00
|
|
|
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 />
|
2021-01-27 10:53:29 +03:00
|
|
|
|
2020-12-19 11:08:19 +03:00
|
|
|
using System;
|
2020-12-19 10:55:47 +03:00
|
|
|
|
|
|
|
namespace Test
|
|
|
|
{
|
2021-01-03 18:44:59 +03:00
|
|
|
partial class Foo
|
2020-12-19 10:55:47 +03:00
|
|
|
{
|
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
|
2021-01-03 12:26:07 +03:00
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
2020-12-21 19:34:53 +03:00
|
|
|
|
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.Last().ToString().ShouldStartWith(expectedResult.Trim());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void When_NoMapToAttributeFound_Should_GenerateOnlyTheAttribute()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
const string source = "";
|
2021-01-23 14:05:58 +03:00
|
|
|
var expectedTypes = new[] { IgnorePropertyAttributeSource.AttributeName, MapFromAttributeSource.AttributeName, ITypeConverterSource.InterfaceName };
|
2020-12-21 19:34:53 +03:00
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
2020-12-24 13:05:29 +03:00
|
|
|
compilation.SyntaxTrees
|
|
|
|
.Select(s => s.ToString())
|
|
|
|
.Where(s => !string.IsNullOrWhiteSpace(s.ToString()))
|
2021-01-06 11:52:50 +03:00
|
|
|
.All(s => expectedTypes.Any(s.Contains))
|
2020-12-24 13:05:29 +03:00
|
|
|
.ShouldBeTrue();
|
2020-12-21 19:34:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2020-12-22 20:35:20 +03:00
|
|
|
public void When_SourceTypeHasDifferentNamespace_Should_NotAddToUsings()
|
2020-12-21 19:34:53 +03:00
|
|
|
{
|
|
|
|
// Arrange
|
2020-12-24 13:05:29 +03:00
|
|
|
var source = GetSourceText(new SourceGeneratorOptions(SourceClassNamespace: "Bazaar"));
|
2020-12-21 19:34:53 +03:00
|
|
|
|
|
|
|
const string expectedResult = @"
|
|
|
|
// <auto-generated />
|
2021-01-27 10:53:29 +03:00
|
|
|
|
2020-12-21 19:34:53 +03:00
|
|
|
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();
|
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 = @"
|
2021-01-03 18:44:59 +03:00
|
|
|
partial class Foo
|
2020-12-19 10:55:47 +03:00
|
|
|
{
|
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
|
2021-01-03 12:26:07 +03:00
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 10:55:47 +03:00
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
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.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
|
2021-01-03 12:26:07 +03:00
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
2020-12-21 19:34:53 +03:00
|
|
|
|
2020-12-19 12:53:31 +03:00
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedResult.Trim());
|
|
|
|
}
|
2021-01-23 14:05:58 +03:00
|
|
|
|
2021-01-06 11:52:50 +03:00
|
|
|
[Fact]
|
|
|
|
public void VerifyTypeConverterInterface()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
const string source = "";
|
|
|
|
var expectedInterface = $@"
|
|
|
|
{Constants.GeneratedFilesHeader}
|
|
|
|
|
|
|
|
namespace MapTo
|
|
|
|
{{
|
|
|
|
public interface ITypeConverter<in TSource, out TDestination>
|
|
|
|
{{
|
2021-01-23 14:05:58 +03:00
|
|
|
TDestination Convert(TSource source, object[] converterParameters);
|
2021-01-06 11:52:50 +03:00
|
|
|
}}
|
|
|
|
}}
|
|
|
|
".Trim();
|
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
2021-01-23 14:05:58 +03:00
|
|
|
compilation.SyntaxTrees.ShouldContainSource(ITypeConverterSource.InterfaceName, expectedInterface);
|
2021-01-06 11:52:50 +03:00
|
|
|
}
|
2021-01-23 14:05:58 +03:00
|
|
|
|
2021-01-27 10:53:29 +03:00
|
|
|
[Fact]
|
|
|
|
public void VerifyTypeConverterInterfaceWithNullableOptionOn()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
const string source = "";
|
|
|
|
var expectedInterface = $@"
|
|
|
|
{Constants.GeneratedFilesHeader}
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
namespace MapTo
|
|
|
|
{{
|
|
|
|
public interface ITypeConverter<in TSource, out TDestination>
|
|
|
|
{{
|
|
|
|
TDestination Convert(TSource source, object[]? converterParameters);
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
".Trim();
|
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions, nullableContextOptions: NullableContextOptions.Enable);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.ShouldContainSource(ITypeConverterSource.InterfaceName, expectedInterface);
|
|
|
|
}
|
|
|
|
|
2021-01-07 11:17:36 +03:00
|
|
|
[Fact]
|
2021-01-21 19:41:12 +03:00
|
|
|
public void VerifyMapTypeConverterAttribute()
|
2021-01-07 11:17:36 +03:00
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
const string source = "";
|
|
|
|
var expectedInterface = $@"
|
|
|
|
{Constants.GeneratedFilesHeader}
|
2021-01-27 10:53:29 +03:00
|
|
|
|
2021-01-07 11:17:36 +03:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace MapTo
|
|
|
|
{{
|
2021-01-21 19:43:58 +03:00
|
|
|
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
|
2021-01-21 19:41:12 +03:00
|
|
|
public sealed class MapTypeConverterAttribute : Attribute
|
2021-01-07 11:17:36 +03:00
|
|
|
{{
|
2021-01-23 14:05:58 +03:00
|
|
|
public MapTypeConverterAttribute(Type converter, object[] converterParameters = null)
|
2021-01-21 19:41:12 +03:00
|
|
|
{{
|
|
|
|
Converter = converter;
|
2021-01-23 14:05:58 +03:00
|
|
|
ConverterParameters = converterParameters;
|
2021-01-21 19:41:12 +03:00
|
|
|
}}
|
2021-01-07 11:17:36 +03:00
|
|
|
|
2021-01-21 19:41:12 +03:00
|
|
|
public Type Converter {{ get; }}
|
2021-01-23 14:05:58 +03:00
|
|
|
|
|
|
|
public object[] ConverterParameters {{ get; }}
|
2021-01-07 11:17:36 +03:00
|
|
|
}}
|
|
|
|
}}
|
|
|
|
".Trim();
|
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
2021-01-21 19:41:12 +03:00
|
|
|
compilation.SyntaxTrees.ShouldContainSource(MapTypeConverterAttributeSource.AttributeName, expectedInterface);
|
2021-01-07 11:17:36 +03:00
|
|
|
}
|
2021-01-27 10:53:29 +03:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void VerifyMapTypeConverterAttributeWithNullableOptionOn()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
const string source = "";
|
|
|
|
var expectedInterface = $@"
|
|
|
|
{Constants.GeneratedFilesHeader}
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace MapTo
|
|
|
|
{{
|
|
|
|
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
|
|
|
|
public sealed class MapTypeConverterAttribute : Attribute
|
|
|
|
{{
|
|
|
|
public MapTypeConverterAttribute(Type converter, object[]? converterParameters = null)
|
|
|
|
{{
|
|
|
|
Converter = converter;
|
|
|
|
ConverterParameters = converterParameters;
|
|
|
|
}}
|
|
|
|
|
|
|
|
public Type Converter {{ get; }}
|
|
|
|
|
|
|
|
public object[]? ConverterParameters {{ get; }}
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
".Trim();
|
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions, nullableContextOptions: NullableContextOptions.Enable);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.ShouldContainSource(MapTypeConverterAttributeSource.AttributeName, expectedInterface);
|
|
|
|
}
|
2021-01-23 14:05:58 +03:00
|
|
|
|
2021-01-21 11:19:17 +03:00
|
|
|
[Fact]
|
|
|
|
public void When_FoundMatchingPropertyNameWithDifferentImplicitlyConvertibleType_Should_GenerateTheProperty()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
var source = GetSourceText(new SourceGeneratorOptions(
|
|
|
|
true,
|
|
|
|
PropertyBuilder: builder =>
|
|
|
|
{
|
|
|
|
builder
|
|
|
|
.PadLeft(Indent2).AppendLine("public long Prop4 { get; set; }");
|
|
|
|
},
|
|
|
|
SourcePropertyBuilder: builder => builder.PadLeft(Indent2).AppendLine("public int Prop4 { get; set; }")));
|
|
|
|
|
|
|
|
var expectedResult = @"
|
|
|
|
partial class Foo
|
|
|
|
{
|
|
|
|
public Foo(Test.Models.Baz baz)
|
|
|
|
{
|
|
|
|
if (baz == null) throw new ArgumentNullException(nameof(baz));
|
|
|
|
|
|
|
|
Prop1 = baz.Prop1;
|
|
|
|
Prop2 = baz.Prop2;
|
|
|
|
Prop3 = baz.Prop3;
|
|
|
|
Prop4 = baz.Prop4;
|
|
|
|
}
|
|
|
|
".Trim();
|
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedResult);
|
|
|
|
}
|
2021-01-23 14:05:58 +03:00
|
|
|
|
2021-01-21 11:19:17 +03:00
|
|
|
[Fact]
|
|
|
|
public void When_FoundMatchingPropertyNameWithIncorrectConverterType_ShouldReportError()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
var source = GetSourceText(new SourceGeneratorOptions(
|
|
|
|
true,
|
|
|
|
PropertyBuilder: builder =>
|
|
|
|
{
|
|
|
|
builder
|
|
|
|
.PadLeft(Indent2).AppendLine("[IgnoreProperty]")
|
|
|
|
.PadLeft(Indent2).AppendLine("public long IgnoreMe { get; set; }")
|
2021-01-21 19:41:12 +03:00
|
|
|
.PadLeft(Indent2).AppendLine("[MapTypeConverter(typeof(Prop4Converter))]")
|
2021-01-21 11:19:17 +03:00
|
|
|
.PadLeft(Indent2).AppendLine("public long Prop4 { get; set; }");
|
|
|
|
},
|
|
|
|
SourcePropertyBuilder: builder => builder.PadLeft(Indent2).AppendLine("public string Prop4 { get; set; }")));
|
|
|
|
|
|
|
|
source += @"
|
|
|
|
namespace Test
|
|
|
|
{
|
|
|
|
using MapTo;
|
|
|
|
|
|
|
|
public class Prop4Converter: ITypeConverter<string, int>
|
|
|
|
{
|
2021-01-23 14:05:58 +03:00
|
|
|
public int Convert(string source, object[] converterParameters) => int.Parse(source);
|
2021-01-21 11:19:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
";
|
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
2021-01-23 14:05:58 +03:00
|
|
|
|
2021-01-21 11:19:17 +03:00
|
|
|
// Assert
|
|
|
|
var expectedError = DiagnosticProvider.InvalidTypeConverterGenericTypesError(GetSourcePropertySymbol("Prop4", compilation), GetSourcePropertySymbol("Prop4", compilation, "Baz"));
|
|
|
|
diagnostics.ShouldBeUnsuccessful(expectedError);
|
|
|
|
}
|
2021-01-23 14:05:58 +03:00
|
|
|
|
2021-01-21 11:19:17 +03:00
|
|
|
[Fact]
|
|
|
|
public void When_FoundMatchingPropertyNameWithConverterType_ShouldUseTheConverterToAssignProperties()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
var source = GetSourceText(new SourceGeneratorOptions(
|
|
|
|
true,
|
|
|
|
PropertyBuilder: builder =>
|
|
|
|
{
|
|
|
|
builder
|
2021-01-21 19:41:12 +03:00
|
|
|
.PadLeft(Indent2).AppendLine("[MapTypeConverter(typeof(Prop4Converter))]")
|
2021-01-21 11:19:17 +03:00
|
|
|
.PadLeft(Indent2).AppendLine("public long Prop4 { get; set; }");
|
|
|
|
},
|
|
|
|
SourcePropertyBuilder: builder => builder.PadLeft(Indent2).AppendLine("public string Prop4 { get; set; }")));
|
|
|
|
|
|
|
|
source += @"
|
|
|
|
namespace Test
|
|
|
|
{
|
|
|
|
using MapTo;
|
|
|
|
|
|
|
|
public class Prop4Converter: ITypeConverter<string, long>
|
|
|
|
{
|
2021-01-23 14:05:58 +03:00
|
|
|
public long Convert(string source, object[] converterParameters) => long.Parse(source);
|
2021-01-21 11:19:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
";
|
|
|
|
|
2021-01-23 14:05:58 +03:00
|
|
|
const string expectedSyntax = "Prop4 = new Test.Prop4Converter().Convert(baz.Prop4, null);";
|
2021-01-21 11:19:17 +03:00
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
2021-01-23 14:05:58 +03:00
|
|
|
|
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedSyntax);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void When_FoundMatchingPropertyNameWithConverterType_ShouldUseTheConverterAndItsParametersToAssignProperties()
|
|
|
|
{
|
|
|
|
// Arrange
|
|
|
|
var source = GetSourceText(new SourceGeneratorOptions(
|
|
|
|
true,
|
|
|
|
PropertyBuilder: builder =>
|
|
|
|
{
|
|
|
|
builder
|
|
|
|
.PadLeft(Indent2).AppendLine("[MapTypeConverter(typeof(Prop4Converter), new object[]{\"G\", 'C', 10})]")
|
|
|
|
.PadLeft(Indent2).AppendLine("public string Prop4 { get; set; }");
|
|
|
|
},
|
|
|
|
SourcePropertyBuilder: builder => builder.PadLeft(Indent2).AppendLine("public long Prop4 { get; set; }")));
|
|
|
|
|
|
|
|
source += @"
|
|
|
|
namespace Test
|
|
|
|
{
|
|
|
|
using MapTo;
|
|
|
|
|
|
|
|
public class Prop4Converter: ITypeConverter<long, string>
|
|
|
|
{
|
|
|
|
public string Convert(long source, object[] converterParameters) => source.ToString(converterParameters[0] as string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
";
|
|
|
|
|
|
|
|
const string expectedSyntax = "Prop4 = new Test.Prop4Converter().Convert(baz.Prop4, new object[] { \"G\", 'C', 10 });";
|
|
|
|
|
|
|
|
// Act
|
|
|
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
|
|
|
|
2021-01-21 11:19:17 +03:00
|
|
|
// Assert
|
|
|
|
diagnostics.ShouldBeSuccessful();
|
|
|
|
compilation.SyntaxTrees.Last().ToString().ShouldContain(expectedSyntax);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static PropertyDeclarationSyntax GetPropertyDeclarationSyntax(SyntaxTree syntaxTree, string targetPropertyName, string targetClass = "Foo")
|
|
|
|
{
|
|
|
|
return syntaxTree.GetRoot()
|
|
|
|
.DescendantNodes()
|
|
|
|
.OfType<ClassDeclarationSyntax>()
|
|
|
|
.Single(c => c.Identifier.ValueText == targetClass)
|
|
|
|
.DescendantNodes()
|
|
|
|
.OfType<PropertyDeclarationSyntax>()
|
|
|
|
.Single(p => p.Identifier.ValueText == targetPropertyName);
|
|
|
|
}
|
2021-01-23 14:05:58 +03:00
|
|
|
|
2021-01-21 11:19:17 +03:00
|
|
|
private static IPropertySymbol GetSourcePropertySymbol(string propertyName, Compilation compilation, string targetClass = "Foo")
|
|
|
|
{
|
|
|
|
var syntaxTree = compilation.SyntaxTrees.First();
|
|
|
|
var propSyntax = GetPropertyDeclarationSyntax(syntaxTree, propertyName, targetClass);
|
2021-01-23 14:05:58 +03:00
|
|
|
|
2021-01-21 11:19:17 +03:00
|
|
|
var semanticModel = compilation.GetSemanticModel(syntaxTree);
|
|
|
|
return semanticModel.GetDeclaredSymbol(propSyntax);
|
|
|
|
}
|
2020-12-19 10:55:47 +03:00
|
|
|
}
|
2020-12-21 19:34:53 +03:00
|
|
|
}
|