-
Notifications
You must be signed in to change notification settings - Fork 25
/
combinators_external_test.go
145 lines (117 loc) · 2.58 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
// Copyright 2019 Gregory Petrosyan <gregory.petrosyan@gmail.com>
//
// 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 (
"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 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")
}
}))
}
}