Skip to content

Compare every item when WithoutStrictOrdering is on - #6

Open
ivank-ys wants to merge 1 commit into
yevhen:mainfrom
ivank-ys:fix-unordered-equivalency-for-get-only-types
Open

ivank-ys wants to merge 1 commit into
yevhen:mainfrom
ivank-ys:fix-unordered-equivalency-for-get-only-types

Conversation

@ivank-ys

Copy link
Copy Markdown

Summary

With WithoutStrictOrdering(), IsEquivalentTo can compare only the first pair of a collection, so an assertion passes on wrong data. The comparer's ignore-order mode matches items by a key built from their settable simple properties. Items without setters, or items whose simple properties match and only a nested member differs, all get the same key. This PR pairs items by full equivalency instead.

class Money(decimal amount, string currency)
{
    public decimal Amount { get; } = amount;
    public string Currency { get; } = currency;
}

Assert(actual.IsEquivalentTo(expected, c => c.WithoutStrictOrdering()));
actual vs expected Expected main This PR
[USD, EUR] vs [USD, UAH] fail ❌ passes fails
[USD, EUR] vs [EUR, USD] pass ❌ fails passes

Money has no setters, so both items get the key Money. On main the comparer then checks only the first pair: USD with USD in the first case, USD with EUR in the second. EUR and UAH are never compared.

 compare collections without order
-  key = settable simple properties, else ToString()   # type name for get-only items
-  compare first item with each key, count the rest
+  for each actual item
+    take the first unmatched expected item that is fully equivalent
+  compare the items left without a pair and report their member differences

A failure now points at the member that differs, for example [1].Amount: expected "3", got "2", instead of the type name. Strings still compare character by character, and WithStrictOrdering() turns the new matching off again.

Merge Danger

Reversibility: reversible

Blast Radius: user tests

Tests that use WithoutStrictOrdering() and passed with differing items will fail after the upgrade; the changelog says so. Matching is now O(n²) item comparisons, fine for test-sized collections but slower on very large ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant