Skip to content

Commit 4f88b7a

Browse files
authored
Refactor FluentNHibernate.Testing.Values List & ReferenceBag to avoid using non-generic IEnumerable (#687)
+semver:patch
1 parent e189537 commit 4f88b7a

File tree

2 files changed

+36
-48
lines changed

2 files changed

+36
-48
lines changed
Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
43
using System.Linq;
54
using 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
}

src/FluentNHibernate/Testing/Values/ReferenceBag.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ public class ReferenceBag<T, TListElement>(Accessor property, IEnumerable<TListE
1111
{
1212
public override void CheckValue(object target)
1313
{
14-
var actual = PropertyAccessor.GetValue(target) as IEnumerable;
14+
var actual = PropertyAccessor.GetValue(target) as IEnumerable<TListElement>;
1515
AssertGenericListMatches(actual, Expected);
1616
}
1717

18-
void AssertGenericListMatches(IEnumerable actualEnumerable, IEnumerable<TListElement> expectedEnumerable)
18+
void AssertGenericListMatches(IEnumerable<TListElement> actualEnumerable, IEnumerable<TListElement> expectedEnumerable)
1919
{
2020
if (actualEnumerable is null)
2121
{
@@ -28,24 +28,23 @@ void AssertGenericListMatches(IEnumerable actualEnumerable, IEnumerable<TListEle
2828
"Actual and expected are not equal (expected was null).");
2929
}
3030

31-
var actualList = actualEnumerable.Cast<object>().ToList();
31+
var actualList = actualEnumerable.ToList();
3232

3333
var expectedList = expectedEnumerable.ToList();
3434

3535
if (actualList.Count != expectedList.Count)
3636
{
37-
throw new ApplicationException(String.Format("Actual count ({0}) does not equal expected count ({1})", actualList.Count, expectedList.Count));
37+
throw new ApplicationException($"Actual count ({actualList.Count}) does not equal expected count ({expectedList.Count})");
3838
}
3939

40-
var equalsFunc = (EntityEqualityComparer is not null)
41-
? new Func<object, object, bool>((a, b) => EntityEqualityComparer.Equals(a, b))
42-
: new Func<object, object, bool>(Equals);
43-
40+
Func<object, object, bool> equalsFunc = EntityEqualityComparer is not null
41+
? EntityEqualityComparer.Equals
42+
: Equals;
4443

4544
var result = actualList.FirstOrDefault(item => actualList.Count(x => equalsFunc(item, x)) != expectedList.Count(x => equalsFunc(item, x)));
4645
if (result is not null)
4746
{
48-
throw new ApplicationException(String.Format("Actual count of item {0} ({1}) does not equal expected item count ({2})",result, actualList.Count(x => x == result), expectedList.Count(x => (object)x == result)));
47+
throw new ApplicationException($"Actual count of item {result} ({actualList.Count(x => ReferenceEquals(x, result))}) does not equal expected item count ({expectedList.Count(x => ReferenceEquals(x, result))})");
4948
}
5049
}
5150
}

0 commit comments

Comments
 (0)