Skip to content

Commit 05abc97

Browse files
committed
move to xunit v3 and unquote
1 parent 4f7b670 commit 05abc97

File tree

7 files changed

+67
-11
lines changed

7 files changed

+67
-11
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace authoring
2+
3+
open System.Diagnostics
4+
open System.Linq
5+
open Elastic.Markdown.Diagnostics
6+
open FsUnitTyped
7+
open Swensen.Unquote
8+
9+
[<AutoOpen>]
10+
module DiagnosticsCollectorAssertions =
11+
12+
[<DebuggerStepThrough>]
13+
let hasNoErrors (actual: GenerateResult) =
14+
test <@ actual.Context.Collector.Errors = 0 @>
15+
16+
[<DebuggerStepThrough>]
17+
let hasError (expected: string) (actual: GenerateResult) =
18+
actual.Context.Collector.Errors |> shouldBeGreaterThan 0
19+
let errorDiagnostics = actual.Context.Collector.Diagnostics
20+
.Where(fun d -> d.Severity = Severity.Error)
21+
.ToArray()
22+
|> List.ofArray
23+
test <@ errorDiagnostics.FirstOrDefault().Message.Contains(expected) @>

tests/authoring/Framework/HtmlAssertions.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ actual: {actual}
8787
sw.ToString()
8888

8989
[<DebuggerStepThrough>]
90-
let convertsToHtml ([<LanguageInjection("html")>]expected: string) (actual: TestResult) =
90+
let convertsToHtml ([<LanguageInjection("html")>]expected: string) (actual: GenerateResult) =
9191
let diffs =
9292
DiffBuilder
9393
.Compare(actual.Html)

tests/authoring/Framework/Setup.fs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ open System.IO
55
open System.IO.Abstractions.TestingHelpers
66
open System.Threading.Tasks
77
open Elastic.Markdown
8-
open Elastic.Markdown.Diagnostics
98
open Elastic.Markdown.IO
109
open JetBrains.Annotations
1110

@@ -34,15 +33,15 @@ type Setup =
3433

3534
fileSystem.AddFile(Path.Combine(root.FullName, "docset.yml"), MockFileData(yaml.ToString()));
3635

37-
static let Generate ([<LanguageInjection("markdown")>]m: string) : Task<TestResult> =
36+
static let Generate ([<LanguageInjection("markdown")>]m: string) : Task<GenerateResult> =
3837

3938
let d = dict [ ("docs/index.md", MockFileData(m)) ]
4039
let opts = MockFileSystemOptions(CurrentDirectory=Paths.Root.FullName)
4140
let fileSystem = MockFileSystem(d, opts)
4241

4342
GenerateDocSetYaml (fileSystem, None)
4443

45-
let collector = DiagnosticsCollector([]);
44+
let collector = TestDiagnosticsCollector();
4645
let context = BuildContext(fileSystem, Collector=collector)
4746
let set = DocumentationSet(context);
4847
let file =

tests/authoring/Framework/TestValues.fs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,34 @@ open System.IO.Abstractions
55
open Elastic.Markdown.Diagnostics
66
open Elastic.Markdown.IO
77
open Markdig.Syntax
8+
open Xunit
89

9-
type TestResult = {
10+
11+
type TestDiagnosticsOutput() =
12+
13+
interface IDiagnosticsOutput with
14+
member this.Write diagnostic =
15+
let line = match diagnostic.Line with | NonNullV l -> l | _ -> 0
16+
match TestContext.Current.TestOutputHelper with
17+
| NonNull output ->
18+
match diagnostic.Severity with
19+
| Severity.Error ->
20+
output.WriteLine($"Error: {diagnostic.Message} ({diagnostic.File}:{line})")
21+
| _ ->
22+
output.WriteLine($"Warn : {diagnostic.Message} ({diagnostic.File}:{line})")
23+
| _ -> ()
24+
25+
26+
type TestDiagnosticsCollector() =
27+
inherit DiagnosticsCollector([TestDiagnosticsOutput()])
28+
29+
let diagnostics = System.Collections.Generic.List<Diagnostic>()
30+
31+
member _.Diagnostics = diagnostics.AsReadOnly()
32+
33+
override this.HandleItem diagnostic = diagnostics.Add(diagnostic);
34+
35+
type GenerateResult = {
1036
Document: MarkdownDocument
1137
Html: string
1238
Context: MarkdownTestContext
@@ -15,7 +41,7 @@ type TestResult = {
1541
and MarkdownTestContext =
1642
{
1743
File: MarkdownFile
18-
Collector: DiagnosticsCollector
44+
Collector: TestDiagnosticsCollector
1945
Set: DocumentationSet
2046
ReadFileSystem: IFileSystem
2147
WriteFileSystem: IFileSystem

tests/authoring/Inline/InlineAnchors.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ this is *regular* text and this $$$is-an-inline-anchor$$$ and this continues to
1414
markdown |> convertsToHtml """
1515
<p>this is <em>regular</em> text and this <a id="is-an-inline-anchor"></a> and this continues to be regular text</p>
1616
"""
17+
[<Fact>]
18+
let ``has no errors`` () = markdown |> hasNoErrors

tests/authoring/Inline/Substitutions.fs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open Xunit
44
open authoring
55

66
type ``read sub from yaml frontmatter`` () =
7-
static let markdown = Setup.Markdown """---
7+
static let markdown = Setup.Document """---
88
sub:
99
hello-world: "Hello World!"
1010
---
@@ -21,7 +21,7 @@ not a comment</p>
2121

2222

2323
type ``requires valid syntax and key to be found`` () =
24-
static let markdown = Setup.Markdown """---
24+
static let markdown = Setup.Document """---
2525
sub:
2626
hello-world: "Hello World!"
2727
---
@@ -33,10 +33,15 @@ not a {{valid-key}}
3333
not a {substitution}
3434
"""
3535

36+
[<Fact>]
37+
let ``emits an error when sub key is not found`` () =
38+
markdown |> hasError "key {valid-key} is undefined"
39+
3640
[<Fact>]
3741
let ``validate HTML: leaves non subs alone`` () =
3842
markdown |> convertsToHtml """
3943
<p>The following should be subbed: Hello World!<br>
4044
not a comment</br>
4145
not a {{valid-key}}<br>
42-
not a {substitution}</p> """
46+
not a {substitution}</p>
47+
"""

tests/authoring/authoring.fsproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
1111
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0"/>
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
13-
<PackageReference Include="xunit" Version="2.9.2"/>
14-
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"/>
13+
<PackageReference Include="xunit.v3" Version="1.0.1"/>
1514
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="21.0.29"/>
1615
<PackageReference Include="FsUnit.xUnit" Version="7.0.1"/>
1716
<PackageReference Include="AngleSharp.Diffing" Version="1.0.0"/>
1817
<PackageReference Include="DiffPlex" Version="1.7.2"/>
18+
<PackageReference Include="Unquote" Version="7.0.1" />
1919
</ItemGroup>
2020

2121
<ItemGroup>
@@ -26,6 +26,7 @@
2626
<Compile Include="Framework\TestValues.fs" />
2727
<Compile Include="Framework\Setup.fs" />
2828
<Compile Include="Framework\HtmlAssertions.fs" />
29+
<Compile Include="Framework\ErrorCollectorAssertions.fs" />
2930
<Compile Include="Inline\Substitutions.fs" />
3031
</ItemGroup>
3132

0 commit comments

Comments
 (0)