forked from asdine/storm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_helpers.go
44 lines (38 loc) · 851 Bytes
/
test_helpers.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
package internal
import (
"encoding/gob"
"reflect"
"testing"
"github.com/AndersonBargas/rainstorm/v3/codec"
)
type testStruct struct {
Name string
}
// RoundtripTester is a test helper to test a MarshalUnmarshaler
func RoundtripTester(t *testing.T, c codec.MarshalUnmarshaler, vals ...interface{}) {
var val, to interface{}
if len(vals) > 0 {
if len(vals) != 2 {
panic("Wrong number of vals, expected 2")
}
val = vals[0]
to = vals[1]
} else {
val = &testStruct{Name: "test"}
to = &testStruct{}
}
encoded, err := c.Marshal(val)
if err != nil {
t.Fatal("Encode error:", err)
}
err = c.Unmarshal(encoded, to)
if err != nil {
t.Fatal("Decode error:", err)
}
if !reflect.DeepEqual(val, to) {
t.Fatalf("Roundtrip codec mismatch, expected\n%#v\ngot\n%#v", val, to)
}
}
func init() {
gob.Register(&testStruct{})
}