这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 31 additions & 61 deletions MLS.Agent.Tests/Markdown/CodeBlockAnnotationExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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]
Expand Down Expand Up @@ -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()}");
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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();

Expand Down Expand Up @@ -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}");
}
Expand All @@ -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);
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand Down Expand Up @@ -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;
}
}
}