-
Notifications
You must be signed in to change notification settings - Fork 733
Add the ability to exclude non-browsable members from equivalency tests #1827
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
Add the ability to exclude non-browsable members from equivalency tests #1827
Conversation
Pull Request Test Coverage Report for Build 1992359664Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
|
Thanks for dogfooding the feature 👍 My first concern was that we normally just "let the the expectation drive the comparison", e.g. when using an anonymous object with a subset of members. But it seems to me that we have approached this issue from the wrong angle. This example var subject = new { Browsable = 1 };
var expected = new NonBrowable { Browsable = 1, NonBrowsable = 2 };
subject.Should().BeEquivalentTo(expected, opt => opt.ExcludingNonBrowsableMembers());
private class NonBrowable
{
public int Browsable { get; set; }
[EditorBrowsable(EditorBrowsableState.Never)]
public int NonBrowsable { get; set; }
}fails with In short, it fails on the missing The example above works if we instead implement this feature using an |
|
That's a clever approach :-) I hadn't thought of this possibility at all, but it makes a lot of sense. I'll try this alternate implementation against our test suites, and I anticipate cherry-picking your commit into this branch :-) |
|
Hmm, as written it doesn't actually do everything that's needed, because it only excludes members if they're marked non-browsable in the With this implementation, tests Is there a way to resolve this? Or is it fundamentally impossible to have a selection rule guide itself based on aspects of the subject? |
|
I've done a more direct fix. We can backtrack if there is a more elegant solution. |
Src/FluentAssertions/Equivalency/Matching/MustMatchByNameRule.cs
Outdated
Show resolved
Hide resolved
Src/FluentAssertions/Equivalency/Steps/StructuralEqualityEquivalencyStep.cs
Outdated
Show resolved
Hide resolved
Tests/FluentAssertions.Equivalency.Specs/SelectionRulesSpecs.cs
Outdated
Show resolved
Hide resolved
Tests/FluentAssertions.Equivalency.Specs/SelectionRulesSpecs.cs
Outdated
Show resolved
Hide resolved
Tests/FluentAssertions.Equivalency.Specs/SelectionRulesSpecs.cs
Outdated
Show resolved
Hide resolved
Src/FluentAssertions/Equivalency/Steps/StructuralEqualityEquivalencyStep.cs
Outdated
Show resolved
Hide resolved
Okay, this makes sense, but, how, then, do I achieve my required use case, which is specifically that non-browsable members of the subject should be skipped? |
If the expectation doesn't contain that property or it is excluded, it should not even look for a property on the subject. |
|
The expectation does contain the property. It is marked non-browsable on the subject only. |
Ah, I didn't get that scenario. Maybe by having an additional option called |
|
Mm, so then ... |
No,
Sorry, I don't think I get that scenario. If you exclude a member from the expectation, than it's completely ignored. |
|
How about we keep the functionality the way it's currently implemented, but rename it from |
|
@jnyrup @dennisdoomen How about my suggestion of simply changing the name so that this feature isn't implicitly expected to follow the same rules as other |
|
It's kind of a niche feature (which nobody asked for in over 10 years), so either we do it correctly (using two options like |
|
That's fair. But, with |
|
|
|
Ah, okay, and the default logic doesn't care if the expectation has extra members? So then, with what you describe, those two methods are basically doing exactly the same thing, just one on the subject and one on the expectation. The wording |
It does. The expectation defines the members that are expected.
I chose But maybe |
|
In previous discussion, the But, looking at the implementation, it doesn't seem to work that way. The various Furthermore, it appears that if you explicitly include any member, then the default inclusion of public properties & fields is disabled. So, it isn't possible to do something like: The current logic sees the ...because the I think if we want this to work consistently and in an easy-to-understand way, then this needs to be reworked so that I'm inclined to instead make an |
c05137a to
14babeb
Compare
|
I have rebased the changes here, collapsing the commits that were part of #1807 into a single commit, and the edits made in this PR into a single commit. The character of the changes changed sufficiently that I didn't see any value in preserving the edit history that went down a different path. With this rebase, this PR can be merged and will reintroduce the changes reverted in #1842 as a base onto which the new approach separating the subject from the expectation is applied. |
I guess in the current state of the code base, there's no other way to make this work. |
dennisdoomen
left a comment
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.
We're almost there ;-)
In #1807, changes were made to allow non-browsable members to be excluded from equivalency tests. But, these changes ended up being reverted because they affected processing of both subject & expectation, but include/exclude functions are supposed to apply only to the expectation. In addition, the original changes only worked properly when the subject & expectation were of the same data type. In my actual usage, the subject and expectation are different data types and only one of them marks the member as non-browsable. This wasn't factored in in the original implementation, for which the unit tests exclusively test the case where the subject and expectation are the same data type. The changes in #1807 got reverted since they were in an incomplete/uncertain state and were blocking a release.
This PR makes another attempt at adding this feature. In addition to the tests added in #1807, the PR adds unit tests that comprehensively cover the behaviour when the subject and expectation are different types and only one of them marks a member as non-browsable.
This PR then also provides an implementation that allows excluding non-browsable members from the expectation using an
Excludingmethod, and from the subject using a separateIgnoremethod.