mirror of
https://github.com/Smaug123/WoofWare.Myriad
synced 2025-10-10 14:38:39 +00:00
JSON serialization (#69)
This commit is contained in:
103
WoofWare.Myriad.Plugins.Test/TestJsonSerialize/TestJsonSerde.fs
Normal file
103
WoofWare.Myriad.Plugins.Test/TestJsonSerialize/TestJsonSerde.fs
Normal file
@@ -0,0 +1,103 @@
|
||||
namespace WoofWare.Myriad.Plugins.Test
|
||||
|
||||
open System
|
||||
open System.Collections.Generic
|
||||
open System.Text.Json.Nodes
|
||||
open NUnit.Framework
|
||||
open FsCheck
|
||||
open FsUnitTyped
|
||||
open ConsumePlugin
|
||||
|
||||
[<TestFixture>]
|
||||
module TestJsonSerde =
|
||||
|
||||
let uriGen : Gen<Uri> =
|
||||
gen {
|
||||
let! suffix = Arb.generate<int>
|
||||
return Uri $"https://example.com/%i{suffix}"
|
||||
}
|
||||
|
||||
let rec innerGen (count : int) : Gen<InnerTypeWithBoth> =
|
||||
gen {
|
||||
let! s = Arb.generate<NonNull<string>>
|
||||
let! mapKeys = Gen.listOf Arb.generate<NonNull<string>>
|
||||
let mapKeys = mapKeys |> List.map _.Get |> List.distinct
|
||||
let! mapValues = Gen.listOfLength mapKeys.Length uriGen
|
||||
let map = List.zip mapKeys mapValues |> Map.ofList
|
||||
|
||||
let! concreteDictKeys =
|
||||
if count > 0 then
|
||||
Gen.listOf Arb.generate<NonNull<string>>
|
||||
else
|
||||
Gen.constant []
|
||||
|
||||
let concreteDictKeys =
|
||||
concreteDictKeys
|
||||
|> List.map _.Get
|
||||
|> List.distinct
|
||||
|> fun x -> List.take (min 3 x.Length) x
|
||||
|
||||
let! concreteDictValues =
|
||||
if count > 0 then
|
||||
Gen.listOfLength concreteDictKeys.Length (innerGen (count - 1))
|
||||
else
|
||||
Gen.constant []
|
||||
|
||||
let concreteDict =
|
||||
List.zip concreteDictKeys concreteDictValues
|
||||
|> List.map KeyValuePair
|
||||
|> Dictionary
|
||||
|
||||
let! readOnlyDictKeys = Gen.listOf Arb.generate<NonNull<string>>
|
||||
let readOnlyDictKeys = readOnlyDictKeys |> List.map _.Get |> List.distinct
|
||||
let! readOnlyDictValues = Gen.listOfLength readOnlyDictKeys.Length (Gen.listOf Arb.generate<char>)
|
||||
let readOnlyDict = List.zip readOnlyDictKeys readOnlyDictValues |> readOnlyDict
|
||||
|
||||
let! dictKeys = Gen.listOf uriGen
|
||||
let! dictValues = Gen.listOfLength dictKeys.Length Arb.generate<bool>
|
||||
let dict = List.zip dictKeys dictValues |> dict
|
||||
|
||||
return
|
||||
{
|
||||
Thing = s.Get
|
||||
Map = map
|
||||
ReadOnlyDict = readOnlyDict
|
||||
Dict = dict
|
||||
ConcreteDict = concreteDict
|
||||
}
|
||||
}
|
||||
|
||||
let outerGen : Gen<JsonRecordTypeWithBoth> =
|
||||
gen {
|
||||
let! a = Arb.generate<int>
|
||||
let! b = Arb.generate<NonNull<string>>
|
||||
let! c = Gen.listOf Arb.generate<int>
|
||||
let! depth = Gen.choose (0, 2)
|
||||
let! d = innerGen depth
|
||||
let! e = Gen.arrayOf Arb.generate<NonNull<string>>
|
||||
let! f = Gen.arrayOf Arb.generate<int>
|
||||
|
||||
return
|
||||
{
|
||||
A = a
|
||||
B = b.Get
|
||||
C = c
|
||||
D = d
|
||||
E = e |> Array.map _.Get
|
||||
F = f
|
||||
}
|
||||
}
|
||||
|
||||
[<Test>]
|
||||
let ``It just works`` () =
|
||||
let property (o : JsonRecordTypeWithBoth) : bool =
|
||||
o
|
||||
|> JsonRecordTypeWithBoth.toJsonNode
|
||||
|> fun s -> s.ToJsonString ()
|
||||
|> JsonNode.Parse
|
||||
|> JsonRecordTypeWithBoth.jsonParse
|
||||
|> shouldEqual o
|
||||
|
||||
true
|
||||
|
||||
property |> Prop.forAll (Arb.fromGen outerGen) |> Check.QuickThrowOnFailure
|
@@ -40,4 +40,8 @@
|
||||
<ProjectReference Include="..\ConsumePlugin\ConsumePlugin.fsproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="TestJsonSerialize\TestJsonSerde.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Reference in New Issue
Block a user