-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogfusc_test.go
More file actions
237 lines (176 loc) · 4.78 KB
/
logfusc_test.go
File metadata and controls
237 lines (176 loc) · 4.78 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package logfusc
import (
"encoding/json"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_NewSecret(t *testing.T) {
t.Parallel()
value := 42
s := NewSecret(value)
assert.Equal(t, value, s.value)
}
func Test_Secret_Expose(t *testing.T) {
t.Parallel()
value := 42
s := NewSecret(value)
assert.Equal(t, value, s.Expose())
}
func Test_Secret_String(t *testing.T) {
t.Parallel()
t.Run("when T is a string", func(t *testing.T) {
t.Parallel()
value := "foo"
s := NewSecret(value)
assert.Equal(t, fmt.Sprintf(redactionTmpl, value), s.String())
})
t.Run("when T is an int", func(t *testing.T) {
t.Parallel()
value := 42
s := NewSecret(value)
assert.Equal(t, fmt.Sprintf(redactionTmpl, value), s.String())
})
t.Run("when T is a struct", func(t *testing.T) {
t.Parallel()
type foo struct {
Bar string
}
value := foo{Bar: "bar"}
s := NewSecret(value)
assert.Equal(t, fmt.Sprintf(redactionTmpl, value), s.String())
})
t.Run("when T is a pointer", func(t *testing.T) {
t.Parallel()
value := 42
s := NewSecret(&value)
assert.Equal(t, fmt.Sprintf(redactionTmpl, &value), s.String())
})
}
func Test_Secret_GoString(t *testing.T) {
t.Parallel()
t.Run("when T is a string", func(t *testing.T) {
t.Parallel()
value := "foo"
s := NewSecret(value)
assert.Equal(t, fmt.Sprintf(redactionTmpl, value), s.GoString())
})
t.Run("when T is an int", func(t *testing.T) {
t.Parallel()
value := 42
s := NewSecret(value)
assert.Equal(t, fmt.Sprintf(redactionTmpl, value), s.GoString())
})
t.Run("when T is a struct", func(t *testing.T) {
t.Parallel()
type foo struct {
Bar string
}
value := foo{Bar: "bar"}
s := NewSecret(value)
assert.Equal(t, fmt.Sprintf(redactionTmpl, value), s.GoString())
})
t.Run("when T is a pointer", func(t *testing.T) {
t.Parallel()
value := 42
s := NewSecret(&value)
assert.Equal(t, fmt.Sprintf(redactionTmpl, &value), s.GoString())
})
}
func Test_string_formatting(t *testing.T) {
t.Parallel()
value := "password"
secret := NewSecret(value)
expected := fmt.Sprintf(redactionTmpl, value)
testCases := []struct {
verb string
expected string
}{
{"%s", expected},
{"%q", fmt.Sprintf("\"%s\"", expected)},
{"%v", expected},
{"%+v", expected},
{"%#v", expected},
{"%x", fmt.Sprintf("%x", expected)},
{"%X", fmt.Sprintf("%X", expected)},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.verb, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.expected, fmt.Sprintf(tc.verb, secret))
})
}
}
func Test_Secret_MarshalJSON(t *testing.T) {
t.Parallel()
secretValue := "password"
t.Run("can be marshaled standalone", func(t *testing.T) {
t.Parallel()
secret := NewSecret(secretValue)
b, err := secret.MarshalJSON()
assert.NoError(t, err)
assert.JSONEq(t, fmt.Sprintf("%q", fmt.Sprintf(redactionTmpl, secretValue)), string(b))
})
t.Run("can be marshaled as part of a struct", func(t *testing.T) {
t.Parallel()
type foo struct {
Secret Secret[string] `json:"secret"`
}
secret := NewSecret(secretValue)
s := foo{Secret: secret}
b, err := json.Marshal(s)
assert.NoError(t, err)
assert.JSONEq(t, fmt.Sprintf("{%q: %q}", "secret", fmt.Sprintf(redactionTmpl, secretValue)), string(b))
})
}
func Test_Secret_UnmarshalJSON(t *testing.T) {
t.Parallel()
t.Run("when the the unmarshal target is valid", func(t *testing.T) {
t.Parallel()
t.Run("unmarshaling null is a no-op", func(t *testing.T) {
t.Parallel()
var secret Secret[string]
err := json.Unmarshal([]byte(`null`), &secret)
assert.NoError(t, err)
assert.Empty(t, secret)
})
t.Run("Secret can be unmarshaled standalone", func(t *testing.T) {
t.Parallel()
var secret Secret[string]
err := json.Unmarshal([]byte(`"password"`), &secret)
assert.NoError(t, err)
assert.Equal(t, "password", secret.value)
})
t.Run("Secret can be unmarshaled as part of a struct", func(t *testing.T) {
t.Parallel()
type foo struct {
Secret Secret[string] `json:"secret"`
}
var secretStruct foo
err := json.Unmarshal([]byte(`{"secret": "password"}`), &secretStruct)
assert.NoError(t, err)
assert.Equal(t, "password", secretStruct.Secret.value)
})
})
t.Run("when the unmarshal target is invalid", func(t *testing.T) {
t.Parallel()
value := &unUnmarshalable{}
secret := NewSecret(value)
err := json.Unmarshal([]byte("0"), &secret)
var unmarshalErr *UnmarshalError[*unUnmarshalable]
assert.ErrorAs(t, err, &unmarshalErr)
})
}
func Test_UnmarshalError_Error(t *testing.T) {
t.Parallel()
err := UnmarshalError[int]{
intendedTarget: 0,
}
assert.Equal(t, fmt.Sprintf(unmarshalErrorTmpl, 0), err.Error())
}
type unUnmarshalable struct{}
func (u *unUnmarshalable) UnmarshalJSON([]byte) error {
return errors.New("unmarshal error")
}