-
Notifications
You must be signed in to change notification settings - Fork 2k
/
encoding_test.go
105 lines (72 loc) · 1.89 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
99
100
101
102
103
104
105
package viper
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type codec struct{}
func (codec) Encode(_ map[string]any) ([]byte, error) {
return nil, nil
}
func (codec) Decode(_ []byte, _ map[string]any) error {
return nil
}
func TestDefaultCodecRegistry(t *testing.T) {
t.Run("OK", func(t *testing.T) {
registry := NewCodecRegistry()
c := codec{}
err := registry.RegisterCodec("myformat", c)
require.NoError(t, err)
encoder, err := registry.Encoder("myformat")
require.NoError(t, err)
assert.Equal(t, c, encoder)
decoder, err := registry.Decoder("myformat")
require.NoError(t, err)
assert.Equal(t, c, decoder)
})
t.Run("CodecNotFound", func(t *testing.T) {
registry := NewCodecRegistry()
_, err := registry.Encoder("myformat")
require.Error(t, err)
_, err = registry.Decoder("myformat")
require.Error(t, err)
})
t.Run("FormatIsCaseInsensitive", func(t *testing.T) {
registry := NewCodecRegistry()
c := codec{}
err := registry.RegisterCodec("MYFORMAT", c)
require.NoError(t, err)
{
encoder, err := registry.Encoder("myformat")
require.NoError(t, err)
assert.Equal(t, c, encoder)
}
{
encoder, err := registry.Encoder("MYFORMAT")
require.NoError(t, err)
assert.Equal(t, c, encoder)
}
{
decoder, err := registry.Decoder("myformat")
require.NoError(t, err)
assert.Equal(t, c, decoder)
}
{
decoder, err := registry.Decoder("MYFORMAT")
require.NoError(t, err)
assert.Equal(t, c, decoder)
}
})
t.Run("OverrideDefault", func(t *testing.T) {
registry := NewCodecRegistry()
c := codec{}
err := registry.RegisterCodec("yaml", c)
require.NoError(t, err)
encoder, err := registry.Encoder("yaml")
require.NoError(t, err)
assert.Equal(t, c, encoder)
decoder, err := registry.Decoder("yaml")
require.NoError(t, err)
assert.Equal(t, c, decoder)
})
}