示例#1
0
        public static void GenerateWithRecords()
        {
            var(diagnostics, output) = MapGeneratorMapFromTests.GetGeneratedOutput(
                @"using InlineMapping;

[MapFrom(typeof(Source))]
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,"));
            });
        }
示例#2
0
        public static void GenerateWhenSourceIsInNamespaceAndDestinationIsNotInNamespace()
        {
            var(diagnostics, output) = MapGeneratorMapFromTests.GetGeneratedOutput(
                @"using InlineMapping;

[MapFrom(typeof(SourceNamespace.Source))]
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("namespace SourceNamespace"));
                Assert.That(output, Does.Contain("Id = self.Id,"));
            });
        }
示例#3
0
        public static void GenerateWhenPropertyTypesDoNotMatch()
        {
            var(diagnostics, output) = MapGeneratorMapFromTests.GetGeneratedOutput(
                @"using InlineMapping;

[MapFrom(typeof(Source))]
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));
            });
        }
示例#4
0
        public static void GenerateWhenDestinationDoesNotMapAllProperties()
        {
            var(diagnostics, output) = MapGeneratorMapFromTests.GetGeneratedOutput(
                @"using InlineMapping;

[MapFrom(typeof(Source))]
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,"));
            });
        }
示例#5
0
        public static void GenerateWhenDestinationHasPublicMultipleArgumentConstructor()
        {
            var(diagnostics, output) = MapGeneratorMapFromTests.GetGeneratedOutput(
                @"using InlineMapping;

[MapFrom(typeof(Source))]
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));
            });
        }
示例#6
0
        public static void GenerateWhenDestinationSetterIsNotPublic()
        {
            var(diagnostics, output) = MapGeneratorMapFromTests.GetGeneratedOutput(
                @"using InlineMapping;

[MapFrom(typeof(Source))]
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));
            });
        }
示例#7
0
        public static void GenerateWhenNoPropertiesExist()
        {
            var(diagnostics, output) = MapGeneratorMapFromTests.GetGeneratedOutput(
                @"using InlineMapping;

[MapFrom(typeof(Source))]
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));
            });
        }