-
Notifications
You must be signed in to change notification settings - Fork 150
/
testing.go
52 lines (43 loc) · 1 KB
/
testing.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
package utils
import (
"bytes"
"github.com/stretchr/testify/assert"
"io"
"reflect"
"testing"
)
type Serializable interface {
io.ReaderFrom
io.WriterTo
}
type RawSerializable interface {
WriteRawTo(io.Writer) (int64, error)
}
func SerializationRoundTrip(o Serializable) func(*testing.T) {
return func(t *testing.T) {
// serialize it...
var buf bytes.Buffer
_, err := o.WriteTo(&buf)
assert.NoError(t, err)
// reconstruct the object
_o := reflect.New(reflect.TypeOf(o).Elem()).Interface().(Serializable)
_, err = _o.ReadFrom(&buf)
assert.NoError(t, err)
// compare
assert.Equal(t, o, _o)
}
}
func SerializationRoundTripRaw(o RawSerializable) func(*testing.T) {
return func(t *testing.T) {
// serialize it...
var buf bytes.Buffer
_, err := o.WriteRawTo(&buf)
assert.NoError(t, err)
// reconstruct the object
_o := reflect.New(reflect.TypeOf(o).Elem()).Interface().(Serializable)
_, err = _o.ReadFrom(&buf)
assert.NoError(t, err)
// compare
assert.Equal(t, o, _o)
}
}