Skip to content

Make :ignore also ingore existance of an attribute altogether #48

@RiRiSharp

Description

@RiRiSharp

New Feature Proposal

Description

Right now, we can ignore a class attribute when comparing two HTML strings. However, if we set up a test with multiple cases, where one test does not have a class at all, it does not get ignored and instead we get an error:

[Theory]
[InlineData("<div class:ignore></div>", "<div class=\"hello\"></div>")]
[InlineData("<div class:ignore></div>", "<div></div>")] // Will fail
[InlineData("<div required:ignore></div>", "<div required></div>")]
[InlineData("<div required:ignore></div>", "<div required=\"\"></div>")]
[InlineData("<div required:ignore></div>", "<div></div>")] // Will fail
public void ShouldHaveNoErrors(string controlHtml, string testHtml)
{
    var diffs = DiffBuilder.Compare(controlHtml).WithTest(testHtml).Build();
    Assert.Empty(diffs);
}

Background

When I'm testing blazor code I also test for null values. However, when the Razor engine sees attr=null it removes the attribute altogether. Although there are thousands of workarounds to come up with, I think it could be a cute little feature.
We have talked about this a bit in bUnit-dev/bUnit#1751

Then it seemed that because there are also boolean attributes, it might be a nifty feature in how it is set up right now. However, now when I look at it again, I feel that the :ignore suffix should also ignore the attribute not being there altogether. The way attributes work in HTML is that they are false by default, adding them makes them true. If we want to ignore a boolean attribute, we don't care whether it is true or false. The way it's working right now, the :ignore suffix forces the boolean value to be true. I.e. the :ignore more works as an :exists.

Specification

I have taken a quick look at the HTML standard and there are two big categories and some sub categories in attributes:

  • Key-Value attributes
  • Boolean attributes
    Then there are some more niche attributes:
  • Enumerated attributes
  • CORS attributes
  • Event handler attributes
  • Aria attributes

But I think, from an XML standpoint, these are all key-value attributes

Therefore, I propose that :ignore also ignores an attribute not being there altogether. As an alternative (to prevent breaking changes) we can add an extra :ignoreExistance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions