Skip to content

Commit

Permalink
Merge pull request MapsterMapper#221 from satano/MapToInterface
Browse files Browse the repository at this point in the history
Support map directly to interface
  • Loading branch information
chaowlert authored Jan 19, 2020
2 parents 63d678e + 7f95158 commit c03bbe7
Show file tree
Hide file tree
Showing 11 changed files with 707 additions and 22 deletions.
131 changes: 131 additions & 0 deletions src/Mapster.Tests/DynamicTypeGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using Mapster.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
using System.Collections.Generic;

namespace Mapster.Tests
{
[TestClass]
public class DynamicTypeGeneratorTests
{
private interface INotVisibleInterface
{
int Id { get; set; }
string Name { get; set; }
}

public interface ISimpleInterface
{
int Id { get; set; }
string Name { get; set; }
}

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);
}

[TestMethod]
public void ThrowExceptionWhenInterfaceIsNotVisible()
{
void action() => DynamicTypeGenerator.GetTypeForInterface(typeof(INotVisibleInterface));

var ex = Should.Throw<InvalidOperationException>(action);
ex.Message.ShouldContain("not accessible");
}

[TestMethod]
public void CreateTypeForSimpleInterface()
{
Type iClass = DynamicTypeGenerator.GetTypeForInterface(typeof(ISimpleInterface));

ISimpleInterface instance = (ISimpleInterface)Activator.CreateInstance(iClass);

instance.ShouldNotBeNull();

instance.Id = 42;
instance.Name = "Lorem ipsum";

instance.Id.ShouldBe(42);
instance.Name.ShouldBe("Lorem ipsum");
}

[TestMethod]
public void CreateTypeForInheritedInterface()
{
Type iClass = DynamicTypeGenerator.GetTypeForInterface(typeof(IInheritedInterface));

IInheritedInterface instance = (IInheritedInterface)Activator.CreateInstance(iClass);

instance.ShouldNotBeNull();

instance.Id = 42;
instance.Name = "Lorem ipsum";
instance.Value = 24;

instance.Id.ShouldBe(42);
instance.Name.ShouldBe("Lorem ipsum");
instance.Value.ShouldBe(24);
}

[TestMethod]
public void CreateTypeForComplexInterface()
{
Type iClass = DynamicTypeGenerator.GetTypeForInterface(typeof(IComplexInterface));

IComplexInterface instance = (IComplexInterface)Activator.CreateInstance(iClass);

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);

int i = 0;
string s = null;
Should.Throw<NotImplementedException>(() => instance.BaseMethod(), "Call BaseMethod.");
Should.Throw<NotImplementedException>(() => instance.SimpleMethod(), "Call SimpleMethod.");
Should.Throw<NotImplementedException>(() => instance.ComplexMethod(123, ref i, out s), "Call ComplexMethod.");
}
}
}
Loading

0 comments on commit c03bbe7

Please sign in to comment.