11using System ;
2- using System . Collections ;
32using System . Collections . Generic ;
43using System . Linq ;
54using FluentNHibernate . Utils ;
@@ -21,29 +20,7 @@ public override Action<T, Accessor, IEnumerable<TListElement>> ValueSetter
2120 }
2221
2322 return ( target , propertyAccessor , value ) =>
24- {
25- object collection ;
26-
27- // sorry guys - create an instance of the collection type because we can't rely
28- // on the user to pass in the correct collection type (especially if they're using
29- // an interface). I've tried to create the common ones, but I'm sure this won't be
30- // infallible.
31- if ( propertyAccessor . PropertyType . IsAssignableFrom ( typeof ( ISet < TListElement > ) ) )
32- {
33- collection = new HashSet < TListElement > ( Expected . ToList ( ) ) ;
34- }
35- else if ( propertyAccessor . PropertyType . IsArray )
36- {
37- collection = Array . CreateInstance ( typeof ( TListElement ) , Expected . Count ( ) ) ;
38- Array . Copy ( ( Array ) Expected , ( Array ) collection , Expected . Count ( ) ) ;
39- }
40- else
41- {
42- collection = new List < TListElement > ( Expected ) ;
43- }
44-
45- propertyAccessor . SetValue ( target , collection ) ;
46- } ;
23+ propertyAccessor . SetValue ( target , CreateCollection ( propertyAccessor . PropertyType ) ) ;
4724 }
4825 set => _valueSetter = value ;
4926 }
@@ -52,11 +29,11 @@ public override Action<T, Accessor, IEnumerable<TListElement>> ValueSetter
5229
5330 public override void CheckValue ( object target )
5431 {
55- var actual = PropertyAccessor . GetValue ( target ) as IEnumerable ;
32+ var actual = PropertyAccessor . GetValue ( target ) as IEnumerable < TListElement > ;
5633 AssertGenericListMatches ( actual , Expected ) ;
5734 }
5835
59- void AssertGenericListMatches ( IEnumerable actualEnumerable , IEnumerable < TListElement > expectedEnumerable )
36+ void AssertGenericListMatches ( IEnumerable < TListElement > actualEnumerable , IEnumerable < TListElement > expectedEnumerable )
6037 {
6138 if ( actualEnumerable is null )
6239 {
@@ -69,20 +46,17 @@ void AssertGenericListMatches(IEnumerable actualEnumerable, IEnumerable<TListEle
6946 "Actual and expected are not equal (expected was null)." ) ;
7047 }
7148
72- List < object > actualList = new List < object > ( ) ;
73- foreach ( var item in actualEnumerable )
74- {
75- actualList . Add ( item ) ;
76- }
77-
49+ var actualList = actualEnumerable . ToList ( ) ;
7850 var expectedList = expectedEnumerable . ToList ( ) ;
7951
8052 if ( actualList . Count != expectedList . Count )
8153 {
82- throw new ApplicationException ( String . Format ( "Actual count ({0 }) does not equal expected count ({1})" , actualList . Count , expectedList . Count ) ) ;
54+ throw new ApplicationException ( $ "Actual count ({ actualList . Count } ) does not equal expected count ({ expectedList . Count } )" ) ;
8355 }
8456
85- var equalsFunc = ( EntityEqualityComparer is not null ) ? ( ( a , b ) => EntityEqualityComparer . Equals ( a , b ) ) : new Func < object , object , bool > ( Equals ) ;
57+ Func < object , object , bool > equalsFunc = EntityEqualityComparer is not null
58+ ? EntityEqualityComparer . Equals
59+ : Equals ;
8660
8761 for ( var i = 0 ; i < actualList . Count ; i ++ )
8862 {
@@ -91,12 +65,27 @@ void AssertGenericListMatches(IEnumerable actualEnumerable, IEnumerable<TListEle
9165 continue ;
9266 }
9367
94- var message = String . Format ( "Expected '{0}' but got '{1}' at position {2}" ,
95- expectedList [ i ] ,
96- actualList [ i ] ,
97- i ) ;
98-
68+ var message = $ "Expected '{ expectedList [ i ] } ' but got '{ actualList [ i ] } ' at position { i } ";
9969 throw new ApplicationException ( message ) ;
10070 }
10171 }
72+
73+ IEnumerable < TListElement > CreateCollection ( Type type )
74+ {
75+ // sorry guys - create an instance of the collection type because we can't rely
76+ // on the user to pass in the correct collection type (especially if they're using
77+ // an interface). I've tried to create the common ones, but I'm sure this won't be
78+ // infallible.
79+ if ( type . IsAssignableFrom ( typeof ( ISet < TListElement > ) ) )
80+ {
81+ return new HashSet < TListElement > ( Expected ) ;
82+ }
83+
84+ if ( type . IsArray )
85+ {
86+ return Expected . ToArray ( ) ;
87+ }
88+
89+ return Expected . ToList ( ) ;
90+ }
10291}
0 commit comments