Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

AlexMountainBN
Copy link

@AlexMountainBN AlexMountainBN commented Sep 17, 2021

IMPORTANT

  • The code complies with the Coding Guidelines for C#.
  • The changes are covered by a new or existing set of unit tests which follow the Arrange-Act-Assert syntax such as is used in this example.
  • If the contribution adds a feature or fixes a bug, please update the release notes, which are published on the website.
  • If the contribution changes the public API the changes needs to be included by running AcceptApiChanges.ps1/AcceptApiChanges.sh.
  • If the contribution affects the documentation, please include your changes in this pull request so the documentation will appear on the website.

Implemented the two needed methods for checking the counting on XElement (#1681)

@AlexMountainBN AlexMountainBN changed the title Implemented BeSingle and HaveCount for XElement Implemented BeSingle and HaveCount for XElement #1681 Sep 17, 2021
Copy link
Member

@dennisdoomen dennisdoomen left a comment

Choose a reason for hiding this comment

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

What we've added for XDocument should also be added to XElement.

@AlexMountainBN AlexMountainBN marked this pull request as draft November 1, 2021 13:25
@AlexMountainBN
Copy link
Author

Thanks for the feedback. Hopefully I understood them correctly. Now everything should be fine.

@AlexMountainBN AlexMountainBN marked this pull request as ready for review November 1, 2021 14:58
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<XDocumentAssertions> HaveSingleElement(XName expected, string because = "", params object[] becauseArgs)
Copy link
Member

Choose a reason for hiding this comment

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

Should return a AndWhichConstraint<XDocumentAssertions, XElement> like HaveElement.

Copy link
Member

Choose a reason for hiding this comment

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

And use the Which that AndWhichConstraint exposes in your test

}

Guard.ThrowIfArgumentIsNull(expected, nameof(expected),
"Cannot assert the document has an element count if the element name is <null>*");
Copy link
Member

Choose a reason for hiding this comment

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

Should the trailing * be a .?

"Expected {context:subject} to have root element with child {0}{reason}, but it has no root element.",
expected.ToString());

var xElements = Subject.Root.Elements(expected);
Copy link
Member

Choose a reason for hiding this comment

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

If HaveElementCount is called inside an AssertionScope the assertion above above will not fail immediately, so Subject.Root can be null here.
See e.g. XElementAssertions.HaveValue for an example of how to handle this.

</parent>");

// Act
Action act = () => document.Should().HaveSingleElement("child");
Copy link
Member

Choose a reason for hiding this comment

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

You could also validate that because and becauseArgs are properly passed along in this test.
See e.g. XDocumentAssertionSpecs.When_the_expected_element_is_null_it_fails

* Adding new overloads to all `GreaterOrEqualTo` and `LessOrEqualTo` assertions, adding the word `Than` - [#1673](https://github.com/fluentassertions/fluentassertions/pull/1673)
* `BeAsync()` and `NotBeAsync()` are now also available on `MethodInfoSelectorAssertions` - [#1700](https://github.com/fluentassertions/fluentassertions/pull/1700)

* Added `HaveSingleElement` and `HaveElementCount` for `XDocument` - [#1681](https://github.com/fluentassertions/fluentassertions/pull/1684)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Added `HaveSingleElement` and `HaveElementCount` for `XDocument` - [#1681](https://github.com/fluentassertions/fluentassertions/pull/1684)
* Added `HaveSingleElement` and `HaveElementCount` for `XDocument` - [#1684](https://github.com/fluentassertions/fluentassertions/pull/1684)

Copy link
Member

@dennisdoomen dennisdoomen left a comment

Choose a reason for hiding this comment

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

I suggested some improvements to the test names to avoid violating rule 4 of https://www.continuousimprover.com/2021/10/laws-test-driven-development.html

/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<XDocumentAssertions> HaveSingleElement(XName expected, string because = "", params object[] becauseArgs)
Copy link
Member

Choose a reason for hiding this comment

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

And use the Which that AndWhichConstraint exposes in your test

}

/// <summary>
/// Asserts that the <see cref="XDocument.Root"/> element of the current <see cref="XDocument"/> has a the specified <paramref name="count"/> of
Copy link
Member

Choose a reason for hiding this comment

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

There's a typo around "has a the specified"

public AndConstraint<XDocumentAssertions> HaveElementCount(XName expected, int count, string because = "",
params object[] becauseArgs)
{
if (Subject is null)
Copy link
Member

Choose a reason for hiding this comment

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

The subject being null is actually something that could happen in a failing test, so I think you should use the normal ForCondition logic.

#region HaveSingleElement

[Fact]
public void When_asserting_document_has_a_single_child_element_and_it_does_it_should_succeed()
Copy link
Member

Choose a reason for hiding this comment

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

🔧 I suggest to keep the name more functional and concise: A_single_expected_child_element_with_the_specified_name_should_be_accepted

}

[Fact]
public void When_asserting_document_has_single_child_element_but_it_does_have_two_it_should_fail()
Copy link
Member

Choose a reason for hiding this comment

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

🔧 Suggestion: Multiple_child_elements_with_the_specified_name_should_fail_the_test

}

[Fact]
public void When_asserting_a_null_xDocument_to_have_a_single_element_it_should_fail()
Copy link
Member

Choose a reason for hiding this comment

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

🔧 Suggestion: Cannot_ensure_a_single_element_if_the_document_is_null

@dennisdoomen
Copy link
Member

@ABergBN do you still intend to finish this PR?

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.

4 participants