This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder_test.go
192 lines (171 loc) · 4.12 KB
/
builder_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package clog
import (
"context"
"testing"
"github.com/alcionai/clues"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type BuilderUnitSuite struct {
suite.Suite
}
func TestBuilderUnitSuite(t *testing.T) {
suite.Run(t, new(BuilderUnitSuite))
}
func (suite *BuilderUnitSuite) TestBuilder() {
table := []struct {
name string
init func(ctx context.Context) context.Context
bldr func(ctx context.Context) *builder
}{
{
name: "standard",
init: func(ctx context.Context) context.Context {
return Init(
ctx,
Settings{}.EnsureDefaults())
},
bldr: func(ctx context.Context) *builder {
return Ctx(ctx)
},
},
{
name: "singleton",
init: func(ctx context.Context) context.Context {
return Init(
ctx,
Settings{}.EnsureDefaults())
},
bldr: func(ctx context.Context) *builder {
return Singleton()
},
},
{
name: "singleton, no prior init",
init: func(ctx context.Context) context.Context {
return ctx
},
bldr: func(ctx context.Context) *builder {
return Singleton()
},
},
}
for _, test := range table {
var (
t = suite.T()
ctx = test.init(context.Background())
bld = test.bldr(ctx)
)
// standard builder checks
bld.With("foo", "bar", "baz", 1)
assert.Contains(t, bld.with, "foo")
assert.Equal(t, bld.with["foo"].(string), "bar")
assert.Contains(t, bld.with, "baz")
assert.Equal(t, bld.with["baz"].(int), 1)
bld.Label("l1", "l2", "l1")
assert.Contains(t, bld.labels, "l1")
assert.Contains(t, bld.labels, "l2")
bld.Comment("a comment")
bld.Comment("another comment")
bld.Comment("a comment")
assert.Contains(t, bld.comments, "a comment")
assert.Contains(t, bld.comments, "another comment")
bld.SkipCaller(1)
// ensure no collision between separate builders
// using the same ctx.
err := clues.New("an error").
With("fnords", "i have seen them").
Label("errLabel")
other := CtxErr(ctx, err)
assert.Empty(t, other.with)
assert.Empty(t, other.labels)
assert.Empty(t, other.comments)
assert.ErrorIs(t, other.err, err, clues.ToCore(err))
other.With("foo", "smarf")
assert.Contains(t, other.with, "foo")
assert.Equal(t, bld.with["foo"].(string), "bar")
other.Label("l3")
assert.Contains(t, other.labels, "l3")
assert.NotContains(t, bld.labels, "l3")
other.Comment("comment a")
assert.Contains(t, other.comments, "comment a")
assert.NotContains(t, bld.comments, "comment a")
// ensure no panics when logging
suite.testDebugLogs(bld)
suite.testInfoLogs(bld)
suite.testErrorLogs(bld)
}
}
func (suite *BuilderUnitSuite) testDebugLogs(bld *builder) {
bld.Debug("a", "log")
bld.Debugf("a %s", "log")
bld.Debugw("a log", "with key")
bld.Debugw("a log", "with key", "and value")
// negative skip caller, just to ensure safety
bld.
SkipCaller(-1).
Debugw("a log", "with key", "and value")
}
func (suite *BuilderUnitSuite) testInfoLogs(bld *builder) {
bld.Info("a", "log")
bld.Infof("a %s", "log")
bld.Infow("a log", "with key")
bld.Infow("a log", "with key", "and value")
// negative skip caller, just to ensure safety
bld.
SkipCaller(-1).
Infow("a log", "with key", "and value")
}
func (suite *BuilderUnitSuite) testErrorLogs(bld *builder) {
bld.Error("a", "log")
bld.Errorf("a %s", "log")
bld.Errorw("a log", "with key")
bld.Errorw("a log", "with key", "and value")
// negative skip caller, just to ensure safety
bld.
SkipCaller(-1).
Errorw("a log", "with key", "and value")
}
func (suite *BuilderUnitSuite) TestGetValue() {
table := []struct {
name string
value any
expected any
}{
{
name: "integer",
value: 1,
expected: 1,
},
{
name: "string",
value: "foo",
expected: "foo",
},
{
name: "pointer to string",
value: func() *string {
s := "foo"
return &s
}(),
expected: "foo",
},
{
name: "nil",
value: nil,
expected: nil,
},
{
name: "nil pointer",
value: func() *string {
return nil
}(),
expected: nil,
},
}
for _, tt := range table {
suite.T().Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, getValue(tt.value))
})
}
}