diff --git a/src/Verify.Tests/Serialization/IgnoreInstanceTests.NonMatchingPredicateKeepsEmptyCollectionsIgnored.verified.txt b/src/Verify.Tests/Serialization/IgnoreInstanceTests.NonMatchingPredicateKeepsEmptyCollectionsIgnored.verified.txt new file mode 100644 index 000000000..dbdf9b6b3 --- /dev/null +++ b/src/Verify.Tests/Serialization/IgnoreInstanceTests.NonMatchingPredicateKeepsEmptyCollectionsIgnored.verified.txt @@ -0,0 +1,5 @@ +{ + NotEmpty: [ + TheValue + ] +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/IgnoreInstanceTests.cs b/src/Verify.Tests/Serialization/IgnoreInstanceTests.cs new file mode 100644 index 000000000..939da0235 --- /dev/null +++ b/src/Verify.Tests/Serialization/IgnoreInstanceTests.cs @@ -0,0 +1,17 @@ +public class IgnoreInstanceTests +{ + [Fact] + public Task NonMatchingPredicateKeepsEmptyCollectionsIgnored() => + Verify( + new + { + Empty = new List(), + NotEmpty = new List + { + "TheValue" + } + }) + // a predicate that never matches is not a decision to keep the value, + // so Empty is still ignored as an empty collection + .IgnoreInstance>(_ => false); +} diff --git a/src/Verify/Serialization/SerializationSettings_ShouldIgnore.cs b/src/Verify/Serialization/SerializationSettings_ShouldIgnore.cs index e95b6be33..f0ea74045 100644 --- a/src/Verify/Serialization/SerializationSettings_ShouldIgnore.cs +++ b/src/Verify/Serialization/SerializationSettings_ShouldIgnore.cs @@ -23,6 +23,8 @@ internal bool TryGetScrubOrIgnore(Type declaringType, Type memberType, string na internal bool TryGetScrubOrIgnoreByInstance(object value, [NotNullWhen(true)] out ScrubOrIgnore? scrubOrIgnore) { var memberType = value.GetType(); + // no predicate matching is not a decision to keep the value, so + // the empty collection check still applies if (GetShouldIgnoreInstance(memberType, out var funcs)) { foreach (var func in funcs) @@ -34,9 +36,6 @@ internal bool TryGetScrubOrIgnoreByInstance(object value, [NotNullWhen(true)] ou return true; } } - - scrubOrIgnore = null; - return false; } if (ignoreEmptyCollections && diff --git a/src/todo.md b/src/todo.md index e40e69c54..5b209e87b 100644 --- a/src/todo.md +++ b/src/todo.md @@ -44,7 +44,7 @@ All six resolved 2026-08-16 (five fixed here; the inline item resolved as by-des - [ ] **`MemberConverter` has no exact-type precedence.** `Verify/Serialization/VerifierSettings_MemberConverter.cs:13-25` — first registered entry with `IsAssignableFrom` wins, so a base-interface converter registered earlier permanently shadows a more specific one. Contrast `TryGetScrubOrIgnoreByMemberOfType`, which checks the exact declaring type first. -- [ ] **Registering an `IgnoreInstance` predicate disables empty-collection ignoring for that type.** +- [x] **Registering an `IgnoreInstance` predicate disables empty-collection ignoring for that type.** `Verify/Serialization/SerializationSettings_ShouldIgnore.cs:26-40` — when predicates exist but none match, the early `return false` skips the `ignoreEmptyCollections` check at 42-47. An empty `List` starts appearing as `[]` merely because an unrelated predicate was registered. - [x] **Combinations name cache collapses distinct keys.**