-
Notifications
You must be signed in to change notification settings - Fork 729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IStructuralEquatable support #3646
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3f8923f
Force types which implement IEnumerable, IEquatable, and IStructuralC…
stevenaw 4d9e50f
Add a few more tests
stevenaw edd9e70
Add 2 more test collection (non-immutable)
stevenaw dd1ba42
Try force another build
stevenaw e780aba
Target lower version of S.C.I. so that deps will play nice
stevenaw 58e63ad
Merge branch 'master' into 3447-structural-equality
stevenaw bf315c4
IStructuralEquatable takes priority over IEnumerable using NUnit Equa…
stevenaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/NUnitFramework/framework/Constraints/Comparers/StructuralComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// *********************************************************************** | ||
// Copyright (c) 2020 Charlie Poole, Rob Prouse | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining | ||
// a copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to | ||
// permit persons to whom the Software is furnished to do so, subject to | ||
// the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// *********************************************************************** | ||
|
||
#if !NET35 | ||
using System.Collections; | ||
|
||
namespace NUnit.Framework.Constraints.Comparers | ||
{ | ||
/// <summary> | ||
/// Comparator for two types related by <see cref="IStructuralEquatable"/>. | ||
/// </summary> | ||
class StructuralComparer : IChainComparer | ||
{ | ||
private readonly NUnitEqualityComparer _equalityComparer; | ||
|
||
internal StructuralComparer(NUnitEqualityComparer equalityComparer) | ||
{ | ||
_equalityComparer = equalityComparer; | ||
} | ||
|
||
public bool? Equal(object x, object y, ref Tolerance tolerance, ComparisonState state) | ||
{ | ||
if (_equalityComparer.CompareAsCollection && state.TopLevelComparison) | ||
return null; | ||
|
||
if (x is IStructuralEquatable xEquatable && y is IStructuralEquatable yEquatable) | ||
{ | ||
// We can't pass tolerance as a ref so we pass by value and reassign later | ||
var equalityComparison = new NUnitEqualityComparison(_equalityComparer, tolerance); | ||
|
||
// Check both directions in case they are different implementations, and only one is aware of the other. | ||
// Like how ImmutableArray<int> is structurally equatable to int[] | ||
// but int[] is NOT structurally equatable to ImmutableArray<int> | ||
var xResult = xEquatable.Equals(y, equalityComparison); | ||
var yResult = yEquatable.Equals(x, equalityComparison); | ||
|
||
// Keep all the refs up to date | ||
tolerance = equalityComparison.Tolerance; | ||
|
||
return xResult || yResult; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private sealed class NUnitEqualityComparison : IEqualityComparer | ||
{ | ||
private readonly NUnitEqualityComparer _comparer; | ||
|
||
private Tolerance _tolerance; | ||
public Tolerance Tolerance { get { return _tolerance; } } | ||
|
||
public NUnitEqualityComparison(NUnitEqualityComparer comparer, Tolerance tolerance) | ||
{ | ||
_comparer = comparer; | ||
_tolerance = tolerance; | ||
} | ||
|
||
public new bool Equals(object x, object y) | ||
{ | ||
return _comparer.AreEqual(x, y, ref _tolerance); | ||
} | ||
|
||
public int GetHashCode(object obj) | ||
{ | ||
// TODO: Better hashcode generation, likely based on the corresponding comparer to ensure types which can be | ||
// compared with each other end up in the same bucket | ||
return 0; | ||
} | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
|
||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" /> | ||
<PackageReference Include="System.Collections.Immutable" Version="1.5.0" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I started to hit issues on the tests when building with Cake with version >= 1.6 on netcoreapp21 (similar to what was encountered here: Azure/azure-functions-vs-build-sdk#353). VS tests run fine with the latest version of this (1.7.1). |
||
</ItemGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' == 'net40'"> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IStructuralEquatable
is not available to NET4 on NuGet viaSystem.Runtime
. Instead, it is just a part of the framework