8000 Implemented ExcludingMembersNamed by weitzhandler · Pull Request #183 · AwesomeAssertions/AwesomeAssertions · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implemented ExcludingMembersNamed #183

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

weitzhandler
Copy link
@weitzhandler weitzhandler commented Jun 11, 2025

Fixes #182

IMPORTANT

  • If the PR touches the public API, the changes have been approved in a separate issue with the "api-approved" label.
  • The code complies with the Coding Guidelines for C#.
  • The changes are covered by unit tests which follow the Arrange-Act-Assert syntax and the naming conventions such as is used in these tests.
  • If the PR adds a feature or fixes a bug, please update the release notes with a functional description that explains what the change means to consumers of this library, which are published on the website.
  • If the PR changes the public API the changes needs to be included by running AcceptApiChanges.ps1 or AcceptApiChanges.sh.
  • If the PR affects the documentation, please include your changes in this pull request so the documentation will appear on the website.
    • Please also run ./build.sh --target spellcheck or .\build.ps1 --target spellcheck before pushing and check the good outcome

@IT-VBFK
Copy link
Contributor
IT-VBFK commented Jun 11, 2025

Thank you for your contribution! 🎉

Out of curiosity: The same as proposed as in FA?

8000

Copy link
Contributor
@IT-VBFK IT-VBFK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work

Just one small suggestion from my side

@IT-VBFK
Copy link
Contributor
IT-VBFK commented Jun 11, 2025

Ah yes.. just forgot: also please take care of the release notes, api snapshots and (if applicable) the docs for this change.

@cbersch
Copy link
Contributor
cbersch commented Jun 11, 2025

Ah yes.. just forgot: also please take care of the release notes

Add

## Unreleased

### Improvements

followed by the changes to docs/_pages/releases.md.

the docs for this change.

I would add an example here:

You can also take a different approach and explicitly tell Awesome Assertions which members to include. You can directly specify a property expression or use a predicate that acts on the provided `ISubjectInfo`.

/// <summary>
/// Selection rule that removes a particular member from the structural comparison if its name matches the specified criteria.
/// </summary>
internal class ExcludeMemberByNameSelectionRule : IMemberSelectionRule
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW @IT-VBFK and @cbersch, I think we could benefit from having an abstract MemberSelectionRule base implementation.

@weitzhandler weitzhandler force-pushed the exclude-member-names branch from e9af95a to 4bc0f8d Compare June 12, 2025 01:25
@weitzhandler weitzhandler requested review from cbersch and IT-VBFK June 12, 2025 01:27
@weitzhandler weitzhandler force-pushed the exclude-member-names branch from 4bc0f8d to fc2c7a5 Compare June 12, 2025 01:38
@weitzhandler weitzhandler changed the title Implemented ExcludingMemberNames Implemented ExcludingMembersNamed Jun 12, 2025
@weitzhandler weitzhandler force-pushed the exclude-member-names branch from fc2c7a5 to 4c1e44e Compare June 12, 2025 01:41
@weitzhandler weitzhandler force-pushed the exclude-member-names branch from 4c1e44e to 27c9a7d Compare June 15, 2025 06:20
@weitzhandler weitzhandler force-pushed the exclude-member-names branch from 27c9a7d to 56580c8 Compare June 15, 2025 06:35
@IT-VBFK
Copy link
Contributor
IT-VBFK commented Jun 21, 2025

The discussion about null and empty elements is the only thing what's missing, correct?

Copy link

Copy link
Contributor
@IT-VBFK IT-VBFK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few notes which I had missed previously

.ExcludingMembersNamed("Name"));

// Assert
act.Should().NotThrow();
Copy link
Contributor
@IT-VBFK IT-VBFK Jun 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually don't assert that something which shouldn't throw to NotThrow, because when it fails it fails anyways

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this test would become

[Fact]
public void A_member_excluded_by_name_is_ignored()
{
    // Arrange
    var subject = new
    {
        Id = 1,
        Name = "John",
        Age = 13
    };

    var customer = new
    {
        Id = 1,
        Name = "Jack",
        Age = 13
    };

    // Act / Assert
    subject.Should().BeEquivalentTo(customer, options => options
        .ExcludingMembersNamed("Name"));
}

But this test isn't necessary at all, because it is a duplicate of When_excluding_member_names_it_should_pass_if_only_the_excluded_members_are_different.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IT-VBFK We actually have tests which use Should().NotThrow(). I guess those are older ones. Since we agree here, I'll change those tests to match our preferences.

// Assert
act.Should().Throw<ArgumentException>()
.WithParameterName("memberNames")
.WithMessage("At least one member name must be specified.\nParameter name: memberNames");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just assert the shape of the message will do, like:

Suggested change
.WithMessage("At least one member name must be specified.\nParameter name: memberNames");
.WithMessage("*one member*must be specified*memberNames*");

// Assert
act.Should().Throw<ArgumentException>()
.WithParameterName("memberNames")
.WithMessage("Collection contains a null or empty value\nParameter name: memberNames");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

@@ -130,6 +130,12 @@ orderDto.Should().BeEquivalentTo(order, options =>
options.Excluding(o => o.Products[1].Status));
```

You can also exclude all members with of a specific name:
Copy link
Contributor

Choose a reas F438 on for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You can also exclude all members with of a specific name:
You can also exclude all members with of a specific name anywhere in the object graph:

.ExcludingMembersNamed("Name"));

// Assert
act.Should().NotThrow();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this test would become

[Fact]
public void A_member_excluded_by_name_is_ignored()
{
    // Arrange
    var subject = new
    {
        Id = 1,
        Name = "John",
        Age = 13
    };

    var customer = new
    {
        Id = 1,
        Name = "Jack",
        Age = 13
    };

    // Act / Assert
    subject.Should().BeEquivalentTo(customer, options => options
        .ExcludingMembersNamed("Name"));
}

But this test isn't necessary at all, because it is a duplicate of When_excluding_member_names_it_should_pass_if_only_the_excluded_members_are_different.

}

[Fact]
public void When_an_empty_or_null_member_name_is_passed_in_memeber_names_parameter_it_should_throw()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: _member_names_ (same in other method names)

Field2 = "ipsum"
};

// Act
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Act / Assert
class1.Should().BeEquivalentTo(class2,
    opts => opts.ExcludingMembersNamed("Field3", "Property1"));

## Unreleased

### What's new
* Add new assertions for strings: `[Not]BeParsableInto` - [#185](https://github.com/AwesomeAssertions/AwesomeAssertions/pull/185)

### Improvements
* Add `ExcludingMembersNamed` to `BeEquivalentTo` which enables excluding members with specified name - [#183](https://github.com/AwesomeAssertions/AwesomeAssertions/pull/183)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move that to What's new, sorry for the wrong proposal.

@IT-VBFK
Copy link
Contributor
IT-VBFK commented Jun 29, 2025

@weitzhandler @cbersch Are there any undiscussed things left, or can we move on?

@cbersch
Copy link
Contributor
cbersch commented Jun 29, 2025

@weitzhandler @cbersch Are there any undiscussed things left, or can we move on?

For me only the unresolved conversations, with minor changes in tests.

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.

ExcludingMembersNamed(params string[] memberNames)
4 participants
0