forked from reddit/baseplate.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding_test.go
98 lines (89 loc) · 1.76 KB
/
encoding_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
package secrets
import (
"errors"
"strings"
"testing"
)
func TestEncoding(t *testing.T) {
t.Parallel()
cases := []struct {
name string
enc Encoding
marshalled string
err error
}{
{
name: "invalid",
enc: -1,
marshalled: `"invalid"`,
err: ErrInvalidEncoding,
},
{
name: "identity",
enc: IdentityEncoding,
marshalled: identityEncodingJSON,
},
{
name: "base64",
enc: Base64Encoding,
marshalled: base64EncodingJSON,
},
}
for _, _c := range cases {
c := _c
t.Run(
"c.name",
func(t *testing.T) {
t.Run(
"MarshalJSON",
func(t *testing.T) {
b, err := c.enc.MarshalJSON()
if !errors.Is(err, c.err) {
t.Fatalf("error mismatch, expected %#v, got %#v", c.err, err)
}
if err != nil {
return
}
marshalled := string(b)
if strings.Compare(marshalled, c.marshalled) != 0 {
t.Fatalf("value mismatch, expected %q, got %q", c.marshalled, marshalled)
}
},
)
t.Run(
"UnmarshalJSON",
func(t *testing.T) {
var e Encoding
err := (&e).UnmarshalJSON([]byte(c.marshalled))
if !errors.Is(err, c.err) {
t.Fatalf("error mismatch, expected %#v, got %#v", c.err, err)
}
if err != nil {
return
}
if e != c.enc {
t.Fatalf("encoding does not match, expected %v, got %v", c.enc, e)
}
},
)
},
)
}
t.Run(
"UnmarshalJSON/fallback",
func(t *testing.T) {
var e Encoding
err := (&e).UnmarshalJSON([]byte(`""`))
if err != nil {
t.Fatal(err)
}
if e != IdentityEncoding {
t.Fatalf(
"encoding does not match, expected %v, got %v",
IdentityEncoding,
e,
)
}
},
)
}