mirror of
https://github.com/Smaug123/WoofWare.Expect
synced 2025-10-06 13:08:39 +00:00
Allow overriding snapshot formatting (#9)
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
namespace WoofWare.Expect.Test
|
||||
|
||||
open System.Collections.Generic
|
||||
open System.Text.Json
|
||||
open System.Text.Json.Serialization
|
||||
open WoofWare.Expect
|
||||
open NUnit.Framework
|
||||
|
||||
@@ -41,3 +44,133 @@ actual was:
|
||||
snapshot @"123"
|
||||
return 123
|
||||
}
|
||||
|
||||
[<Test>]
|
||||
let ``Formatting example`` () =
|
||||
expect {
|
||||
withFormat (fun x -> x.GetType().Name)
|
||||
snapshot @"Int32"
|
||||
return 123
|
||||
}
|
||||
|
||||
expect {
|
||||
snapshot @"Int32"
|
||||
withFormat (fun x -> x.GetType().Name)
|
||||
return 123
|
||||
}
|
||||
|
||||
[<Test>]
|
||||
let ``Custom JSON output`` () =
|
||||
// Out of the box, comments in snapshots cause the JSON parser to throw, so the snapshot fails to match...
|
||||
expect {
|
||||
snapshot
|
||||
@"snapshot mismatch! snapshot at file.fs:99 (Custom JSON output) was:
|
||||
|
||||
- [JSON failed to parse:] {
|
||||
- // a key here
|
||||
- ""a"":3
|
||||
- }
|
||||
|
||||
actual was:
|
||||
|
||||
+ {
|
||||
+ ""a"": 3
|
||||
+ }"
|
||||
|
||||
return
|
||||
Assert.Throws<ExpectException> (fun () ->
|
||||
expectWithMockedFilePath ("file.fs", 99) {
|
||||
snapshotJson
|
||||
@"{
|
||||
// a key here
|
||||
""a"":3
|
||||
}"
|
||||
|
||||
return Map.ofList [ "a", 3 ]
|
||||
}
|
||||
)
|
||||
|> _.Message
|
||||
}
|
||||
|
||||
// but it can be made to like them!
|
||||
expect {
|
||||
snapshotJson
|
||||
@"{
|
||||
// a key here
|
||||
""a"":3
|
||||
}"
|
||||
|
||||
withJsonDocOptions (JsonDocumentOptions (CommentHandling = JsonCommentHandling.Skip))
|
||||
return Map.ofList [ "a", 3 ]
|
||||
}
|
||||
|
||||
type SomeDu =
|
||||
| Something of IReadOnlyDictionary<string, string>
|
||||
| SomethingElse of string
|
||||
|
||||
type MoreComplexType =
|
||||
{
|
||||
Thing : int
|
||||
SomeDu : SomeDu option
|
||||
}
|
||||
|
||||
[<Test>]
|
||||
let ``JSON snapshot of complex ADT`` () =
|
||||
expect {
|
||||
snapshotJson
|
||||
@"{
|
||||
""SomeDu"": {
|
||||
""Case"": ""Something"",
|
||||
""Fields"": [
|
||||
{
|
||||
""hi"": ""bye""
|
||||
}
|
||||
]
|
||||
},
|
||||
""Thing"": 3,
|
||||
}"
|
||||
|
||||
return
|
||||
{
|
||||
Thing = 3
|
||||
SomeDu = Some (SomeDu.Something (Map.ofList [ "hi", "bye" ]))
|
||||
}
|
||||
}
|
||||
|
||||
[<Test>]
|
||||
let ``Overriding JSON format, from docstring`` () =
|
||||
expect {
|
||||
snapshotJson @"{""a"":3}"
|
||||
withJsonSerializerOptions (JsonSerializerOptions (WriteIndented = false))
|
||||
return Map.ofList [ "a", 3 ]
|
||||
}
|
||||
|
||||
[<Test>]
|
||||
let ``Overriding the JSON format`` () =
|
||||
expect {
|
||||
snapshotJson
|
||||
@"{
|
||||
""Thing"": 3,
|
||||
""SomeDu"": [
|
||||
""Some"",
|
||||
[
|
||||
""Something"",
|
||||
{
|
||||
""hi"": ""bye""
|
||||
}
|
||||
]
|
||||
]
|
||||
}"
|
||||
|
||||
withJsonSerializerOptions (
|
||||
let options = JsonFSharpOptions.ThothLike().ToJsonSerializerOptions ()
|
||||
options.WriteIndented <- true
|
||||
options
|
||||
)
|
||||
|
||||
return
|
||||
{
|
||||
Thing = 3
|
||||
SomeDu = Some (SomeDu.Something (Map.ofList [ "hi", "bye" ]))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user