Skip to content

Commit

Permalink
Add test for mapping to complex interface
Browse files Browse the repository at this point in the history
  • Loading branch information
satano committed Jan 18, 2020
1 parent b9c14a5 commit c23b1c5
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 10 deletions.
21 changes: 21 additions & 0 deletions src/Mapster.Tests/DynamicTypeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
using System.Collections.Generic;

namespace Mapster.Tests
{
Expand All @@ -25,17 +26,29 @@ public interface IInheritedInterface : ISimpleInterface
int Value { get; set; }
}

public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}

public interface IComplexBaseInterface
{
int Id { get; set; }
Foo Foo { get; set; }
void BaseMethod();
}

public interface IComplexInterface : IComplexBaseInterface
{
string Name { get; set; }
int? NullableInt { get; set; }
int ReadOnlyProp { get; }
int WriteOnlyProp { set; }
IEnumerable<int> Ints { get; set; }
IEnumerable<Foo> Foos { get; set; }
int[] IntArray { get; set; }
Foo[] FooArray { get; set; }
void SimpleMethod();
int ComplexMethod(byte b, ref int i, out string s);
}
Expand Down Expand Up @@ -93,10 +106,18 @@ public void CreateTypeForComplexInterface()
instance.ShouldNotBeNull();

instance.Id = 42;
instance.Foo = new Foo();
instance.NullableInt = 123;
instance.Name = "Lorem ipsum";
instance.WriteOnlyProp = 24;
instance.Ints = new List<int>();
instance.IntArray = new int[2];
instance.Foos = new List<Foo>();
instance.FooArray = new Foo[2];

instance.Id.ShouldBe(42);
instance.Foo.ShouldNotBeNull();
instance.NullableInt.ShouldBe(123);
instance.Name.ShouldBe("Lorem ipsum");
instance.ReadOnlyProp.ShouldBe(0);

Expand Down
132 changes: 122 additions & 10 deletions src/Mapster.Tests/WhenMappingToInterface.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Mapster.Tests
{
Expand All @@ -26,9 +28,11 @@ public void MapToInterface()
IDto idto = dto.Adapt<IDto>();

idto.ShouldNotBeNull();
idto.Id.ShouldBe(dto.Id);
idto.Name.ShouldBe(dto.Name);
idto.UnmappedTarget.ShouldBeNull();
idto.ShouldSatisfyAllConditions(
() => idto.Id.ShouldBe(dto.Id),
() => idto.Name.ShouldBe(dto.Name),
() => idto.UnmappedTarget.ShouldBeNull()
);
}

[TestMethod]
Expand All @@ -45,10 +49,82 @@ public void MapToInheritedInterface()
IInheritedDto idto = dto.Adapt<IInheritedDto>();

idto.ShouldNotBeNull();
idto.Id.ShouldBe(dto.Id);
idto.Name.ShouldBe(dto.Name);
idto.DateOfBirth.ShouldBe(dto.DateOfBirth);
idto.UnmappedTarget.ShouldBeNull();
idto.ShouldSatisfyAllConditions(
() => idto.Id.ShouldBe(dto.Id),
() => idto.Name.ShouldBe(dto.Name),
() => idto.DateOfBirth.ShouldBe(dto.DateOfBirth),
() => idto.UnmappedTarget.ShouldBeNull()
);
}

[TestMethod]
public void MapToComplexInterface()
{
var subItem = new ComplexDto
{
Name = "Inner lrem ipsum",
Int32 = 420,
Int64 = long.MaxValue,
NullInt1 = null,
NullInt2 = 240,
Floatn = 2.2F,
Doublen = 4.4,
DateTime = new DateTime(1978, 12, 10),
SubItem = null,
Dtos = new List<ComplexDto>(),
DtoArr = null,
Ints = new List<int>(),
IntArr = null
};

var dto = new ComplexDto
{
Name = "Lorem ipsum",
Int32 = 42,
Int64 = long.MaxValue,
NullInt1 = null,
NullInt2 = 24,
Floatn = 1.2F,
Doublen = 2.4,
DateTime = new DateTime(1978, 12, 10),
SubItem = subItem,
Dtos = new List<ComplexDto>(new[] { subItem, null }),
DtoArr = new ComplexDto[] { null, subItem },
Ints = new List<int>(new[] { 1, 2 }),
IntArr = new[] { 3, 4 }
};

IComplexInterface idto = dto.Adapt<IComplexInterface>();

idto.ShouldNotBeNull();
idto.ShouldSatisfyAllConditions(
() => idto.Name.ShouldBe(dto.Name),
() => idto.Int32.ShouldBe(dto.Int32),
() => idto.Int64.ShouldBe(dto.Int64),
() => idto.NullInt1.ShouldBeNull(),
() => idto.NullInt2.ShouldBe(dto.NullInt2),
() => idto.Floatn.ShouldBe(dto.Floatn),
() => idto.Doublen.ShouldBe(dto.Doublen),
() => idto.DateTime.ShouldBe(dto.DateTime)
);
idto.SubItem.ShouldSatisfyAllConditions(
() => idto.SubItem.Name.ShouldBe(dto.SubItem.Name),
() => idto.SubItem.Int32.ShouldBe(dto.SubItem.Int32),
() => idto.SubItem.Int64.ShouldBe(dto.SubItem.Int64),
() => idto.SubItem.NullInt1.ShouldBeNull(),
() => idto.SubItem.NullInt2.ShouldBe(dto.SubItem.NullInt2),
() => idto.SubItem.Floatn.ShouldBe(dto.SubItem.Floatn),
() => idto.SubItem.Doublen.ShouldBe(dto.SubItem.Doublen),
() => idto.SubItem.DateTime.ShouldBe(dto.SubItem.DateTime)
);
idto.ShouldSatisfyAllConditions(
() => idto.Dtos.Count().ShouldBe(dto.Dtos.Count()),
() => idto.DtoArr.Length.ShouldBe(dto.DtoArr.Length),
() => idto.Ints.First().ShouldBe(dto.Ints.First()),
() => idto.Ints.Last().ShouldBe(dto.Ints.Last()),
() => idto.IntArr[0].ShouldBe(dto.IntArr[0]),
() => idto.IntArr[1].ShouldBe(dto.IntArr[1])
);
}

[TestMethod]
Expand All @@ -64,9 +140,11 @@ public void MapToInterfaceWithMethods()
IInterfaceWithMethods idto = dto.Adapt<IInterfaceWithMethods>();

idto.ShouldNotBeNull();
idto.Id.ShouldBe(dto.Id);
idto.Name.ShouldBe(dto.Name);
Should.Throw<NotImplementedException>(() => idto.DoSomething());
idto.ShouldSatisfyAllConditions(
() => idto.Id.ShouldBe(dto.Id),
() => idto.Name.ShouldBe(dto.Name),
() => Should.Throw<NotImplementedException>(() => idto.DoSomething())
);
}

[TestMethod]
Expand Down Expand Up @@ -123,5 +201,39 @@ public class InheritedDto
public DateTime DateOfBirth { get; set; }
public string UnmappedSource { get; set; }
}

public interface IComplexInterface
{
string Name { get; set; }
int Int32 { get; set; }
long Int64 { get; set; }
int? NullInt1 { get; set; }
int? NullInt2 { get; set; }
float Floatn { get; set; }
double Doublen { get; set; }
DateTime DateTime { get; set; }
ComplexDto SubItem { get; set; }
IEnumerable<ComplexDto> Dtos { get; set; }
ComplexDto[] DtoArr { get; set; }
IEnumerable<int> Ints { get; set; }
int[] IntArr { get; set; }
}

public class ComplexDto
{
public string Name { get; set; }
public int Int32 { get; set; }
public long Int64 { set; get; }
public int? NullInt1 { get; set; }
public int? NullInt2 { get; set; }
public float Floatn { get; set; }
public double Doublen { get; set; }
public DateTime DateTime { get; set; }
public ComplexDto SubItem { get; set; }
public IEnumerable<ComplexDto> Dtos { get; set; }
public ComplexDto[] DtoArr { get; set; }
public IEnumerable<int> Ints { get; set; }
public int[] IntArr { get; set; }
}
}
}

0 comments on commit c23b1c5

Please sign in to comment.