public static void GenerateWhenPropertyTypesDoNotMatch() { var(diagnostics, output) = MapGeneratorMapTests.GetGeneratedOutput( @"using InlineMapping; [assembly: Map(typeof(Source), typeof(Destination))] public class Destination { public string Id { get; set; } } public class Source { public int Id { get; set; } }"); Assert.Multiple(() => { Assert.That(diagnostics.Length, Is.EqualTo(3)); Assert.That(() => diagnostics.Single(_ => _.Id == NoPropertyMapsFoundDiagnostic.Id), Throws.Nothing); Assert.That(() => diagnostics.Single(_ => _.Id == NoMatchDiagnostic.Id && _.GetMessage().Contains("source type Source", StringComparison.InvariantCulture)), Throws.Nothing); Assert.That(() => diagnostics.Single(_ => _.Id == NoMatchDiagnostic.Id && _.GetMessage().Contains("destination type Destination", StringComparison.InvariantCulture)), Throws.Nothing); Assert.That(output, Is.EqualTo(string.Empty)); }); }
public static void GenerateWithRecords() { var(diagnostics, output) = MapGeneratorMapTests.GetGeneratedOutput( @"using InlineMapping; [assembly: Map(typeof(Source), typeof(Destination))] public record Destination { public string Id { get; init; } } public record Source { public string Id { get; init; } }"); Assert.Multiple(() => { Assert.That(diagnostics.Length, Is.EqualTo(0)); Assert.That(output, Does.Not.Contain("namespace")); Assert.That(output, Does.Contain("using System;")); Assert.That(output, Does.Contain("public static Destination MapToDestination(this Source self) =>")); Assert.That(output, Does.Contain("self is null ? throw new ArgumentNullException(nameof(self)) :")); Assert.That(output, Does.Contain("Id = self.Id,")); }); }
public static void GenerateWhenDestinationDoesNotMapAllProperties() { var(diagnostics, output) = MapGeneratorMapTests.GetGeneratedOutput( @"using InlineMapping; [assembly: Map(typeof(Source), typeof(Destination))] public class Destination { public string Id { get; set; } public string Name { get; set; } } public class Source { public string Id { get; set; } }"); Assert.Multiple(() => { Assert.That(diagnostics.Length, Is.EqualTo(1)); var noMatchMessage = diagnostics.Single(_ => _.Id == NoMatchDiagnostic.Id).GetMessage(); Assert.That(noMatchMessage, Contains.Substring("Name")); Assert.That(noMatchMessage, Contains.Substring("destination type Destination")); Assert.That(output, Does.Not.Contain("namespace")); Assert.That(output, Does.Contain("using System;")); Assert.That(output, Does.Contain("public static Destination MapToDestination(this Source self) =>")); Assert.That(output, Does.Contain("self is null ? throw new ArgumentNullException(nameof(self)) :")); Assert.That(output, Does.Contain("Id = self.Id,")); }); }
public static void GenerateWhenDestinationHasPublicMultipleArgumentConstructor() { var(diagnostics, output) = MapGeneratorMapTests.GetGeneratedOutput( @"using InlineMapping; [assembly: Map(typeof(Source), typeof(Destination))] public class Destination { public Destination(int id) { } public string Id { get; set; } } public class Source { public string Id { get; set; } }"); Assert.Multiple(() => { Assert.That(diagnostics.Length, Is.EqualTo(1)); Assert.That(() => diagnostics.Single(_ => _.Id == NoArgumentConstructorDiagnostic.Id), Throws.Nothing); Assert.That(output, Is.EqualTo(string.Empty)); }); }
public static void GenerateWhenDestinationSetterIsNotPublic() { var(diagnostics, output) = MapGeneratorMapTests.GetGeneratedOutput( @"using InlineMapping; [assembly: Map(typeof(Source), typeof(Destination))] public class Destination { public string Id { get; private set; } } public class Source { public string Id { get; set; } }"); Assert.Multiple(() => { Assert.That(diagnostics.Length, Is.EqualTo(2)); Assert.That(() => diagnostics.Single(_ => _.Id == NoPropertyMapsFoundDiagnostic.Id), Throws.Nothing); var noMatchMessage = diagnostics.Single(_ => _.Id == NoMatchDiagnostic.Id).GetMessage(); Assert.That(noMatchMessage, Contains.Substring("Id")); Assert.That(noMatchMessage, Contains.Substring("source type Source")); Assert.That(output, Is.EqualTo(string.Empty)); }); }
public static void GenerateWhenDestinationIsNotInSourceNamespace() { var(diagnostics, output) = MapGeneratorMapTests.GetGeneratedOutput( @"using InlineMapping; [assembly: Map(typeof(SourceNamespace.Source), typeof(DestinationNamespace.Destination))] namespace DestinationNamespace { public class Destination { public string Id { get; set; } } } namespace SourceNamespace { public class Source { public string Id { get; set; } } }"); Assert.Multiple(() => { Assert.That(diagnostics.Length, Is.EqualTo(0)); Assert.That(output, Does.Contain("using DestinationNamespace;")); Assert.That(output, Does.Contain("namespace SourceNamespace")); Assert.That(output, Does.Contain("Id = self.Id,")); }); }
public static void GenerateWhenNoPropertiesExist() { var(diagnostics, output) = MapGeneratorMapTests.GetGeneratedOutput( @"using InlineMapping; [assembly: Map(typeof(Source), typeof(Destination))] public class Destination { } public class Source { }"); Assert.Multiple(() => { Assert.That(diagnostics.Length, Is.EqualTo(1)); Assert.That(diagnostics[0].Id, Is.EqualTo(NoPropertyMapsFoundDiagnostic.Id)); Assert.That(output, Is.EqualTo(string.Empty)); }); }