forked from flyingmutant/rapid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_test.go
More file actions
82 lines (65 loc) · 1.89 KB
/
make_test.go
File metadata and controls
82 lines (65 loc) · 1.89 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
// Copyright 2022 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 (
"reflect"
"testing"
"pgregory.net/rapid"
)
type privateFields struct {
private bool
}
type fieldOverride struct {
Name string
Count int
}
type stringAlias string
type kindOverride struct {
Name string
Alias stringAlias
}
func TestMakeIgnoresPrivateFields(t *testing.T) {
// Private fields are ignored (and don't panic).
rapid.Make[privateFields]().Example()
}
func TestMakeCustomTypeOverride(t *testing.T) {
ex := rapid.MakeCustom[privateFields](rapid.MakeConfig{
Types: map[reflect.Type]*rapid.Generator[any]{
reflect.TypeOf(privateFields{}): rapid.Just(privateFields{private: true}).AsAny(),
},
}).Example()
if !ex.private {
t.Errorf(".private should be true. got: %#v", ex)
}
}
func TestMakeCustomFieldOverride(t *testing.T) {
const customName = "custom-name"
v := rapid.MakeCustom[fieldOverride](rapid.MakeConfig{
Fields: map[reflect.Type]map[string]*rapid.Generator[any]{
reflect.TypeOf(fieldOverride{}): {
"Name": rapid.Just(customName).AsAny(),
},
},
}).Example()
if v.Name != customName {
t.Fatalf("expected Name to be %q, got %q", customName, v.Name)
}
}
func TestMakeCustomKindOverride(t *testing.T) {
const customValue = "kind-override"
v := rapid.MakeCustom[kindOverride](rapid.MakeConfig{
Kinds: map[reflect.Kind]*rapid.Generator[any]{
reflect.String: rapid.Just(customValue).AsAny(),
},
}).Example()
if v.Name != customValue {
t.Fatalf("expected Name to be %q, got %q", customValue, v.Name)
}
expectedAlias := stringAlias(customValue)
if v.Alias != expectedAlias {
t.Fatalf("expected Alias to be %q, got %q", expectedAlias, v.Alias)
}
}