-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcombinators_external_test.go
202 lines (162 loc) · 3.79 KB
/
combinators_external_test.go
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2019 Gregory Petrosyan <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid_test
import (
"context"
"errors"
"fmt"
"strconv"
"testing"
. "pgregory.net/rapid"
)
type testStruct struct {
x int
y int
}
func genBool(t *T) bool {
return Bool().Draw(t, "")
}
func genInterface(t *T) any {
if Bool().Draw(t, "coinflip") {
return Int8().Draw(t, "")
} else {
return Float64().Draw(t, "")
}
}
func genSlice(t *T) []uint64 {
return []uint64{
Uint64().Draw(t, ""),
Uint64().Draw(t, ""),
}
}
func genStruct(t *T) testStruct {
return testStruct{
x: Int().Draw(t, "x"),
y: Int().Draw(t, "y"),
}
}
func TestCustom(t *testing.T) {
t.Parallel()
gens := []*Generator[any]{
Custom(genBool).AsAny(),
Custom(genInterface).AsAny(),
Custom(genSlice).AsAny(),
Custom(genStruct).AsAny(),
}
for _, g := range gens {
t.Run(g.String(), MakeCheck(func(t *T) { g.Draw(t, "") }))
}
}
func TestCustomContext(t *testing.T) {
t.Parallel()
type key struct{}
gen := Custom(func(t *T) context.Context {
ctx := t.Context()
// Inside the custom generator, the context must be valid.
if err := ctx.Err(); err != nil {
t.Fatalf("context must be valid: %v", err)
}
x := Int().Draw(t, "x")
return context.WithValue(ctx, key{}, x)
})
Check(t, func(t *T) {
ctx := gen.Draw(t, "value")
if _, ok := ctx.Value(key{}).(int); !ok {
t.Fatalf("context must contain an int")
}
// Outside the custom generator,
// the context from inside the generator
// must no longer be valid.
if err := ctx.Err(); err == nil || !errors.Is(err, context.Canceled) {
t.Fatalf("context must be canceled: %v", err)
}
})
}
func TestCustomCleanup(t *testing.T) {
t.Parallel()
var open bool
gen := Custom(func(t *T) int {
t.Cleanup(func() { open = false })
return Int().Draw(t, "")
})
// Cleanup functions registered during a Custom generator
// are run after generation, not after the Check.
Check(t, func(t *T) {
open = true
_ = gen.Draw(t, "value")
// Cleanup must run after each run of the custom generator.
if open {
t.Fatalf("cleanup must be run")
}
})
}
func TestFilter(t *testing.T) {
t.Parallel()
g := Int().Filter(func(i int) bool { return i >= 0 })
Check(t, func(t *T) {
v := g.Draw(t, "v")
if v < 0 {
t.Fatalf("got negative %v", v)
}
})
}
func TestMap(t *testing.T) {
t.Parallel()
g := Map(Int(), strconv.Itoa)
Check(t, func(t *T) {
s := g.Draw(t, "s")
_, err := strconv.Atoi(s)
if err != nil {
t.Fatalf("Atoi() error %v", err)
}
})
}
func TestSampledFrom(t *testing.T) {
t.Parallel()
gens := []*Generator[int]{
Just(3),
SampledFrom([]int{3, 5, 7}),
}
for _, g := range gens {
t.Run(g.String(), MakeCheck(func(t *T) {
n := g.Draw(t, "n")
if n != 3 && n != 5 && n != 7 {
t.Fatalf("got impossible %v", n)
}
}))
}
}
func TestOneOf_SameType(t *testing.T) {
t.Parallel()
pos := Int().Filter(func(v int) bool { return v >= 10 })
neg := Int().Filter(func(v int) bool { return v <= -10 })
g := OneOf(pos, neg)
Check(t, func(t *T) {
n := g.Draw(t, "n")
if n > -10 && n < 10 {
t.Fatalf("got impossible %v", n)
}
})
}
func TestOneOf_DifferentTypes(t *testing.T) {
t.Parallel()
g := OneOf(Int().AsAny(), Int8().AsAny(), Int16().AsAny(), Int32().AsAny(), Int64().AsAny())
Check(t, func(t *T) {
n := g.Draw(t, "n")
_ = rv(n).Int()
})
}
func TestPtr(t *testing.T) {
t.Parallel()
for _, allowNil := range []bool{false, true} {
t.Run(fmt.Sprintf("allowNil=%v", allowNil), MakeCheck(func(t *T) {
i := Ptr(Int(), allowNil).Draw(t, "i")
if i == nil && !allowNil {
t.Fatalf("got nil pointer")
}
}))
}
}