1
1
using System ;
2
- using System . Collections ;
3
2
using System . Collections . Generic ;
4
3
using System . Linq ;
5
4
using FluentNHibernate . Utils ;
@@ -21,29 +20,7 @@ public override Action<T, Accessor, IEnumerable<TListElement>> ValueSetter
21
20
}
22
21
23
22
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 ) ) ;
47
24
}
48
25
set => _valueSetter = value ;
49
26
}
@@ -52,11 +29,11 @@ public override Action<T, Accessor, IEnumerable<TListElement>> ValueSetter
52
29
53
30
public override void CheckValue ( object target )
54
31
{
55
- var actual = PropertyAccessor . GetValue ( target ) as IEnumerable ;
32
+ var actual = PropertyAccessor . GetValue ( target ) as IEnumerable < TListElement > ;
56
33
AssertGenericListMatches ( actual , Expected ) ;
57
34
}
58
35
59
- void AssertGenericListMatches ( IEnumerable actualEnumerable , IEnumerable < TListElement > expectedEnumerable )
36
+ void AssertGenericListMatches ( IEnumerable < TListElement > actualEnumerable , IEnumerable < TListElement > expectedEnumerable )
60
37
{
61
38
if ( actualEnumerable is null )
62
39
{
@@ -69,20 +46,17 @@ void AssertGenericListMatches(IEnumerable actualEnumerable, IEnumerable<TListEle
69
46
"Actual and expected are not equal (expected was null)." ) ;
70
47
}
71
48
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 ( ) ;
78
50
var expectedList = expectedEnumerable . ToList ( ) ;
79
51
80
52
if ( actualList . Count != expectedList . Count )
81
53
{
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 } )" ) ;
83
55
}
84
56
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 ;
86
60
87
61
for ( var i = 0 ; i < actualList . Count ; i ++ )
88
62
{
@@ -91,12 +65,27 @@ void AssertGenericListMatches(IEnumerable actualEnumerable, IEnumerable<TListEle
91
65
continue ;
92
66
}
93
67
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 } ";
99
69
throw new ApplicationException ( message ) ;
100
70
}
101
71
}
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
+ }
102
91
}
0 commit comments