forked from SierraSoftworks/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions_test.go
170 lines (133 loc) · 3.37 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
package sentry
import (
"bytes"
"encoding/json"
"os"
"reflect"
"testing"
"github.com/smartystreets/goconvey/convey"
. "github.com/smartystreets/goconvey/convey"
)
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) {
Convey("Options", t, func() {
oldOptionsProviders := defaultOptionProviders
defaultOptionProviders = []func() Option{}
defer func() {
defaultOptionProviders = oldOptionsProviders
}()
Convey("AddDefaultOptionProvider", func() {
So(defaultOptionProviders, ShouldBeEmpty)
id, err := NewEventID()
So(err, ShouldBeNil)
provider := func() Option {
return EventID(id)
}
AddDefaultOptionProvider(provider)
So(defaultOptionProviders, ShouldHaveLength, 1)
for _, provider := range defaultOptionProviders {
So(provider(), ShouldResemble, EventID(id))
}
})
Convey("AddDefaultOptions", func() {
So(defaultOptionProviders, ShouldBeEmpty)
id, err := NewEventID()
So(err, ShouldBeNil)
AddDefaultOptions(EventID(id), nil, EventID(id))
So(defaultOptionProviders, ShouldHaveLength, 2)
for _, provider := range defaultOptionProviders {
So(provider(), ShouldResemble, EventID(id))
}
})
})
}
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 testFinalizableOption struct {
finalized bool
}
func (o *testFinalizableOption) Class() string {
return "test"
}
func (o *testFinalizableOption) 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 testSerializableOption struct {
data string
}
func (o *testSerializableOption) Class() string {
return "test"
}
func (o *testSerializableOption) MarshalJSON() ([]byte, error) {
return json.Marshal(o.data)
}
func testGetOptionsProvider(sameType Option) Option {
st := reflect.TypeOf(sameType)
convey.So(st, convey.ShouldNotBeNil)
for _, provider := range defaultOptionProviders {
opt := provider()
if reflect.TypeOf(opt) == st {
return opt
}
}
return nil
}
func testOptionsSerialize(opt Option) interface{} {
if opt == nil {
return nil
}
var data interface{}
buf := bytes.NewBuffer([]byte{})
convey.So(json.NewEncoder(buf).Encode(opt), convey.ShouldBeNil)
convey.So(json.NewDecoder(buf).Decode(&data), convey.ShouldBeNil)
return data
}