@@ -8,13 +8,13 @@ namespace AutoMapper.ExtendedConverters.Tests
88 [ TestClass ]
99 public class CollectionConverterTests
1010 {
11- class Model
11+ public class Model
1212 {
1313 public int Id { get ; set ; }
1414 public string Text { get ; set ; }
1515 }
1616
17- class Entity : IComparable < Entity >
17+ public class Entity : IComparable < Entity >
1818 {
1919 public int Id { get ; set ; }
2020 public string Text { get ; set ; }
@@ -33,10 +33,12 @@ public void Initialize()
3333 var config = new MapperConfiguration ( cfg => {
3434 cfg . CreateMap < Model , Entity > ( ) ;
3535
36+ cfg . CreateMap < ModelWrapper , EntityWrapper > ( ) ;
37+
3638 cfg . CreateMap < Model [ ] , SortedSet < Entity > > ( )
3739 . UsingCollectionConverter ( ( Model m ) => m . Id , ( Entity e ) => e . Id ) ;
38-
39- cfg . CreateMap < IEnumerable < Model > , LinkedList < Entity > > ( )
40+
41+ cfg . CreateMap < IEnumerable < Model > , ICollection < Entity > > ( )
4042 . UsingCollectionConverter ( ( Model m ) => m . Id , ( Entity e ) => e . Id ) ;
4143 } ) ;
4244
@@ -136,10 +138,20 @@ private static IEnumerable<Model> BuildSource()
136138 yield return new Model { Id = 5 , Text = "E" } ;
137139 }
138140
141+ public class ModelWrapper
142+ {
143+ public IEnumerable < Model > Collection { get ; set ; }
144+ }
145+
146+ public class EntityWrapper
147+ {
148+ public ICollection < Entity > Collection { get ; set ; }
149+ }
150+
139151 [ TestMethod ]
140152 public void ShouldMapAnyIEnumerable_ToAnyICollection ( )
141153 {
142- ICollection < Entity > dest = new LinkedList < Entity > ( new [ ] {
154+ var dest = new LinkedList < Entity > ( new [ ] {
143155 new Entity { Id = 1 , Text = "a" } ,
144156 new Entity { Id = 2 , Text = "b" } ,
145157 new Entity { Id = 3 , Text = "c" } ,
@@ -148,11 +160,16 @@ public void ShouldMapAnyIEnumerable_ToAnyICollection()
148160 var destArray = new Entity [ 3 ] ;
149161 dest . CopyTo ( destArray , 0 ) ;
150162
151- ICollection < Entity > res = Mapper . Map ( BuildSource ( ) , dest ) ;
163+ var destWrapper = new EntityWrapper { Collection = dest } ;
164+ var srcWrapper = new ModelWrapper { Collection = BuildSource ( ) } ;
165+
166+ var res = Mapper . Map ( srcWrapper , destWrapper ) ;
152167
153168 var resArray = new Entity [ 4 ] ;
154- res . CopyTo ( resArray , 0 ) ;
169+ res . Collection . CopyTo ( resArray , 0 ) ;
155170
171+ // should preserve collection type
172+ Assert . AreEqual ( dest . GetType ( ) , res . Collection . GetType ( ) ) ;
156173 // should preserve objects with keys both in source and destination
157174 Assert . AreSame ( destArray [ 0 ] , resArray [ 0 ] ) ;
158175 Assert . AreSame ( destArray [ 1 ] , resArray [ 1 ] ) ;
@@ -164,8 +181,8 @@ public void ShouldMapAnyIEnumerable_ToAnyICollection()
164181 Assert . AreNotSame ( destArray [ 2 ] , resArray [ 3 ] ) ;
165182
166183 // should map values of collection items
167- Assert . IsTrue ( BuildSource ( ) . Select ( m => m . Id ) . SequenceEqual ( res . Select ( e => e . Id ) ) ) ;
168- Assert . IsTrue ( BuildSource ( ) . Select ( m => m . Text ) . SequenceEqual ( res . Select ( e => e . Text ) ) ) ;
184+ Assert . IsTrue ( BuildSource ( ) . Select ( m => m . Id ) . SequenceEqual ( res . Collection . Select ( e => e . Id ) ) ) ;
185+ Assert . IsTrue ( BuildSource ( ) . Select ( m => m . Text ) . SequenceEqual ( res . Collection . Select ( e => e . Text ) ) ) ;
169186 }
170187 }
171188}
0 commit comments