Fix YAMLDecoder Read behaviour

Make it adhere to the Read contract by returning the number of bytes
read.
This commit is contained in:
Steve Larkin 2017-12-10 13:00:14 +01:00
parent bb72237375
commit 86d02ac368
2 changed files with 15 additions and 1 deletions

View File

@ -122,7 +122,7 @@ func (d *YAMLDecoder) Read(data []byte) (n int, err error) {
if left <= len(data) { if left <= len(data) {
copy(data, d.remaining) copy(data, d.remaining)
d.remaining = nil d.remaining = nil
return len(d.remaining), nil return left, nil
} }
// caller will need to reread // caller will need to reread

View File

@ -22,12 +22,26 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"math/rand" "math/rand"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
) )
func TestYAMLDecoder(t *testing.T) {
d := `---
stuff: 1
test-foo: 1
`
s := NewDocumentDecoder(ioutil.NopCloser(bytes.NewReader([]byte(d))))
b := make([]byte, len(d))
n, err := s.Read(b)
if err != nil || n != len(d) {
t.Fatalf("unexpected body: %d / %v", n, err)
}
}
func TestSplitYAMLDocument(t *testing.T) { func TestSplitYAMLDocument(t *testing.T) {
testCases := []struct { testCases := []struct {
input string input string