-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions_test.go
178 lines (138 loc) · 3.82 KB
/
options_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
package sentry
import (
"bytes"
"encoding/json"
"os"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func ExampleAddDefaultOptions() {
// You can add default options to all of your root Sentry
// clients like this.
AddDefaultOptions(
Release("v1.0.0"),
DSN("..."),
)
}
func ExampleAddDefaultOptionProvider() {
// You can also register options providers which will dynamically
// generate options for each new event that is sent
AddDefaultOptionProvider(func() Option {
if dsn := os.Getenv("SENTRY_DSN"); dsn != "" {
return DSN(dsn)
}
return nil
})
}
func TestOptions(t *testing.T) {
oldOptionsProviders := defaultOptionProviders
defer func() {
defaultOptionProviders = oldOptionsProviders
}()
id, err := NewEventID()
assert.Nil(t, err, "there should be no errors creating the ID")
t.Run("AddDefaultOptionProvider()", func(t *testing.T) {
defaultOptionProviders = []func() Option{}
provider := func() Option {
return EventID(id)
}
AddDefaultOptionProvider(provider)
assert.Len(t, defaultOptionProviders, 1, "the provider should now be present in the default options providers list")
for _, provider := range defaultOptionProviders {
assert.Equal(t, EventID(id), provider(), "the provider should return the right option")
}
})
t.Run("AddDefaultOptions()", func(t *testing.T) {
defaultOptionProviders = []func() Option{}
AddDefaultOptions(EventID(id), nil, EventID(id))
assert.Len(t, defaultOptionProviders, 2, "the provider should now be present in the default options providers list")
for _, provider := range defaultOptionProviders {
assert.Equal(t, EventID(id), provider(), "the provider should return the right option")
}
})
}
type testOption struct {
}
func (o *testOption) Class() string {
return "test"
}
type testCustomClassOption struct {
class string
}
func (o *testCustomClassOption) Class() string {
return o.class
}
type testOmitableOption struct {
omit bool
}
func (o *testOmitableOption) Class() string {
return "test"
}
func (o *testOmitableOption) Omit() bool {
return o.omit
}
type testFinalizeableOption struct {
finalized bool
}
func (o *testFinalizeableOption) Class() string {
return "test"
}
func (o *testFinalizeableOption) Finalize() {
o.finalized = true
}
type testMergeableOption struct {
data int
}
func (o *testMergeableOption) Class() string {
return "test"
}
func (o *testMergeableOption) Merge(other Option) Option {
if oo, ok := other.(*testMergeableOption); ok {
return &testMergeableOption{
data: o.data + oo.data,
}
}
return o
}
type testAdvancedOption struct {
data map[string]Option
}
func (o *testAdvancedOption) Class() string {
return "test"
}
func (o *testAdvancedOption) Apply(packet map[string]Option) {
for k, v := range o.data {
packet[k] = v
}
}
type testSerializableOption struct {
data string
}
func (o *testSerializableOption) Class() string {
return "test"
}
func (o *testSerializableOption) MarshalJSON() ([]byte, error) {
return json.Marshal(o.data)
}
func testGetOptionsProvider(t *testing.T, sameType Option) Option {
st := reflect.TypeOf(sameType)
assert.NotNil(t, st, "getting the reflection type should not fail")
for _, provider := range defaultOptionProviders {
opt := provider()
if reflect.TypeOf(opt) == st {
return opt
}
}
return nil
}
func testOptionsSerialize(t *testing.T, opt Option) interface{} {
if opt == nil {
return nil
}
var data interface{}
buf := bytes.NewBuffer([]byte{})
assert.Nil(t, json.NewEncoder(buf).Encode(opt), "no error should occur when serializing to JSON")
assert.Nil(t, json.NewDecoder(buf).Decode(&data), "no error should occur when deserializing from JSON")
return data
}