Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chaowlert committed Jan 6, 2016
1 parent 6797186 commit 79ff8e5
Show file tree
Hide file tree
Showing 41 changed files with 794 additions and 1,636 deletions.
6 changes: 4 additions & 2 deletions src/Mapster.Tests/WhenAddingCustomMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class WhenAddingCustomMappings
public void Property_Is_Mapped_To_Different_Property_Successfully()
{
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
.Map(dest => dest.AnotherName, src => src.Name);
.Map(dest => dest.AnotherName, src => src.Name)
.Recompile();

var poco = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

Expand All @@ -27,7 +28,8 @@ public void Property_Is_Mapped_To_Different_Property_Successfully()
public void Property_Is_Mapped_From_Null_Value_Successfully()
{
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
.Map(dest => dest.AnotherName, src => null);
.Map(dest => dest.AnotherName, src => null)
.Recompile();

var poco = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" };

Expand Down
55 changes: 33 additions & 22 deletions src/Mapster.Tests/WhenAddingPrimitiveTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ namespace Mapster.Tests
[TestFixture]
public class WhenAddingPrimitiveTypes
{
public WhenAddingPrimitiveTypes()
{
//this is to prevent type initialization exception
TypeAdapterConfig.GlobalSettings.PrimitiveTypes.Add(typeof (Uri));
TypeAdapter<Uri, Uri>.Recompile();
}

[TearDown]
public void TearDown()
{
Expand All @@ -16,20 +23,23 @@ public void TearDown()
[Test]
public void No_Primitive_Uri_Should_Throw()
{
TypeAdapter.ClearCache();
var sourceUri = new Uri("https://example.com");

var exception = Assert.Throws<ArgumentNullException>(() => TypeAdapter.Adapt<Uri, Uri>(sourceUri));
Console.WriteLine(exception.Message);

exception.ParamName.ShouldEqual("con");
try
{
TypeAdapterConfig.GlobalSettings.PrimitiveTypes.Clear();
TypeAdapter<Uri, Uri>.Recompile();

var sourceUri = new Uri("https://example.com");
TypeAdapter.Adapt<Uri, Uri>(sourceUri);
Assert.Fail("Should go to catch");
}
catch (ArgumentException) { }
}

[Test]
public void Set_Primitive_Uri_Success()
{
TypeAdapter.ClearCache();
TypeAdapterConfig.GlobalSettings.PrimitiveTypes.Add(typeof(Uri));
TypeAdapter<Uri, Uri>.Recompile();

var sourceUri = new Uri("https://example.com");
var targetUri = TypeAdapter.Adapt<Uri, Uri>(sourceUri);
Expand All @@ -40,26 +50,27 @@ public void Set_Primitive_Uri_Success()
[Test]
public void No_Primitive_Uri_Property_Should_Throw()
{
TypeAdapterConfig<SimplePoco, SimplePoco>.Clear();
var sourceDto = new SimplePoco
{
Id = 1,
Website = new Uri("https://example.com"),
};

var exception = Assert.Throws<InvalidOperationException>(() => TypeAdapter.Adapt<SimplePoco, SimplePoco>(sourceDto));
Console.WriteLine(exception.Message);

var innerException = exception.InnerException;
innerException.ShouldBeType<ArgumentNullException>();
((ArgumentNullException)innerException).ParamName.ShouldEqual("con");
try {
TypeAdapterConfig.GlobalSettings.PrimitiveTypes.Clear();
TypeAdapter<Uri, Uri>.Recompile();

var sourceDto = new SimplePoco
{
Id = 1,
Website = new Uri("https://example.com"),
};

TypeAdapter.Adapt<SimplePoco, SimplePoco>(sourceDto);
Assert.Fail("Should go to catch");
}
catch (ArgumentException) { }
}

[Test]
public void Set_Primitive_Uri_Property_Success()
{
TypeAdapterConfig<SimplePoco, SimplePoco>.Clear();
TypeAdapterConfig.GlobalSettings.PrimitiveTypes.Add(typeof(Uri));
TypeAdapter<Uri, Uri>.Recompile();

var sourceDto = new SimplePoco
{
Expand Down
6 changes: 3 additions & 3 deletions src/Mapster.Tests/WhenConfiguringMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public void IgnoreMemberTest()
};

TypeAdapterConfig<ConfigA, ConfigB>.NewConfig()
.IgnoreMember(dest => dest.Id);
.Ignore(dest => dest.Id);

var objB = ClassAdapter<ConfigA, ConfigB>.Adapt(objA);
var objB = TypeAdapter<ConfigA, ConfigB>.Adapt(objA);

Assert.IsNotNull(objB);

Expand All @@ -111,7 +111,7 @@ public void MapFromTest()
//.Map(dest => dest.FullName, (src) => string.Concat(src.Name, " ", src.Surname));
.Map(dest => dest.FullName, src => string.Concat(src.Name, " ", src.Surname));

var objD = ClassAdapter<ConfigC, ConfigD>.Adapt(objC);
var objD = TypeAdapter<ConfigC, ConfigD>.Adapt(objC);

Assert.IsNotNull(objD);

Expand Down
31 changes: 19 additions & 12 deletions src/Mapster.Tests/WhenExplicitMappingRequired.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using NUnit.Framework;
using Should;

Expand All @@ -19,18 +20,24 @@ public void TearDown()
[Test]
public void Unmapped_Classes_Should_Throw()
{
TypeAdapterConfig.GlobalSettings.RequireExplicitMapping = true;

TypeAdapterConfig<SimplePoco, SimpleDto>.Clear();

var simplePoco = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

var exception = Assert.Throws<InvalidOperationException>(() => TypeAdapter.Adapt<SimplePoco, SimpleDto>(simplePoco));

Console.WriteLine(exception.Message);

exception.Message.ShouldContain("SimplePoco");
exception.Message.ShouldContain("SimpleDto");
try
{
//this is to prevent TypeInitializeException
TypeAdapterConfig<SimplePoco, SimpleDto>.Clear();

TypeAdapterConfig.GlobalSettings.RequireExplicitMapping = true;
TypeAdapterConfig<SimplePoco, SimpleDto>.Clear();

var simplePoco = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

TypeAdapter.Adapt<SimplePoco, SimpleDto>(simplePoco);
Assert.Fail();
}
catch (TargetInvocationException ex)
{
ex.InnerException.Message.ShouldContain("SimplePoco");
ex.InnerException.Message.ShouldContain("SimpleDto");
}
}

[Test]
Expand Down
6 changes: 3 additions & 3 deletions src/Mapster.Tests/WhenFlattening.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class WhenFlattening
[Test]
public void GetMethodTest()
{
var b = ClassAdapter<A, B>.Adapt(new A { X = 100, Y = 50 });
var b = TypeAdapter<A, B>.Adapt(new A { X = 100, Y = 50 });

Assert.IsNotNull(b);
Assert.IsTrue(b.Total == 150);
Expand All @@ -53,7 +53,7 @@ public void GetMethodTest()
[Test]
public void PropertyTest()
{
var d = ClassAdapter<C, D>.Adapt(new C { BClass = new B { Total = 250 } });
var d = TypeAdapter<C, D>.Adapt(new C { BClass = new B { Total = 250 } });

Assert.IsNotNull(d);
Assert.IsTrue(d.BClassTotal == 250);
Expand All @@ -62,7 +62,7 @@ public void PropertyTest()
[Test]
public void PropertyTest_NameWithUnderscore()
{
var e = ClassAdapter<C, E>.Adapt(new C { BClass = new B { Total = 250 } });
var e = TypeAdapter<C, E>.Adapt(new C { BClass = new B { Total = 250 } });

Assert.IsNotNull(e);
Assert.IsTrue(e.BClass_Total == 250);
Expand Down
46 changes: 32 additions & 14 deletions src/Mapster.Tests/WhenHandlingUnmappedMembers.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using NUnit.Framework;
using Should;

Expand All @@ -18,6 +19,7 @@ public void TearDown()
public void No_Errors_Thrown_With_Default_Configuration_On_Unmapped_Primitive()
{
TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = false;
TypeAdapterConfig<ParentPoco, ParentDto>.Clear();

var source = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

Expand All @@ -31,19 +33,27 @@ public void No_Errors_Thrown_With_Default_Configuration_On_Unmapped_Primitive()
[Test]
public void Error_Thrown_With_Explicit_Configuration_On_Unmapped_Primitive()
{
TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true;

var source = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" };

var exception = Assert.Throws<ArgumentOutOfRangeException>(() => TypeAdapter.Adapt<SimplePoco, SimpleDto>(source));

exception.Message.ShouldContain("UnmappedMember");
try
{
TypeAdapterConfig<ParentPoco, ParentDto>.Clear();
TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true;
TypeAdapterConfig<ParentPoco, ParentDto>.Clear();

var source = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

TypeAdapter.Adapt<SimplePoco, SimpleDto>(source);
}
catch (TargetInvocationException ex)
{
ex.InnerException.Message.ShouldContain("UnmappedMember");
}
}

[Test]
public void No_Errors_Thrown_With_Default_Configuration_On_Unmapped_Child_Collection()
{
TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = false;
TypeAdapterConfig<ParentPoco, ParentDto>.Clear();

var source = new ParentPoco { Id = Guid.NewGuid(), Name = "TestName", Children = new List<ChildPoco> { new ChildPoco { Id = Guid.NewGuid(), Name = "TestName" } } };

Expand All @@ -57,13 +67,21 @@ public void No_Errors_Thrown_With_Default_Configuration_On_Unmapped_Child_Collec
[Test]
public void Error_Thrown_With_Explicit_Configuration_On_Unmapped_Child_Collection()
{
TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true;

var source = new ParentPoco { Id = Guid.NewGuid(), Name = "TestName", Children = new List<ChildPoco> { new ChildPoco { Id = Guid.NewGuid(), Name = "TestName" } } };

var exception = Assert.Throws<ArgumentOutOfRangeException>(() => TypeAdapter.Adapt<ParentPoco, ParentDto>(source));

exception.Message.ShouldContain("UnmappedChildren");
try
{
TypeAdapterConfig<ParentPoco, ParentDto>.Clear();
TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true;
TypeAdapterConfig<ParentPoco, ParentDto>.Clear();

var source = new ParentPoco {Id = Guid.NewGuid(), Name = "TestName", Children = new List<ChildPoco> {new ChildPoco {Id = Guid.NewGuid(), Name = "TestName"}}};

TypeAdapter.Adapt<ParentPoco, ParentDto>(source);
Assert.Fail();
}
catch (TargetInvocationException ex)
{
ex.InnerException.Message.ShouldContain("UnmappedChildren");
}

}

Expand Down
4 changes: 2 additions & 2 deletions src/Mapster.Tests/WhenMappingCollections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void MapCollectionProperty()
RelatedDepartments = new Departments[] { Departments.IT, Departments.Finance }
};

var dto = ClassAdapter<Person, PersonDTO>.Adapt(person);
var dto = TypeAdapter<Person, PersonDTO>.Adapt(person);
Assert.IsNotNull(dto);
Assert.IsTrue(dto.Id == person.Id &&
dto.Name == person.Name &&
Expand Down Expand Up @@ -169,7 +169,7 @@ public void MapCollection()

var persons = new List<Person>() { person };

var dtos = (Person[])CollectionAdapter<Person, List<Person>, Person, Person[]>.Adapt(persons);
var dtos = TypeAdapter<List<Person>, Person[]>.Adapt(persons);

Assert.IsNotNull(dtos);
Assert.IsTrue(dtos.Length == 1);
Expand Down
9 changes: 6 additions & 3 deletions src/Mapster.Tests/WhenMappingConditionally.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class WhenMappingConditionally
public void False_Condition_Primitive_Does_Not_Map()
{
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
.Map(dest => dest.Name, src => src.Name, cond => false);
.Map(dest => dest.Name, src => src.Name, cond => false)
.Recompile();

var poco = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" };

Expand All @@ -26,7 +27,8 @@ public void False_Condition_Primitive_Does_Not_Map()
public void Failed_Condition_Primitive_Does_Not_Map()
{
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
.Map(dest => dest.Name, src => src.Name, cond => cond.Name != "TestName");
.Map(dest => dest.Name, src => src.Name, cond => cond.Name != "TestName")
.Recompile();

var poco = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" };

Expand All @@ -41,7 +43,8 @@ public void Passed_Condition_Primitive_Does_Map()
{

TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
.Map(dest => dest.Name, src => src.Name, cond => cond.Name == "TestName");
.Map(dest => dest.Name, src => src.Name, cond => cond.Name == "TestName")
.Recompile();

var poco = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" };

Expand Down
2 changes: 1 addition & 1 deletion src/Mapster.Tests/WhenMappingEntityWithOnlyPrimitives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Customer GetCustomer()
[Test]
public void ConvertPrimitiveEntityToDto()
{
var dto = ClassAdapter<Customer, CustomerDTO>.Adapt(GetCustomer());
var dto = TypeAdapter<Customer, CustomerDTO>.Adapt(GetCustomer());

Assert.IsNotNull(dto);
Assert.IsTrue(dto.Id == 1 &&
Expand Down
4 changes: 2 additions & 2 deletions src/Mapster.Tests/WhenMappingEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Int_Is_Mapped_To_Enum()

var employee = new Employee { Id = Guid.NewGuid(), Name = "Timuçin", Surname = "KIVANÇ", Department = (int)Departments.IT };

var dto = ClassAdapter<Employee, EmployeeDTO>.Adapt(employee);
var dto = TypeAdapter<Employee, EmployeeDTO>.Adapt(employee);

Assert.IsNotNull(dto);

Expand All @@ -64,7 +64,7 @@ public void String_Is_Mapped_To_Enum()
{
var employee = new EmployeeWithStringEnum { Id = Guid.NewGuid(), Name = "Timuçin", Department = Departments.IT.ToString() };

var dto = ClassAdapter<EmployeeWithStringEnum, EmployeeDTO>.Adapt(employee);
var dto = TypeAdapter<EmployeeWithStringEnum, EmployeeDTO>.Adapt(employee);

dto.ShouldNotBeNull();

Expand Down
3 changes: 1 addition & 2 deletions src/Mapster.Tests/WhenMappingPrimitives.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Text;
using Mapster.Adapters;
using NUnit.Framework;
using Should;

Expand All @@ -12,7 +11,7 @@ public class WhenMappingPrimitives
[Test]
public void Integer_Is_Mapped_To_Byte()
{
byte b = PrimitiveAdapter<int, byte>.Adapt(5);
byte b = TypeAdapter<int, byte>.Adapt(5);

Assert.IsTrue(b == 5);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Mapster.Tests/WhenUsingTypeResolvers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public SimpleDto Resolve(SimplePoco source)
UnmappedMember2 = "Unmapped2"
};
}

public SimpleDto Resolve(SimplePoco source, SimpleDto destination)
{
return Resolve(source);
}
}

public class SimplePoco
Expand Down
5 changes: 5 additions & 0 deletions src/Mapster.Tests/WhenValidatingMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ public SimpleDto Resolve(SimplePoco source)
Name = "I got converted!",
};
}

public SimpleDto Resolve(SimplePoco source, SimpleDto destination)
{
return Resolve(source);
}
}


Expand Down
Loading

0 comments on commit 79ff8e5

Please sign in to comment.