Added BeOneOf for object comparisons with custom comparer support#2111
Added BeOneOf for object comparisons with custom comparer support#2111xmarshal wants to merge 0 commit intofluentassertions:developfrom
BeOneOf for object comparisons with custom comparer support#2111Conversation
|
Hmm.. I think this should be discussed in an issue first (aka api review process ;) ) |
|
Api proposal added #2112 |
Pull Request Test Coverage Report for Build 3968029095Details
💛 - Coveralls |
|
@xmarshal Do you mind covering your latest changes with tests? 😉 |
| } | ||
|
|
||
| /// <summary> | ||
| /// Asserts that a <typeparamref name="TExpectation"/> equals another <typeparamref name="TExpectation"/> using its <see cref="object.Equals(object)" /> implementation. |
There was a problem hiding this comment.
🔧 You're not using object.Equals here. You're using IEqualityComparer<T>.
| .BecauseOf(because, becauseArgs) | ||
| .ForCondition(Subject is TExpectation subject && comparer.Equals(subject, expected)) | ||
| .WithDefaultIdentifier(Identifier) | ||
| .FailWith("Expected {context} to be {0}{reason}, but found {1}.", expected, |
There was a problem hiding this comment.
🤔 I would also expect some reference to the comparer, should I not?
There was a problem hiding this comment.
You mean something like including comparer.GetType()?
We do something similar when using an IEqualityComparer<T> in equivalency assertions.
https://github.com/fluentassertions/fluentassertions/pull/1284/files#diff-f060184f7198367f57bfcf2bbfc8ce3ddc5ce5db1949c93e376d6d3c780c53b8R37
| } | ||
| } | ||
|
|
||
| internal class SomeClass |
There was a problem hiding this comment.
🔧 We prefer to keep the test classes as simple and specific for the individual test cases as possible, and also next to them (in the same grouping class, e.g. BeOneOf. See also
docs/_pages/basicassertions.md
Outdated
| To assert that an object is equal to one of the provided objects with custom equality comparer, you can use | ||
|
|
||
| ```csharp | ||
| theObject.Should().BeOneOf<TObj>(new[] { obj1, obj2, obj3 } , new ObjEqualityCompmarer()); |
There was a problem hiding this comment.
🤔 And what about the new Be and NotBe overloads?
docs/_pages/releases.md
Outdated
| ## Unreleased | ||
|
|
||
| ### What's new | ||
| * Added `BeOneOf` for object comparisons with custom comparer support -[#2111](https://github.com/fluentassertions/fluentassertions/pull/2111) |
There was a problem hiding this comment.
🤔 And what about the new Be and NotBe overloads?
| [Fact] | ||
| public void When_a_value_is_not_one_of_the_specified_values_and_equality_comparer_passed_it_should_throw() | ||
| { | ||
| // Arrange | ||
| var value = new SomeClass(3); | ||
|
|
||
| // Act | ||
| Action act = () => value.Should().BeOneOf(new[] { new SomeClass(4), new SomeClass(5) }, new SomeClassEqualityComparer()); | ||
|
|
||
| // Assert | ||
| act | ||
| .Should().Throw<XunitException>() | ||
| .WithMessage("Expected value to be one of {SomeClass(4), SomeClass(5)}, but found SomeClass(3)."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void When_a_value_is_not_one_of_the_specified_values_and_equality_comparer_passed_it_should_throw_with_descriptive_message() | ||
| { | ||
| // Arrange | ||
| var value = new SomeClass(3); | ||
|
|
||
| // Act | ||
| Action act = () => value.Should().BeOneOf(new[] { new SomeClass(4), new SomeClass(5) }, "because those are the valid values"); | ||
|
|
||
| // Assert | ||
| act | ||
| .Should().Throw<XunitException>() | ||
| .WithMessage("Expected value to be one of {SomeClass(4), SomeClass(5)} because those are the valid values, but found SomeClass(3)."); | ||
| } |
There was a problem hiding this comment.
You can merge these two tests, there's no reason to have two tests here as they're testing a single concept.
I have written myself a note to clean this up.
The same comment applies for the two tests below.
|
@xmarshal Do you plan continuing this or anything blocking you? |
|
@xmarshal checking in one more time to see if you'd like to drive this PR to completion. |
Added BeOneOf for object comparisons with custom comparer support
provided api for #2112