Add syntax for exceptions (#19)

This commit is contained in:
Patrick Stevens
2025-07-06 22:30:02 +01:00
committed by GitHub
parent 75899d5668
commit ca74c4816b
8 changed files with 104 additions and 11 deletions

View File

@@ -17,20 +17,21 @@ type CallerInfo =
type private SnapshotValue =
| Json of expected : string
| Formatted of expected : string
| ThrowsException of expected : string
type private CompletedSnapshotValue<'T> =
| Json of expected : string * JsonSerializerOptions * JsonDocumentOptions
| Formatted of expected : string * format : ('T -> string)
| Formatted of expected : string * format : ((unit -> 'T) -> string)
/// The state accumulated by the `expect` builder. You should never find yourself interacting with this type.
type ExpectState<'T> =
private
{
Formatter : ('T -> string) option
Formatter : ((unit -> 'T) -> string) option
JsonSerialiserOptions : JsonSerializerOptions option
JsonDocOptions : JsonDocumentOptions option
Snapshot : (SnapshotValue * CallerInfo) option
Actual : 'T option
Actual : (unit -> 'T) option
}
/// The state accumulated by the `expect` builder. You should never find yourself interacting with this type.
@@ -39,7 +40,7 @@ type internal CompletedSnapshotGeneric<'T> =
{
SnapshotValue : CompletedSnapshotValue<'T>
Caller : CallerInfo
Actual : 'T
Actual : unit -> 'T
}
[<RequireQualifiedAccess>]
@@ -68,11 +69,22 @@ module internal CompletedSnapshotGeneric =
| SnapshotValue.Formatted expected ->
let formatter =
match state.Formatter with
| None -> fun x -> x.ToString ()
| None -> fun x -> x().ToString ()
| Some f -> f
CompletedSnapshotValue.Formatted (expected, formatter)
| SnapshotValue.ThrowsException expected ->
CompletedSnapshotValue.Formatted (
expected,
fun x ->
try
x () |> ignore
"<no exception raised>"
with e ->
e.GetType().FullName + ": " + e.Message
)
{
SnapshotValue = snapshot
Caller = source
@@ -84,7 +96,7 @@ module internal CompletedSnapshotGeneric =
let internal replacement (s : CompletedSnapshotGeneric<'T>) =
match s.SnapshotValue with
| CompletedSnapshotValue.Json (_existing, options, _) ->
JsonSerializer.Serialize (s.Actual, options)
JsonSerializer.Serialize (s.Actual (), options)
|> JsonDocument.Parse
|> _.RootElement
|> _.ToString()
@@ -104,7 +116,7 @@ module internal CompletedSnapshotGeneric =
None
let canonicalActual =
JsonSerializer.Serialize (state.Actual, jsonSerOptions) |> JsonDocument.Parse
JsonSerializer.Serialize (state.Actual (), jsonSerOptions) |> JsonDocument.Parse
match canonicalSnapshot with
| None -> Some ("[JSON failed to parse:] " + snapshot, canonicalActual.RootElement.ToString ())