Skip to content
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 feature to check if an XElement or XAttribute is absent within the XDocument #2589

Open
skukshaus opened this issue Feb 25, 2024 · 5 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation enhancement

Comments

@skukshaus
Copy link

skukshaus commented Feb 25, 2024

Background and motivation

Currently, there is an implementation to check whenever an Attribute or Element is within the Document: https://fluentassertions.com/xml/
However, sometimes it is necessary to check if those attributes are absent. Currently, there is no possibility to do this. Maybe it would be possible to use the HaveElement() Method and catch the Exception, but this feels dirty.

I think it could also be a good idea, to add a constraint if the attribute/element does not have a specific value

API Proposal

public class XElementAssertions
{
    public AndConstraint<XElementAssertions> NotHaveAttribute(string unexpectedAttribute, string because = "", params object[] becauseArgs);
    public AndConstraint<XElementAssertions> NotHaveAttribute(string unexpectedAttribute, string unexpectedValue, string because = "", params object[] becauseArgs);

    public AndWhichConstraint<XElementAssertions, XElement> NotHaveElement(string unexpectedElement, string because = "", params object[] becauseArgs);
    public AndWhichConstraint<XElementAssertions, XElement> NotHaveElement(string unexpectedElement, object unexpectedValue, string because = "", params object[] becauseArgs);
}

API Usage

var element = XElement.Parse("<user><forename>john</forename></user>");
element.Should().NotHaveElement("surname");


var element = XElement.Parse("<user><forename>john</forename><surname>doe</surname></user>");
element.Should().NotHaveElement("surname", "smith");



var element = XElement.Parse("<user forename='john' />");
element.Should().NotHaveAttribute("surname");


var element = XElement.Parse("<user forename='john' surname='doe' />");
element.Should().NotHaveAttribute("surname", "smith");

Alternative Designs

No response

Risks

No response

Are you willing to help with a proof-of-concept (as PR in that or a separate repo) first and as pull-request later on?

Yes, please assign this issue to me.

@skukshaus skukshaus added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Feb 25, 2024
@dennisdoomen
Copy link
Member

Sure, makes sense. What do you think @jnyrup ?

@dennisdoomen dennisdoomen changed the title [API Proposal]: Add feature, to check if an XElement or XAttribute is absent within the XDocument Add feature to check if an XElement or XAttribute is absent within the XDocument Mar 15, 2024
@jnyrup
Copy link
Member

jnyrup commented Mar 24, 2024

It certainly makes sense to have more negated assertions for XML types 👍

You suggest adding an overload of NotHaveElement having an object unexpectedValue, but we don't currently have an affirmative HaveElement(string expectedElement, object expectedValue).
So I'm not sure we should add that overload.

We currently have HaveElement(string expectedName) on

  • XDocumentAssertions
  • XElementAssertions
  • XmlElementAssertions

and HaveAttribute(string expectedName, string expectedValue) on

  • XElementAssertions
  • XmlElementAssertions

In order to have a consistent API, it would be be nice to add the proposed methods on all relevant classes.

@skukshaus
Copy link
Author

skukshaus commented Mar 26, 2024

Hey @jnyrup and @dennisdoomen

Thanks for the reply, should I implement the changes according to your guidelines and create another PR?
I mean, including the overloads for the other use cases as well.

(I mean, I really need this extension because I have a dirty workaround for my problem (I've added a Must<XElementAssertions>() method because Should<XElementAssertions>() is already there).)

BG Sergej

@dennisdoomen
Copy link
Member

Thanks for the reply, should I implement the changes according to your guidelines and create another PR?

Everything that is in this proposal should be delivered as a consistent set of changes. And to prevent contributors from abandoning the work after the first PR, we need to insist on contributors to provide a PR that covers everything that is needed to maintain that consistency. Hope you understand.

@dennisdoomen
Copy link
Member

It took me a couple of seconds to realize that element.Should().NotHaveElement("surname", "smith"); meant that element should not have an element with a specific value. I guess that also means that if element has element surname with a different value, it is okay? If so, then I think we should call it NotHaveElementWithValue.

In that case, we need:

  • XDocumentAssertions.HaveElementWithValue

  • XDocumentAssertions.NotHaveElementWithValue

  • XDocumentAssertions.NotHaveElement

  • XElementAssertions.HaveElementWithValue

  • XElementAssertions.NotHaveElementWithValue

  • XElementAssertions.NotHaveElement

  • XElementAssertions.HaveAttributeWithValue

  • XElementAssertions.NotHaveAttributeWithValue

  • XElementAssertions.NotHaveAttribute

To be clear, you can only add the NotHaveElementWithValue if you also include HaveElementWithValue

XmlElementAssertions are out of scope as far as I'm concerned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation enhancement
Projects
None yet
Development

No branches or pull requests

3 participants