diff --git a/MLS.Agent.Tests/Markdown/CodeBlockAnnotationExtensionTests.cs b/MLS.Agent.Tests/Markdown/CodeBlockAnnotationExtensionTests.cs index 89cc801ac..3b2105b6a 100644 --- a/MLS.Agent.Tests/Markdown/CodeBlockAnnotationExtensionTests.cs +++ b/MLS.Agent.Tests/Markdown/CodeBlockAnnotationExtensionTests.cs @@ -187,11 +187,8 @@ public async Task Sets_the_trydotnet_package_attribute_using_the_passed_project_ ```"; var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF(); - - var htmlDocument = new HtmlDocument(); - htmlDocument.LoadHtml(html); - var output = htmlDocument.DocumentNode - .SelectSingleNode("//pre/code").Attributes["data-trydotnet-package"]; + var node = GetSingleHtmlNode(html, "//pre/code"); + var output = node.Attributes["data-trydotnet-package"]; var fullProjectPath = directoryAccessor.GetFullyQualifiedPath(new RelativeFilePath(package)); output.Value.Should().Be(fullProjectPath.FullName); @@ -248,11 +245,8 @@ public async Task Sets_the_trydotnet_package_attribute_using_the_passed_package_ ```"; var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF(); - - var htmlDocument = new HtmlDocument(); - htmlDocument.LoadHtml(html); - var output = htmlDocument.DocumentNode - .SelectSingleNode("//pre/code").Attributes["data-trydotnet-package"]; + var node = GetSingleHtmlNode(html, "//pre/code"); + var output = node.Attributes["data-trydotnet-package"]; output.Value.Should().Be(package); } @@ -277,13 +271,10 @@ public async Task When_both_package_and_project_are_specified_then_package_wins( ```"; var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF(); + var node = GetSingleHtmlNode(html, "//pre/code"); + var output = node.Attributes["data-trydotnet-package"]; - var htmlDocument = new HtmlDocument(); - htmlDocument.LoadHtml(html); - var node = htmlDocument.DocumentNode - .SelectSingleNode("//pre/code").Attributes["data-trydotnet-package"]; - - node.Value.Should().Be(package); + output.Value.Should().Be(package); } [Fact] @@ -318,12 +309,8 @@ static void MyProgram(string[] args) ```"; var pipeline = new MarkdownPipelineBuilder().UseCodeBlockAnnotations(directoryAccessor,await Default.PackageRegistry.ValueAsync()).Build(); var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF(); - - var htmlDocument = new HtmlDocument(); - htmlDocument.LoadHtml(html); - var output = htmlDocument.DocumentNode - .SelectSingleNode("//pre/code") - .InnerText.Trim(); + var node = GetSingleHtmlNode(html, "//pre/code"); + var output = node.InnerText.Trim(); output.Should().BeEquivalentTo($"{regionCode.HtmlEncode()}"); } @@ -348,11 +335,8 @@ public async Task Sets_the_trydotnet_filename_using_the_filename_specified_in_th ```"; var pipeline = new MarkdownPipelineBuilder().UseCodeBlockAnnotations(directoryAccessor,await Default.PackageRegistry.ValueAsync()).Build(); var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF(); - - var htmlDocument = new HtmlDocument(); - htmlDocument.LoadHtml(html); - var output = htmlDocument.DocumentNode - .SelectSingleNode("//pre/code").Attributes["data-trydotnet-file-name"]; + var node = GetSingleHtmlNode(html, "//pre/code"); + var output = node.Attributes["data-trydotnet-file-name"]; output.Value.Should().Be(directoryAccessor.GetFullyQualifiedPath(new RelativeFilePath(filename)).FullName); } @@ -378,11 +362,8 @@ public async Task Sets_the_trydotnet_filename_using_the_filename_specified_in_th ```"; var pipeline = new MarkdownPipelineBuilder().UseCodeBlockAnnotations(directoryAccessor,await Default.PackageRegistry.ValueAsync()).Build(); var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF(); - - var htmlDocument = new HtmlDocument(); - htmlDocument.LoadHtml(html); - var output = htmlDocument.DocumentNode - .SelectSingleNode("//pre/code").Attributes["data-trydotnet-file-name"]; + var node = GetSingleHtmlNode(html, "//pre/code"); + var output = node.Attributes["data-trydotnet-file-name"]; output.Value.Should().Be(directoryAccessor.GetFullyQualifiedPath(new RelativeFilePath(destinationFile)).FullName); } @@ -407,11 +388,8 @@ public async Task Sets_the_trydotnet_region_using_the_region_passed_in_the_markd ```"; var pipeline = new MarkdownPipelineBuilder().UseCodeBlockAnnotations(directoryAccessor,await Default.PackageRegistry.ValueAsync()).Build(); var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF(); - - var htmlDocument = new HtmlDocument(); - htmlDocument.LoadHtml(html); - var output = htmlDocument.DocumentNode - .SelectSingleNode("//pre/code").Attributes["data-trydotnet-region"]; + var node = GetSingleHtmlNode(html, "//pre/code"); + var output = node.Attributes["data-trydotnet-region"]; output.Value.Should().Be(region); } @@ -432,10 +410,7 @@ public async Task If_the_specified_region_does_not_exist_then_an_error_message_i ```"; var pipeline = new MarkdownPipelineBuilder().UseCodeBlockAnnotations(directoryAccessor,await Default.PackageRegistry.ValueAsync()).Build(); var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF(); - - var htmlDocument = new HtmlDocument(); - htmlDocument.LoadHtml(html); - var node = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='notification is-danger']"); + var node = GetSingleHtmlNode(html, "//div[@class='notification is-danger']"); var expected = $"Region \"{region}\" not found in file {directoryAccessor.GetFullyQualifiedPath(new RelativeFilePath("./Program.cs"))}".HtmlEncode().ToString(); @@ -463,10 +438,7 @@ public async Task If_the_specified_region_exists_more_than_once_then_an_error_is ```"; var pipeline = new MarkdownPipelineBuilder().UseCodeBlockAnnotations(directoryAccessor,await Default.PackageRegistry.ValueAsync()).Build(); var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF(); - - var htmlDocument = new HtmlDocument(); - htmlDocument.LoadHtml(html); - var pre = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='notification is-danger']"); + var pre = GetSingleHtmlNode(html, "//div[@class='notification is-danger']"); pre.InnerHtml.Should().Contain($"Multiple regions found: {region}"); } @@ -489,12 +461,8 @@ public async Task Sets_the_trydotnet_session_using_the_session_passed_in_the_mar .Build(); var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF(); - - var htmlDocument = new HtmlDocument(); - htmlDocument.LoadHtml(html); - var output = htmlDocument.DocumentNode - .SelectSingleNode("//pre/code") - .Attributes["data-trydotnet-session-id"]; + var node = GetSingleHtmlNode(html, "//pre/code"); + var output = node.Attributes["data-trydotnet-session-id"]; output.Value.Should().Be(session); } @@ -516,12 +484,8 @@ public async Task Sets_the_trydotnet_session_to_a_default_value_when_a_session_i .Build(); var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF(); - - var htmlDocument = new HtmlDocument(); - htmlDocument.LoadHtml(html); - var output = htmlDocument.DocumentNode - .SelectSingleNode("//pre/code") - .Attributes["data-trydotnet-session-id"]; + var node = GetSingleHtmlNode(html, "//pre/code"); + var output = node.Attributes["data-trydotnet-session-id"]; output.Value.Should().StartWith("Run"); } @@ -547,10 +511,7 @@ public async Task Sets_a_diagnostic_if_the_package_cannot_be_found() ```"; var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF(); - - var htmlDocument = new HtmlDocument(); - htmlDocument.LoadHtml(html); - var node = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='notification is-danger']"); + var node = GetSingleHtmlNode(html, "//div[@class='notification is-danger']"); node.InnerHtml.Should().Contain($"Package named "{package}" not found"); } @@ -583,5 +544,14 @@ public async Task Arguments_are_forwarded_to_the_users_program_entry_point() value.Should().Be("--region the-region --source-file Program.cs -- one two \"and three\"".HtmlAttributeEncode().ToString()); } + + private static HtmlNode GetSingleHtmlNode(string html, string nodeName) + { + var htmlDocument = new HtmlDocument(); + htmlDocument.LoadHtml(html); + var node = htmlDocument?.DocumentNode?.SelectSingleNode(nodeName); + Assert.True(node != null, $"Unexpected value for `{nameof(node)}`. Html was:\r\n{html}"); + return node; + } } }