Skip to content

Commit a1e3d07

Browse files
committed
Added an overload to CheckReference in the PersistenceSpecificationExtensions so you can specify the properties of the reference to compare without creating a custom IEqualityComparer.
1 parent fe85daa commit a1e3d07

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTester.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ public void VerifyTheMappings_returns_instance()
168168
var cat = _spec.CheckProperty(x => x.FirstKitten, _cat.FirstKitten).VerifyTheMappings();
169169
cat.ShouldNotBeNull();
170170
}
171+
172+
[Test]
173+
public void Comparing_reference_should_use_the_specified_property_comparisons()
174+
{
175+
_spec.CheckReference(cat => cat.FirstKitten, _cat.FirstKitten, x => x.Id).VerifyTheMappings();
176+
177+
Assert.Throws<ApplicationException>(() => _spec.CheckReference(cat => cat.FirstKitten, _cat.FirstKitten, x => x.Id, x => x.Name)
178+
.VerifyTheMappings());
179+
}
171180
}
172181

173182
[TestFixture]

src/FluentNHibernate/Testing/PersistenceSpecificationExtensions.cs

Lines changed: 34 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,
@@ -254,5 +268,25 @@ public static PersistenceSpecification<T> CheckEnumerable<T, TItem>(this Persist
254268
{
255269
return spec.CheckList(expression, itemsToAdd, addAction);
256270
}
271+
272+
private class FuncEqualityComparer<T> : EqualityComparer<T>
273+
{
274+
readonly IEnumerable<Func<T, object>> comparisons;
275+
276+
public FuncEqualityComparer(IEnumerable<Func<T, object>> comparisons)
277+
{
278+
this.comparisons = comparisons;
279+
}
280+
281+
public override bool Equals(T x, T y)
282+
{
283+
return comparisons.All(func => object.Equals(func(x), func(y)));
284+
}
285+
286+
public override int GetHashCode(T obj)
287+
{
288+
throw new NotSupportedException();
289+
}
290+
}
257291
}
258292
}

0 commit comments

Comments
 (0)