Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 161 lines (135 sloc) 4.051 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
using System.Collections.Generic;
using Should;
using Xunit;
using System.Linq;

namespace AutoMapper.UnitTests
{
namespace FillingExistingDestination
{

public class When_the_destination_object_is_specified : AutoMapperSpecBase
{
private Source _source;
private Destination _originalDest;
private Destination _dest;

public class Source
{
public int Value { get; set; }
}

public class Destination
{
public int Value { get; set; }
}

protected override void Establish_context()
{
base.Establish_context();

Mapper.CreateMap<Source, Destination>();

_source = new Source
{
Value = 10,
};
}

protected override void Because_of()
{
_originalDest = new Destination { Value = 1111 };
_dest = Mapper.Map<Source, Destination>(_source, _originalDest);
}

[Fact]
public void Should_do_the_translation()
{
_dest.Value.ShouldEqual(10);
}

[Fact]
public void Should_return_the_destination_object_that_was_passed_in()
{
_originalDest.ShouldBeSameAs(_dest);
}
}
       
        public class When_the_destination_object_is_specified_with_child_objects : AutoMapperSpecBase
        {
            private Source _source;
            private Destination _originalDest;
            private Destination _dest;

            public class Source
            {
                public int Value { get; set; }
                public ChildSource Child { get; set; }
            }

            public class Destination
            {
                public int Value { get; set; }
                public string Name { get; set; }
                public ChildDestination Child { get; set; }
            }

            public class ChildSource
            {
                public int Value { get; set; }
            }

            public class ChildDestination
            {
                public int Value { get; set; }
                public string Name { get; set; }
            }

            protected override void Establish_context()
            {
                base.Establish_context();

                Mapper.Initialize(cfg =>
                {
                    cfg.CreateMap<Source, Destination>(MemberList.Source);
                    cfg.CreateMap<ChildSource, ChildDestination>(MemberList.Source);
                });

                _source = new Source
                {
                    Value = 10,
                    Child = new ChildSource
                    {
                        Value = 20
                    }
                };
            }

            protected override void Because_of()
            {
                _originalDest = new Destination
                {
                    Value = 1111,
                    Name = "foo",
                    Child = new ChildDestination
                    {
                        Name = "bar"
                    }
                };
                _dest = Mapper.Map<Source, Destination>(_source, _originalDest);
            }

[Fact]
            public void Should_do_the_translation()
            {
                _dest.Value.ShouldEqual(10);
                _dest.Child.Value.ShouldEqual(20);
            }

[Fact]
            public void Should_return_the_destination_object_that_was_passed_in()
            {
                _dest.Name.ShouldEqual("foo");
                _dest.Child.Name.ShouldEqual("bar");
            }
        }


public class When_the_destination_object_is_specified_and_you_are_converting_an_enum : AutoMapperSpecBase
{
private string _result;

public enum SomeEnum
{
One,
Two,
Three
}

protected override void Because_of()
{
_result = Mapper.Map<SomeEnum, string>(SomeEnum.Two, "test");
}

[Fact]
public void Should_return_the_enum_as_a_string()
{
_result.ShouldEqual("Two");
}
}
}
}
Something went wrong with that request. Please try again.