mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
pkg/pbutil: add marshal-related tests
This commit is contained in:
parent
aec2eef498
commit
7e67fd13f6
@ -16,7 +16,48 @@
|
|||||||
|
|
||||||
package pbutil
|
package pbutil
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"errors"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMarshaler(t *testing.T) {
|
||||||
|
data := []byte("test data")
|
||||||
|
m := &fakeMarshaler{data: data}
|
||||||
|
if g := MustMarshal(m); !reflect.DeepEqual(g, data) {
|
||||||
|
t.Errorf("data = %s, want %s", g, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarshalerPanic(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r == nil {
|
||||||
|
t.Errorf("recover = nil, want error")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
m := &fakeMarshaler{err: errors.New("blah")}
|
||||||
|
MustMarshal(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshaler(t *testing.T) {
|
||||||
|
data := []byte("test data")
|
||||||
|
m := &fakeUnmarshaler{}
|
||||||
|
MustUnmarshal(m, data)
|
||||||
|
if !reflect.DeepEqual(m.data, data) {
|
||||||
|
t.Errorf("data = %s, want %s", m.data, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalerPanic(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r == nil {
|
||||||
|
t.Errorf("recover = nil, want error")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
m := &fakeUnmarshaler{err: errors.New("blah")}
|
||||||
|
MustUnmarshal(m, nil)
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetBool(t *testing.T) {
|
func TestGetBool(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
@ -38,3 +79,22 @@ func TestGetBool(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type fakeMarshaler struct {
|
||||||
|
data []byte
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *fakeMarshaler) Marshal() ([]byte, error) {
|
||||||
|
return m.data, m.err
|
||||||
|
}
|
||||||
|
|
||||||
|
type fakeUnmarshaler struct {
|
||||||
|
data []byte
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *fakeUnmarshaler) Unmarshal(data []byte) error {
|
||||||
|
m.data = data
|
||||||
|
return m.err
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user