Commit 05c5233f authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Kamil Kisiel

tests: Split TestEncode into main driver + worker that tests 1 particular case

Similarly to TestDecode. No integration with main tests table yet.
parent b291b470
......@@ -101,6 +101,47 @@ func TestDecode(t *testing.T) {
}
}
// TestEncode verifies ogórek encoder.
func TestEncode(t *testing.T) {
type foo struct {
Foo string
Bar int32
}
tests := []struct {
name string
input interface{}
output interface{}
}{
{
"graphite message",
graphiteObject1,
nil,
},
{
"small types",
[]interface{}{int64(0), int64(1), int64(258), int64(65537), false, true},
nil,
},
{
"array of struct types",
[]foo{{"Qux", 4}},
[]interface{}{map[interface{}]interface{}{"Foo": "Qux", "Bar": int64(4)}},
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%s", test.name), func(t *testing.T) {
objectOut := test.output
if objectOut == nil {
objectOut = test.input
}
testEncode(t, test.input, objectOut)
})
}
}
// testDecode decodes input and verifies it is == object.
//
// It also verifies decoder robustness - via feeding it various kinds of
......@@ -168,64 +209,46 @@ func testDecode(t *testing.T, object interface{}, input string) {
}
}
func TestEncode(t *testing.T) {
type foo struct {
Foo string
Bar int32
}
// testEncode encodes object and verifies it is ok.
//
// It also verifies that encoder handles write errors via using it on all kinds
// of limited writers. The data, that encoder produces, must decode back to
// expected object.
func testEncode(t *testing.T, object, objectDecodedBack interface{}) {
buf := &bytes.Buffer{}
enc := NewEncoder(buf)
tests := []struct {
name string
input interface{}
output interface{}
}{
{
"graphite message",
graphiteObject1,
nil,
},
{
"small types",
[]interface{}{int64(0), int64(1), int64(258), int64(65537), false, true},
nil,
},
{
"array of struct types",
[]foo{{"Qux", 4}},
[]interface{}{map[interface{}]interface{}{"Foo": "Qux", "Bar": int64(4)}},
},
// encode(object)
err := enc.Encode(object)
if err != nil {
t.Fatalf("encode error: %s", err)
}
for _, tt := range tests {
p := &bytes.Buffer{}
e := NewEncoder(p)
err := e.Encode(tt.input)
if err != nil {
t.Errorf("%s: encode error: %v", tt.name, err)
}
d := NewDecoder(bytes.NewReader(p.Bytes()))
output, _ := d.Decode()
want := tt.output
if want == nil {
want = tt.input
}
data := buf.String()
if !reflect.DeepEqual(want, output) {
t.Errorf("%s: got\n%q\n expected\n%q", tt.name, output, want)
// encode | limited writer -> write error
for l := int64(len(data))-1; l >= 0; l-- {
buf.Reset()
enc = NewEncoder(LimitWriter(buf, l))
err = enc.Encode(object)
if err != io.EOF {
t.Errorf("encoder did not handle write error @%d: got %#v", l, err)
}
}
for l := int64(p.Len())-1; l >= 0; l-- {
p.Reset()
e := NewEncoder(LimitWriter(p, l))
err = e.Encode(tt.input)
if err != io.EOF {
t.Errorf("%s: encoder did not handle write error @%v: got %#v", tt.name, l, err)
// decode(encode(object)) == object
dec := NewDecoder(bytes.NewBufferString(data))
v, err := dec.Decode()
if err != nil {
t.Errorf("encode -> decode -> error: %s", err)
} else {
if !reflect.DeepEqual(v, objectDecodedBack) {
what := "identity"
if object != objectDecodedBack {
what = "expected object"
}
t.Errorf("encode -> decode != %s\nhave: %#v\nwant: %#v", what, v, objectDecodedBack)
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment