Add a specific exception (#3)

This commit is contained in:
Patrick Stevens
2025-06-16 10:28:28 +01:00
committed by GitHub
parent f9ac7c2239
commit 39370d5235
3 changed files with 16 additions and 3 deletions

View File

@@ -27,7 +27,7 @@ actual was:
return
Assert
.Throws<Exception>(fun () ->
.Throws<ExpectException>(fun () ->
expectWithMockedFilePath ("filepath.fs", 99) {
snapshot "123"
return 124

View File

@@ -15,6 +15,9 @@ type private SnapshotValue =
| BareString of string
| Json of string
/// An exception indicating that a value failed to match its snapshot.
exception ExpectException of Message : string
/// A dummy type which is here to provide better error messages when you supply
/// the `snapshot` keyword multiple times.
type YouHaveSuppliedMultipleSnapshots = private | NonConstructible
@@ -160,25 +163,29 @@ type ExpectBuilder (?sourceOverride : string * int) =
JsonSerializer.Serialize (actual, options) |> JsonDocument.Parse
if not (JsonElement.DeepEquals (canonicalActual.RootElement, canonicalSnapshot.RootElement)) then
failwithf
sprintf
"snapshot mismatch! snapshot at %s:%i (%s) was:\n\n%s\n\nactual was:\n\n%s"
(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 '-')
|> ExpectException
|> raise
| SnapshotValue.BareString snapshot ->
let actual = actual.ToString ()
if actual <> snapshot then
failwithf
sprintf
"snapshot mismatch! snapshot at %s:%i (%s) was:\n\n%s\n\nactual was:\n\n%s"
(sourceOverride |> Option.map fst |> Option.defaultValue source.FilePath)
(sourceOverride |> Option.map snd |> Option.defaultValue source.LineNumber)
source.MemberName
(snapshot |> Text.predent '-')
(actual |> Text.predent '+')
|> ExpectException
|> raise
| None, _ -> failwith "Must specify snapshot"
| _, None -> failwith "Must specify actual value with 'return'"

View File

@@ -11,6 +11,12 @@ WoofWare.Expect.ExpectBuilder.Return [method]: unit -> 'T WoofWare.Expect.Expect
WoofWare.Expect.ExpectBuilder.Run [method]: (unit -> 'T WoofWare.Expect.ExpectState) -> unit
WoofWare.Expect.ExpectBuilder.Snapshot [method]: (unit WoofWare.Expect.ExpectState, string, string option, int option, string option) -> WoofWare.Expect.YouHaveSuppliedMultipleSnapshots WoofWare.Expect.ExpectState
WoofWare.Expect.ExpectBuilder.SnapshotJson [method]: (unit WoofWare.Expect.ExpectState, string, string option, int option, string option) -> WoofWare.Expect.YouHaveSuppliedMultipleSnapshots WoofWare.Expect.ExpectState
WoofWare.Expect.ExpectException inherit System.Exception, implements System.Collections.IStructuralEquatable
WoofWare.Expect.ExpectException..ctor [constructor]: string
WoofWare.Expect.ExpectException..ctor [constructor]: unit
WoofWare.Expect.ExpectException.Equals [method]: (System.Exception, System.Collections.IEqualityComparer) -> bool
WoofWare.Expect.ExpectException.Equals [method]: System.Exception -> bool
WoofWare.Expect.ExpectException.Message [property]: [read-only] string
WoofWare.Expect.ExpectState`1 inherit obj, implements 'T WoofWare.Expect.ExpectState System.IEquatable, System.Collections.IStructuralEquatable, 'T WoofWare.Expect.ExpectState System.IComparable, System.IComparable, System.Collections.IStructuralComparable
WoofWare.Expect.ExpectState`1.Equals [method]: ('T WoofWare.Expect.ExpectState, System.Collections.IEqualityComparer) -> bool
WoofWare.Expect.YouHaveSuppliedMultipleSnapshots inherit obj, implements WoofWare.Expect.YouHaveSuppliedMultipleSnapshots System.IEquatable, System.Collections.IStructuralEquatable, WoofWare.Expect.YouHaveSuppliedMultipleSnapshots System.IComparable, System.IComparable, System.Collections.IStructuralComparable