Skip to content

Commit e7cf411

Browse files
committed
Merge remote-tracking branch 'RyanHauert/TestingImprovements'
2 parents 2984c8c + 7665282 commit e7cf411

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTester.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ public void should_not_be_equal_without_the_equality_comparer()
145145
_spec.CheckList(x => x.AllKittens, _cat.AllKittens).VerifyTheMappings());
146146
}
147147

148+
[Test]
149+
public void Comparing_objects_in_two_lists_should_use_the_specified_comparisons()
150+
{
151+
_spec.CheckList(x => x.AllKittens, _cat.AllKittens, kitten => kitten.Id).VerifyTheMappings();
152+
153+
// Should fail because the names don't match.
154+
Assert.Throws<ApplicationException>(() => _spec.CheckList(x => x.AllKittens, _cat.AllKittens, kitten => kitten.Id, kitten => kitten.Name)
155+
.VerifyTheMappings());
156+
}
157+
148158
[Test]
149159
public void Can_test_enumerable()
150160
{
@@ -168,6 +178,16 @@ public void VerifyTheMappings_returns_instance()
168178
var cat = _spec.CheckProperty(x => x.FirstKitten, _cat.FirstKitten).VerifyTheMappings();
169179
cat.ShouldNotBeNull();
170180
}
181+
182+
[Test]
183+
public void Comparing_reference_should_use_the_specified_property_comparisons()
184+
{
185+
_spec.CheckReference(cat => cat.FirstKitten, _cat.FirstKitten, x => x.Id).VerifyTheMappings();
186+
187+
// Should fail because the names don't match.
188+
Assert.Throws<ApplicationException>(() => _spec.CheckReference(cat => cat.FirstKitten, _cat.FirstKitten, x => x.Id, x => x.Name)
189+
.VerifyTheMappings());
190+
}
171191
}
172192

173193
[TestFixture]

src/FluentNHibernate/Testing/PersistenceSpecificationExtensions.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Linq.Expressions;
45
using FluentNHibernate.Testing.Values;
56
using FluentNHibernate.Utils;
@@ -81,6 +82,19 @@ public static PersistenceSpecification<T> CheckReference<T>(this PersistenceSpec
8182
return spec.RegisterCheckedProperty(new ReferenceProperty<T, object>(property, propertyValue), propertyComparer);
8283
}
8384

85+
public static PersistenceSpecification<T> CheckReference<T, TReference>(this PersistenceSpecification<T> spec,
86+
Expression<Func<T, object>> expression,
87+
TReference propertyValue,
88+
params Func<TReference, object>[] propertiesToCompare)
89+
{
90+
// Because of the params keyword, the compiler will select this overload
91+
// instead of the one above, even when no funcs are supplied in the method call.
92+
if (propertiesToCompare == null || propertiesToCompare.Length == 0)
93+
return spec.CheckReference(expression, propertyValue, (IEqualityComparer)null);
94+
95+
return spec.CheckReference(expression, propertyValue, new FuncEqualityComparer<TReference>(propertiesToCompare));
96+
}
97+
8498
public static PersistenceSpecification<T> CheckReference<T, TProperty>(this PersistenceSpecification<T> spec,
8599
Expression<Func<T, TProperty>> expression,
86100
TProperty propertyValue,
@@ -121,6 +135,19 @@ public static PersistenceSpecification<T> CheckList<T, TListElement>(this Persis
121135
return spec.RegisterCheckedProperty(new ReferenceList<T, TListElement>(property, propertyValue), elementComparer);
122136
}
123137

138+
public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
139+
Expression<Func<T, IEnumerable<TListElement>>> expression,
140+
IEnumerable<TListElement> propertyValue,
141+
params Func<TListElement, object>[] propertiesToCompare)
142+
{
143+
// Because of the params keyword, the compiler can select this overload
144+
// instead of the one above, even when no funcs are supplied in the method call.
145+
if (propertiesToCompare == null || propertiesToCompare.Length == 0)
146+
return spec.CheckList(expression, propertyValue, (IEqualityComparer)null);
147+
148+
return spec.CheckList(expression, propertyValue, new FuncEqualityComparer<TListElement>(propertiesToCompare));
149+
}
150+
124151
public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
125152
Expression<Func<T, IEnumerable<TListElement>>> expression,
126153
IEnumerable<TListElement> propertyValue,
@@ -254,5 +281,25 @@ public static PersistenceSpecification<T> CheckEnumerable<T, TItem>(this Persist
254281
{
255282
return spec.CheckList(expression, itemsToAdd, addAction);
256283
}
284+
285+
private class FuncEqualityComparer<T> : EqualityComparer<T>
286+
{
287+
readonly IEnumerable<Func<T, object>> comparisons;
288+
289+
public FuncEqualityComparer(IEnumerable<Func<T, object>> comparisons)
290+
{
291+
this.comparisons = comparisons;
292+
}
293+
294+
public override bool Equals(T x, T y)
295+
{
296+
return comparisons.All(func => object.Equals(func(x), func(y)));
297+
}
298+
299+
public override int GetHashCode(T obj)
300+
{
301+
throw new NotSupportedException();
302+
}
303+
}
257304
}
258305
}

0 commit comments

Comments
 (0)