Skip to content

Extends Verify to allow comparison of html files via AngleSharp.

License

Notifications You must be signed in to change notification settings

VerifyTests/Verify.AngleSharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verify.AngleSharp

Discussions Build status NuGet Status

Extends Verify with Html verification utilities via AngleSharp.

See Milestones for release notes.

NuGet package

https://nuget.org/packages/Verify.AngleSharp/

Comparer Usage

Extends Verify to allow comparison of htm and html files via AngleSharp.

Initialize

Call VerifyAngleSharpDiffing.Initialize() once at assembly load time.

Initialize takes an optional Action<IDiffingStrategyCollection> to control settings at a global level:

[ModuleInitializer]
public static void Init() =>
    VerifyAngleSharpDiffing.Initialize();

snippet source | anchor

Verify html

Given an existing verified file:

<!DOCTYPE html>
<html>
<body>
  <h1>My First Heading</h1>
  <p>My first paragraph.</p>
</body>
</html>

snippet source | anchor

And a test:

[Test]
public Task Sample()
{
    var html =
        """
        <!DOCTYPE html>
        <html>
          <body>
            <h1>My First Heading</h1>
            <p>My first paragraph.</p>
          </body>
        </html>
        """;
    return Verify(html, "html");
}

snippet source | anchor

Note that the input html differs from the verified html, but not in a semantically significant way. Hence this test will pass.

Diff results

If the comparison fails, the resulting differences will be included in the test result displayed to the user.

For example if, in the above html, <h1>My First Heading</h1> changes to <h1>First Heading</h1> then the following will be printed in the test results:

Comparer result:
 * Node Diff
   Path: h1(0) > #text(0)
   Received: First Heading
   Verified: My First Heading

Test level settings

Settings can also be controlled for a specific test.

var settings = new VerifySettings();
settings.AngleSharpDiffingSettings(
    action =>
    {
        static FilterDecision SpanFilter(
            in ComparisonSource source,
            FilterDecision decision)
        {
            if (source.Node.NodeName == "SPAN")
            {
                return FilterDecision.Exclude;
            }

            return decision;
        }

        var options = action.AddDefaultOptions();
        options.AddFilter(SpanFilter);
    });

snippet source | anchor

Global settings

VerifyAngleSharpDiffing.Initialize(
    action =>
    {
        static FilterDecision SpanFilter(
            in ComparisonSource source,
            FilterDecision decision)
        {
            if (source.Node.NodeName == "SPAN")
            {
                return FilterDecision.Exclude;
            }

            return decision;
        }

        var options = action.AddDefaultOptions();
        options.AddFilter(SpanFilter);
    });

snippet source | anchor

Pretty Print

Html can be pretty printed.

[Test]
public Task PrettyPrintHtml()
{
    var html = """
               <!DOCTYPE html>
               <html><body><h1>My First Heading</h1>
               <p>My first paragraph.</p></body></html>
               """;
    return Verify(html, "html")
        .PrettyPrintHtml();
}

snippet source | anchor

Results in

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <h1>My First Heading</h1>
    <p>My first paragraph.</p>
  </body>
</html>

snippet source | anchor

To apply this to all html files use HtmlPrettyPrint.All();

Manipulate Html

Nodes can be manipulated as part of the pretty print:

[Test]
public Task PrettyPrintHtmlWithNodeManipulation()
{
    var html = """
               <!DOCTYPE html>
               <html>
                 <body>
                   <h1>My First Heading</h1>
                   <p>My first paragraph.</p>
                 </body>
               </html>
               """;
    return Verify(html, "html")
        .PrettyPrintHtml(
            nodes =>
            {
                foreach (var node in nodes.QuerySelectorAll("h1"))
                {
                    node.Remove();
                }
            });
}

snippet source | anchor

Results in

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <p>My first paragraph.</p>
  </body>
</html>

snippet source | anchor

AngleSharp helpers

ScrubEmptyDivs

[Test]
public Task ScrubEmptyDivs()
{
    var html = """
               <!DOCTYPE html>
               <html>
                 <body>
                   <div>My First Heading</div>
                   <div></div>
                 </body>
               </html>
               """;
    return Verify(html, "html")
        .PrettyPrintHtml(nodes => nodes.ScrubEmptyDivs());
}

snippet source | anchor

Results in:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div>My First Heading</div>
  </body>
</html>

snippet source | anchor

ScrubAttributes

Removal

[Test]
public Task ScrubAttributes()
{
    var html = """
               <!DOCTYPE html>
               <html>
                 <body>
                   <div id='a'>My First Heading</div>
                 </body>
               </html>
               """;
    return Verify(html, "html")
        .PrettyPrintHtml(nodes => nodes.ScrubAttributes("id"));
}

snippet source | anchor

Results in:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div>My First Heading</div>
  </body>
</html>

snippet source | anchor

Replace Value

[Test]
public Task ScrubAttributeWithNewValue()
{
    var html = """
               <!DOCTYPE html>
               <html>
                 <body>
                   <div id='a'>My First Heading</div>
                 </body>
               </html>
               """;
    return Verify(html, "html")
        .PrettyPrintHtml(
            nodes => nodes.ScrubAttributes(x =>
            {
                if (x.Name == "id")
                {
                    return "new value";
                }

                return null;
            }));
}

snippet source | anchor

Results in:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div id="new value">My First Heading</div>
  </body>
</html>

snippet source | anchor

About

Extends Verify to allow comparison of html files via AngleSharp.

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Contributors 4

  •  
  •  
  •  
  •