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) { ...@@ -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. // testDecode decodes input and verifies it is == object.
// //
// It also verifies decoder robustness - via feeding it various kinds of // It also verifies decoder robustness - via feeding it various kinds of
...@@ -168,64 +209,46 @@ func testDecode(t *testing.T, object interface{}, input string) { ...@@ -168,64 +209,46 @@ func testDecode(t *testing.T, object interface{}, input string) {
} }
} }
func TestEncode(t *testing.T) { // testEncode encodes object and verifies it is ok.
//
type foo struct { // It also verifies that encoder handles write errors via using it on all kinds
Foo string // of limited writers. The data, that encoder produces, must decode back to
Bar int32 // expected object.
} func testEncode(t *testing.T, object, objectDecodedBack interface{}) {
buf := &bytes.Buffer{}
enc := NewEncoder(buf)
tests := []struct { // encode(object)
name string err := enc.Encode(object)
input interface{} if err != nil {
output interface{} t.Fatalf("encode error: %s", err)
}{
{
"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 _, tt := range tests { data := buf.String()
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
}
if !reflect.DeepEqual(want, output) { // encode | limited writer -> write error
t.Errorf("%s: got\n%q\n expected\n%q", tt.name, output, want) 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-- { // decode(encode(object)) == object
p.Reset() dec := NewDecoder(bytes.NewBufferString(data))
e := NewEncoder(LimitWriter(p, l)) v, err := dec.Decode()
err = e.Encode(tt.input) if err != nil {
if err != io.EOF { t.Errorf("encode -> decode -> error: %s", err)
t.Errorf("%s: encoder did not handle write error @%v: got %#v", tt.name, l, 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