|
| 1 | +module PropertyTests |
| 2 | + |
| 3 | +open System |
| 4 | +open FsCheck |
| 5 | +open FsCheck.Xunit |
| 6 | +open CodecMapper |
| 7 | +open TestCommon |
| 8 | + |
| 9 | +let private addressSchema = |
| 10 | + Schema.define<Address> |
| 11 | + |> Schema.construct makeAddress |
| 12 | + |> Schema.field "street" _.Street |
| 13 | + |> Schema.field "city" _.City |
| 14 | + |> Schema.build |
| 15 | + |
| 16 | +let private personSchema = |
| 17 | + Schema.define<Person> |
| 18 | + |> Schema.construct makePerson |
| 19 | + |> Schema.field "id" _.Id |
| 20 | + |> Schema.field "name" _.Name |
| 21 | + |> Schema.fieldWith "home" _.Home addressSchema |
| 22 | + |> Schema.build |
| 23 | + |
| 24 | +let private optionalRecordSchema = |
| 25 | + Schema.define<OptionalRecord> |
| 26 | + |> Schema.construct makeOptionalRecord |
| 27 | + |> Schema.field "nickname" _.Nickname |
| 28 | + |> Schema.field "age" _.Age |
| 29 | + |> Schema.build |
| 30 | + |
| 31 | +let private collectionSchema = |
| 32 | + Schema.define<CollectionRecord> |
| 33 | + |> Schema.construct makeCollectionRecord |
| 34 | + |> Schema.field "list" _.List |
| 35 | + |> Schema.field "array" _.Array |
| 36 | + |> Schema.build |
| 37 | + |
| 38 | +type private PropertyArbitraries = |
| 39 | + static member private SafeText() : Arbitrary<string> = |
| 40 | + let safeChar: Gen<char> = |
| 41 | + FsCheck.FSharp.Gen.elements ([ 'a' .. 'z' ] @ [ 'A' .. 'Z' ] @ [ '0' .. '9' ] @ [ ' '; '-'; '_'; '.' ]) |
| 42 | + |
| 43 | + let generator: Gen<string> = |
| 44 | + FsCheck.FSharp.GenBuilder.gen { |
| 45 | + let! length = FsCheck.FSharp.Gen.choose (0, 24) |
| 46 | + let! chars = FsCheck.FSharp.Gen.arrayOfLength length safeChar |
| 47 | + return String(chars: char array) |
| 48 | + } |
| 49 | + |
| 50 | + FsCheck.FSharp.Arb.fromGen generator |
| 51 | + |
| 52 | + static member Address() : Arbitrary<Address> = |
| 53 | + let safeText: Gen<string> = PropertyArbitraries.SafeText().Generator |
| 54 | + |
| 55 | + let generator: Gen<Address> = |
| 56 | + FsCheck.FSharp.GenBuilder.gen { |
| 57 | + let! street = safeText |
| 58 | + let! city = safeText |
| 59 | + return { Street = street; City = city } |
| 60 | + } |
| 61 | + |
| 62 | + FsCheck.FSharp.Arb.fromGen generator |
| 63 | + |
| 64 | + static member Person() : Arbitrary<Person> = |
| 65 | + let safeText: Gen<string> = PropertyArbitraries.SafeText().Generator |
| 66 | + let addressGen: Gen<Address> = PropertyArbitraries.Address().Generator |
| 67 | + |
| 68 | + let generator: Gen<Person> = |
| 69 | + FsCheck.FSharp.GenBuilder.gen { |
| 70 | + let! id = FsCheck.FSharp.Gen.choose (-5000, 5000) |
| 71 | + let! name = safeText |
| 72 | + let! home = addressGen |
| 73 | + return { Id = id; Name = name; Home = home } |
| 74 | + } |
| 75 | + |
| 76 | + FsCheck.FSharp.Arb.fromGen generator |
| 77 | + |
| 78 | + static member OptionalRecord() : Arbitrary<OptionalRecord> = |
| 79 | + let safeText: Gen<string> = PropertyArbitraries.SafeText().Generator |
| 80 | + |
| 81 | + let safeOption (generator: Gen<'T>) : Gen<'T option> = |
| 82 | + FsCheck.FSharp.Gen.frequency ( |
| 83 | + [ |
| 84 | + 1, FsCheck.FSharp.Gen.constant None |
| 85 | + 3, FsCheck.FSharp.Gen.map Some generator |
| 86 | + ] |
| 87 | + ) |
| 88 | + |
| 89 | + let generator: Gen<OptionalRecord> = |
| 90 | + FsCheck.FSharp.GenBuilder.gen { |
| 91 | + let! nickname = safeOption safeText |
| 92 | + let! age = safeOption (FsCheck.FSharp.Gen.choose (-5000, 5000)) |
| 93 | + return { Nickname = nickname; Age = age } |
| 94 | + } |
| 95 | + |
| 96 | + FsCheck.FSharp.Arb.fromGen generator |
| 97 | + |
| 98 | + static member CollectionRecord() : Arbitrary<CollectionRecord> = |
| 99 | + let safeText: Gen<string> = PropertyArbitraries.SafeText().Generator |
| 100 | + |
| 101 | + let generator: Gen<CollectionRecord> = |
| 102 | + FsCheck.FSharp.GenBuilder.gen { |
| 103 | + let! values = FsCheck.FSharp.Gen.listOf (FsCheck.FSharp.Gen.choose (-100, 100)) |
| 104 | + let! aliases = FsCheck.FSharp.Gen.map List.toArray (FsCheck.FSharp.Gen.listOf safeText) |
| 105 | + return { List = values; Array = aliases } |
| 106 | + } |
| 107 | + |
| 108 | + FsCheck.FSharp.Arb.fromGen generator |
| 109 | + |
| 110 | +let private jsonPersonCodec = Json.compile personSchema |
| 111 | +let private xmlPersonCodec = Xml.compile personSchema |
| 112 | +let private jsonOptionalCodec = Json.compile optionalRecordSchema |
| 113 | +let private xmlOptionalCodec = Xml.compile optionalRecordSchema |
| 114 | +let private jsonCollectionCodec = Json.compile collectionSchema |
| 115 | +let private xmlCollectionCodec = Xml.compile collectionSchema |
| 116 | + |
| 117 | +[<Property(Arbitrary = [| typeof<PropertyArbitraries> |], MaxTest = 100)>] |
| 118 | +let ``Person round-trips through JSON`` (value: Person) = |
| 119 | + Json.deserialize jsonPersonCodec (Json.serialize jsonPersonCodec value) = value |
| 120 | + |
| 121 | +[<Property(Arbitrary = [| typeof<PropertyArbitraries> |], MaxTest = 100)>] |
| 122 | +let ``Person round-trips through XML`` (value: Person) = |
| 123 | + Xml.deserialize xmlPersonCodec (Xml.serialize xmlPersonCodec value) = value |
| 124 | + |
| 125 | +[<Property(Arbitrary = [| typeof<PropertyArbitraries> |], MaxTest = 100)>] |
| 126 | +let ``Optional records round-trip through JSON`` (value: OptionalRecord) = |
| 127 | + Json.deserialize jsonOptionalCodec (Json.serialize jsonOptionalCodec value) = value |
| 128 | + |
| 129 | +[<Property(Arbitrary = [| typeof<PropertyArbitraries> |], MaxTest = 100)>] |
| 130 | +let ``Optional records round-trip through XML`` (value: OptionalRecord) = |
| 131 | + Xml.deserialize xmlOptionalCodec (Xml.serialize xmlOptionalCodec value) = value |
| 132 | + |
| 133 | +[<Property(Arbitrary = [| typeof<PropertyArbitraries> |], MaxTest = 100)>] |
| 134 | +let ``Collection records round-trip through JSON`` (value: CollectionRecord) = |
| 135 | + Json.deserialize jsonCollectionCodec (Json.serialize jsonCollectionCodec value) = value |
| 136 | + |
| 137 | +[<Property(Arbitrary = [| typeof<PropertyArbitraries> |], MaxTest = 100)>] |
| 138 | +let ``Collection records round-trip through XML`` (value: CollectionRecord) = |
| 139 | + Xml.deserialize xmlCollectionCodec (Xml.serialize xmlCollectionCodec value) = value |
0 commit comments