viper/internal/encoding/encoder_test.go

60 lines
1.3 KiB
Go
Raw Normal View History

package encoding
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type encoder struct {
b []byte
}
2023-09-26 17:59:38 +03:00
func (e encoder) Encode(_ map[string]any) ([]byte, error) {
return e.b, nil
}
func TestEncoderRegistry_RegisterEncoder(t *testing.T) {
t.Run("OK", func(t *testing.T) {
registry := NewEncoderRegistry()
err := registry.RegisterEncoder("myformat", encoder{})
require.NoError(t, err)
})
t.Run("AlreadyRegistered", func(t *testing.T) {
registry := NewEncoderRegistry()
err := registry.RegisterEncoder("myformat", encoder{})
require.NoError(t, err)
err = registry.RegisterEncoder("myformat", encoder{})
assert.ErrorIs(t, err, ErrEncoderFormatAlreadyRegistered)
})
}
func TestEncoderRegistry_Decode(t *testing.T) {
t.Run("OK", func(t *testing.T) {
registry := NewEncoderRegistry()
encoder := encoder{
b: []byte("key: value"),
}
err := registry.RegisterEncoder("myformat", encoder)
require.NoError(t, err)
2023-09-26 17:59:38 +03:00
b, err := registry.Encode("myformat", map[string]any{"key": "value"})
require.NoError(t, err)
assert.Equal(t, "key: value", string(b))
})
t.Run("EncoderNotFound", func(t *testing.T) {
registry := NewEncoderRegistry()
2023-09-26 17:59:38 +03:00
_, err := registry.Encode("myformat", map[string]any{"key": "value"})
assert.ErrorIs(t, err, ErrEncoderNotFound)
})
}