|
|
@@ -16,7 +16,48 @@
|
|
|
|
|
|
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) {
|
|
|
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
|
|
|
+}
|