Allow line number stability too (#1)

This commit is contained in:
Patrick Stevens
2025-06-16 00:36:50 +01:00
committed by GitHub
parent 62ae417701
commit c9230c24ce
3 changed files with 18 additions and 11 deletions

View File

@@ -17,7 +17,7 @@ module SimpleTest =
let ``Example of a failing test`` () =
expect {
snapshot
"snapshot mismatch! snapshot at filepath.fs:32 (Example of a failing test) was:
"snapshot mismatch! snapshot at filepath.fs:99 (Example of a failing test) was:
- 123
@@ -28,10 +28,17 @@ actual was:
return
Assert
.Throws<Exception>(fun () ->
expectWithMockedFilePath "filepath.fs" {
expectWithMockedFilePath ("filepath.fs", 99) {
snapshot "123"
return 124
}
)
.Message
}
[<Test>]
let ``Basic example`` () =
expect {
snapshot "123"
return 123
}

View File

@@ -32,8 +32,8 @@ module private Text =
/// The builder which powers WoofWare.Expect.
/// </summary>
/// <remarks>You're not expected to construct this explicitly; it's a computation expression, available as <c>Builder.expect</c>.</remarks>
/// <param name="filePathOverride">Override the file paths reported in snapshots, so that your tests can be fully stable even on failure. (You almost certainly don't want to set this.)</param>
type ExpectBuilder (?filePathOverride : string) =
/// <param name="sourceOverride">Override the file path and line numbers reported in snapshots, so that your tests can be fully stable even on failure. (You almost certainly don't want to set this.)</param>
type ExpectBuilder (?sourceOverride : string * int) =
/// Combine two `ExpectState`s. The first one is the "expected" snapshot; the second is the "actual".
member _.Bind (state : ExpectState<unit>, f : unit -> ExpectState<'U>) : ExpectState<'U> =
let actual = f ()
@@ -153,8 +153,8 @@ type ExpectBuilder (?filePathOverride : string) =
if not (JsonElement.DeepEquals (canonicalActual.RootElement, canonicalSnapshot.RootElement)) then
failwithf
"snapshot mismatch! snapshot at %s:%i (%s) was:\n\n%s\n\nactual was:\n\n%s"
(filePathOverride |> Option.defaultValue source.FilePath)
source.LineNumber
(sourceOverride |> Option.map fst |> Option.defaultValue source.FilePath)
(sourceOverride |> Option.map snd |> Option.defaultValue source.LineNumber)
source.MemberName
(canonicalSnapshot.RootElement.ToString () |> Text.predent '-')
(canonicalActual.RootElement.ToString () |> Text.predent '-')
@@ -165,8 +165,8 @@ type ExpectBuilder (?filePathOverride : string) =
if actual <> snapshot then
failwithf
"snapshot mismatch! snapshot at %s:%i (%s) was:\n\n%s\n\nactual was:\n\n%s"
(filePathOverride |> Option.defaultValue source.FilePath)
source.LineNumber
(sourceOverride |> Option.map fst |> Option.defaultValue source.FilePath)
(sourceOverride |> Option.map snd |> Option.defaultValue source.LineNumber)
source.MemberName
(snapshot |> Text.predent '-')
(actual |> Text.predent '+')
@@ -200,4 +200,4 @@ module Builder =
/// You probably don't want to use this; use `expect` instead.
/// The point of the mocked builder is to allow fully predictable testing of the WoofWare.Expect library itself.
/// </remarks>
let expectWithMockedFilePath (path : string) = ExpectBuilder path
let expectWithMockedFilePath (path : string, line : int) = ExpectBuilder ((path, line))

View File

@@ -1,9 +1,9 @@
WoofWare.Expect.Builder inherit obj
WoofWare.Expect.Builder.expect [static property]: [read-only] WoofWare.Expect.ExpectBuilder
WoofWare.Expect.Builder.expectWithMockedFilePath [static method]: string -> WoofWare.Expect.ExpectBuilder
WoofWare.Expect.Builder.expectWithMockedFilePath [static method]: (string, int) -> WoofWare.Expect.ExpectBuilder
WoofWare.Expect.Builder.get_expect [static method]: unit -> WoofWare.Expect.ExpectBuilder
WoofWare.Expect.ExpectBuilder inherit obj
WoofWare.Expect.ExpectBuilder..ctor [constructor]: string option
WoofWare.Expect.ExpectBuilder..ctor [constructor]: (string * int) option
WoofWare.Expect.ExpectBuilder.Bind [method]: (unit WoofWare.Expect.ExpectState, unit -> 'U WoofWare.Expect.ExpectState) -> 'U WoofWare.Expect.ExpectState
WoofWare.Expect.ExpectBuilder.Delay [method]: (unit -> 'T WoofWare.Expect.ExpectState) -> (unit -> 'T WoofWare.Expect.ExpectState)
WoofWare.Expect.ExpectBuilder.Return [method]: 'T -> 'T WoofWare.Expect.ExpectState