public static void GenerateWithRecords() { var(diagnostics, output) = MapToGeneratorTests.GetGeneratedOutput( @"using InlineMapping; public record Destination { public string Id { get; init; } } [MapTo(typeof(Destination))] 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 GenerateWhenSourceIsInNamespaceAndDestinationIsNotInNamespace() { var(diagnostics, output) = MapToGeneratorTests.GetGeneratedOutput( @"using InlineMapping; public class Destination { public string Id { get; set; } } namespace SourceNamespace { [MapTo(typeof(Destination))] public class Source { public string Id { get; set; } } }"); Assert.Multiple(() => { Assert.That(diagnostics.Length, Is.EqualTo(0)); Assert.That(output, Does.Contain("namespace SourceNamespace")); Assert.That(output, Does.Contain("Id = self.Id,")); }); }
public static void GenerateWhenPropertyTypesDoNotMatch() { var(diagnostics, output) = MapToGeneratorTests.GetGeneratedOutput( @"using InlineMapping; public class Destination { public string Id { get; set; } } [MapTo(typeof(Destination))] public class Source { public int Id { get; set; } }"); Assert.Multiple(() => { Assert.That(diagnostics.Length, Is.EqualTo(3)); Assert.That(() => diagnostics.Single(_ => _.Id == NoPropertyMapsFoundDescriptorConstants.Id), Throws.Nothing); Assert.That(() => diagnostics.Single(_ => _.Id == NoMatchDescriptorConstants.Id && _.GetMessage().Contains("source type Source", StringComparison.InvariantCulture)), Throws.Nothing); Assert.That(() => diagnostics.Single(_ => _.Id == NoMatchDescriptorConstants.Id && _.GetMessage().Contains("destination type Destination", StringComparison.InvariantCulture)), Throws.Nothing); Assert.That(output, Is.EqualTo(string.Empty)); }); }
public static void GenerateWhenDestinationDoesNotMapAllProperties() { var(diagnostics, output) = MapToGeneratorTests.GetGeneratedOutput( @"using InlineMapping; public class Destination { public string Id { get; set; } public string Name { get; set; } } [MapTo(typeof(Destination))] public class Source { public string Id { get; set; } }"); Assert.Multiple(() => { Assert.That(diagnostics.Length, Is.EqualTo(1)); var noMatchMessage = diagnostics.Single(_ => _.Id == NoMatchDescriptorConstants.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) = MapToGeneratorTests.GetGeneratedOutput( @"using InlineMapping; public class Destination { public Destination(int id) { } public string Id { get; set; } } [MapTo(typeof(Destination))] public class Source { public string Id { get; set; } }"); Assert.Multiple(() => { Assert.That(diagnostics.Length, Is.EqualTo(1)); Assert.That(() => diagnostics.Single(_ => _.Id == NoArgumentConstructorDescriptorConstants.Id), Throws.Nothing); Assert.That(output, Is.EqualTo(string.Empty)); }); }
public static void GenerateWhenDestinationSetterIsNotPublic() { var(diagnostics, output) = MapToGeneratorTests.GetGeneratedOutput( @"using InlineMapping; public class Destination { public string Id { get; private set; } } [MapTo(typeof(Destination))] public class Source { public string Id { get; set; } }"); Assert.Multiple(() => { Assert.That(diagnostics.Length, Is.EqualTo(2)); Assert.That(() => diagnostics.Single(_ => _.Id == NoPropertyMapsFoundDescriptorConstants.Id), Throws.Nothing); var noMatchMessage = diagnostics.Single(_ => _.Id == NoMatchDescriptorConstants.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 GenerateWhenNoPropertiesExist() { var(diagnostics, output) = MapToGeneratorTests.GetGeneratedOutput( @"using InlineMapping; public class Destination { } [MapTo(typeof(Destination))] public class Source { }"); Assert.Multiple(() => { Assert.That(diagnostics.Length, Is.EqualTo(1)); Assert.That(diagnostics[0].Id, Is.EqualTo(NoPropertyMapsFoundDescriptorConstants.Id)); Assert.That(output, Is.EqualTo(string.Empty)); }); }