MapTo/test/MapTo.Tests/IgnorePropertyAttributeTest...

73 lines
2.2 KiB
C#
Raw Normal View History

2021-01-30 11:59:39 +03:00
using System.Linq;
using MapTo.Extensions;
using MapTo.Sources;
using MapTo.Tests.Extensions;
using MapTo.Tests.Infrastructure;
using Shouldly;
using Xunit;
using static MapTo.Tests.Common;
namespace MapTo.Tests
{
public class IgnorePropertyAttributeTests
{
[Fact]
public void VerifyIgnorePropertyAttribute()
{
// Arrange
const string source = "";
var expectedAttribute = $@"
{Constants.GeneratedFilesHeader}
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();
compilation.SyntaxTrees.ShouldContainSource(IgnorePropertyAttributeSource.AttributeName, expectedAttribute);
}
[Fact]
public void When_IgnorePropertyAttributeIsSpecified_Should_NotGenerateMappingsForThatProperty()
{
// Arrange
var source = GetSourceText(new SourceGeneratorOptions(
true,
PropertyBuilder: builder =>
{
builder
.WriteLine("[IgnoreProperty]")
.WriteLine("public int Prop4 { get; set; }");
2021-01-30 11:59:39 +03:00
},
SourcePropertyBuilder: builder => builder.WriteLine("public int Prop4 { get; set; }")));
2021-01-30 11:59:39 +03:00
var expectedResult = @"
partial class Foo
{
public Foo(Baz baz)
2021-01-30 11:59:39 +03:00
{
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);
// Assert
diagnostics.ShouldBeSuccessful();
2021-02-13 14:27:32 +03:00
compilation.SyntaxTrees.Last().ShouldContainPartialSource(expectedResult);
2021-01-30 11:59:39 +03:00
}
}
}