Skip to content

Instantly share code, notes, and snippets.

@brenovieira
Created August 19, 2015 22:12
Show Gist options
  • Save brenovieira/106b904f3899713e090d to your computer and use it in GitHub Desktop.
Save brenovieira/106b904f3899713e090d to your computer and use it in GitHub Desktop.
using System.Linq;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Should;
using Xunit;
namespace UnitTestProject1
{
public class MapAndProjectEnumsTest
{
const SourceType SrcType = SourceType.Type2;
const DestinationType DestType = DestinationType.Type2;
private readonly Source _source = new Source { Type = SrcType };
class Source
{
public SourceType Type { get; set; }
}
enum SourceType
{
Type1 = 0,
Type2 = 1
}
class Destination
{
public DestinationType Type { get; set; }
}
enum DestinationType
{
Type1 = 0,
Type2 = 1
}
[Fact]
public void Should_map_and_project_correctly_but_project_fails()
{
Mapper.CreateMap<Source, Destination>();
//map
var destination = Mapper.Map<Source, Destination>(_source);
destination.Type.ShouldEqual(DestType);
//project
var query = (new Source[] { _source }).AsQueryable();
var destinations = query.ProjectTo<Destination>().ToArray();
destinations[0].Type.ShouldEqual(DestType);
}
[Fact]
public void Should_map_and_project_correctly_but_map_fails()
{
Mapper.CreateMap<Source, Destination>();
Mapper.CreateMap<SourceType, DestinationType>().ProjectUsing(s => (DestinationType)s);
//map
var destination = Mapper.Map<Source, Destination>(_source);
destination.Type.ShouldEqual(DestType);
//project
var query = (new Source[] { _source }).AsQueryable();
var destinations = query.ProjectTo<Destination>().ToArray();
destinations[0].Type.ShouldEqual(DestType);
}
[Fact]
public void Should_map_and_project_correctly()
{
Mapper.CreateMap<Source, Destination>();
var m = Mapper.CreateMap<SourceType, DestinationType>();
m.ProjectUsing(s => (DestinationType)s);
m.ConvertUsing(s => (DestinationType)s);
//map
var destination = Mapper.Map<Source, Destination>(_source);
destination.Type.ShouldEqual(DestType);
//project
var query = (new Source[] { _source }).AsQueryable();
var destinations = query.ProjectTo<Destination>().ToArray();
destinations[0].Type.ShouldEqual(DestType);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment