-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgenerator_test.go
83 lines (66 loc) · 1.78 KB
/
generator_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
// 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
import (
"context"
"errors"
"testing"
)
type trivialGenImpl struct{}
func (trivialGenImpl) String() string { return "" }
func (trivialGenImpl) value(t *T) uint64 { return t.s.drawBits(64) }
func BenchmarkTrivialGenImplValue(b *testing.B) {
t := newT(nil, newRandomBitStream(baseSeed(), false), false, nil)
g := trivialGenImpl{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
g.value(t)
}
}
func BenchmarkGenerator_Value(b *testing.B) {
t := newT(nil, newRandomBitStream(baseSeed(), false), false, nil)
g := newGenerator[uint64](trivialGenImpl{})
b.ResetTimer()
for i := 0; i < b.N; i++ {
g.value(t)
}
}
func TestExampleHelper(t *testing.T) {
g := Custom(func(t *T) int {
t.Helper()
return Int().Draw(t, t.Name())
})
g.Example(0)
}
func TestCustomExampleContext(t *testing.T) {
type key struct{}
g := Custom(func(t *T) context.Context {
ctx := context.WithValue(t.Context(), key{}, Int().Draw(t, "x"))
if err := ctx.Err(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
return ctx
})
ctx := g.Example(0)
if err := ctx.Err(); err == nil || !errors.Is(err, context.Canceled) {
t.Fatalf("expected context to be canceled, got: %v", err)
}
if _, ok := ctx.Value(key{}).(int); !ok {
t.Fatalf("context must have a value")
}
}
func TestCustomExampleCleanup(t *testing.T) {
var state bool
g := Custom(func(t *T) int {
t.Cleanup(func() { state = false })
return Int().Draw(t, "x")
})
state = true
_ = g.Example(0)
if state {
t.Fatalf("cleanup must be called")
}
}