This repository was archived by the owner on Nov 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAutoGenConfigTests.fs
More file actions
102 lines (83 loc) · 3.82 KB
/
AutoGenConfigTests.fs
File metadata and controls
102 lines (83 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
module Hedgehog.Experimental.Tests.AutoGenConfigTests
open Hedgehog.Experimental
open Xunit
open Swensen.Unquote
open Hedgehog
[<Fact>]
let ``merging AutoGenConfig preserves set values``() =
let expectedRange = Range.exponential 2 6
let expectedDepth = 2
let config1 =
AutoGenConfig.defaults
|> AutoGenConfig.setSeqRange expectedRange
|> AutoGenConfig.setRecursionDepth expectedDepth
|> AutoGenConfig.addGenerator (Gen.int32 (Range.exponentialBounded()))
let config2 = AutoGenConfig.defaults |> AutoGenConfig.addGenerator Gen.bool
let merged = AutoGenConfig.merge config1 config2
test <@ AutoGenConfig.recursionDepth merged = expectedDepth @>
let property = property {
let! array = merged |> GenX.autoWith<(int * bool)[]>
test <@ Array.length array >= 2 && Array.length array <= 6 @>
}
Property.check property
[<Fact>]
let ``merging AutoGenConfig overrides values``() =
let previousRange = Range.exponential 10 15
let expectedRange = Range.exponential 2 6
let expectedDepth = 2
let config1 = AutoGenConfig.defaults |> AutoGenConfig.setSeqRange previousRange |> AutoGenConfig.setRecursionDepth 1
let config2 =
AutoGenConfig.defaults
|> AutoGenConfig.setSeqRange expectedRange
|> AutoGenConfig.setRecursionDepth expectedDepth
|> AutoGenConfig.addGenerator (Gen.int32 (Range.exponentialBounded()))
let merged = AutoGenConfig.merge config1 config2
test <@ AutoGenConfig.recursionDepth merged = expectedDepth @>
let property = property {
let! array = merged |> GenX.autoWith<int[]>
test <@ Array.length array >= 2 && Array.length array <= 6 @>
}
Property.check property
type CustomType = { Value: int; Items: string list }
type CustomGenerators =
// Generator that uses AutoGenConfig to access seqRange
static member CustomTypeGen(config: AutoGenConfig) : Gen<CustomType> = gen {
let! value = Gen.int32 (Range.exponentialBounded())
let! items = Gen.string (Range.linear 1 5) Gen.alpha |> Gen.list (AutoGenConfig.seqRange config)
return { Value = value; Items = items }
}
// Generator that takes both AutoGenConfig and Gen<_> parameters
static member CustomTypeWithGen(config: AutoGenConfig, genValue: Gen<int>) : Gen<CustomType> = gen {
let! value = genValue
let! items = Gen.string (Range.linear 1 5) Gen.alpha |> Gen.list (AutoGenConfig.seqRange config)
return { Value = value; Items = items }
}
[<Fact>]
let ``addGenerators supports methods with AutoGenConfig parameter``() =
let customConfig =
GenX.defaults
|> AutoGenConfig.setSeqRange (Range.constant 3 3)
|> AutoGenConfig.addGenerators<CustomGenerators>
// Create the generator and sample a value to ensure it works
let gen = customConfig |> GenX.autoWith<CustomType>
let sample = Gen.sample 0 1 gen |> Seq.head
test <@ sample.Items.Length = 3 @>
open System.Collections.Immutable
type ImmutableListGenerators =
static member ImmutableListGen<'T>(config: AutoGenConfig, genItem: Gen<'T>) : Gen<ImmutableList<'T>> =
genItem |> Gen.list (AutoGenConfig.seqRange config) |> Gen.map ImmutableList.CreateRange
[<Fact>]
let ``addGenerators supports generic methods with AutoGenConfig and Gen parameters``() =
let customConfig =
GenX.defaults
|> AutoGenConfig.setSeqRange (Range.constant 5 5)
|> AutoGenConfig.addGenerators<ImmutableListGenerators>
// The ImmutableListGen<int> will be called with config and Gen<int>
// This demonstrates that generic generators work with both AutoGenConfig and Gen<T> parameters
let gen = customConfig |> GenX.autoWith<ImmutableList<int>>
let sample = Gen.sample 0 1 gen |> Seq.head
test <@ sample.Count = 5 @>
// Also test with a different type to verify generics work
let genString = customConfig |> GenX.autoWith<ImmutableList<string>>
let sampleString = Gen.sample 0 1 genString |> Seq.head
test <@ sampleString.Count = 5 @>