From 1a6ff2fcc6e790a1b454ade2234b535827c46eb3 Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Sat, 25 May 2024 08:18:39 +0200 Subject: [PATCH] add additional test case Change-Id: I56767015384d06cae6f0c4cea62a9f286bfe4743 --- src/encoding/json/stream_test.go | 62 +++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/src/encoding/json/stream_test.go b/src/encoding/json/stream_test.go index 43e3e335179..11284cdf3d1 100644 --- a/src/encoding/json/stream_test.go +++ b/src/encoding/json/stream_test.go @@ -614,7 +614,7 @@ func TestDecoderMoreWithToken(t *testing.T) { dec := NewDecoder(&decoderMoreWithTokenFailingReader{t: t, err: notIgnoredError}) if val, err := dec.Token(); err != nil || val != Delim('{') { - t.Fatalf("(*Decoder).Token() = (%v, %v); want = ([, )", val, err) + t.Fatalf("(*Decoder).Token() = (%v, %v); want = ({, )", val, err) } if !dec.More() { @@ -633,3 +633,63 @@ func TestDecoderMoreWithToken(t *testing.T) { t.Fatalf("(*Decoder).Token() = (%v, %v); want = (, %v)", val, err, io.EOF) } } + +type moreWithErrorsTestReader struct { + t *testing.T + i int + err error +} + +func (c *moreWithErrorsTestReader) Read(b []byte) (n int, err error) { + i := c.i + c.i++ + + defer func() { + // Decoder always passes a buffer with size that has at least 512 Bytes. This test + // depends on that behaviour, so that the reader does not get unnecessarily complicated. + if len(b) == n { + c.t.Fatal("small buffer passed to Read") + } + }() + + switch i { + case 0: + return copy(b, `[{ "test": 1 }`), nil + case 1: + return 0, c.err + case 2: + return copy(b, `, {"test": 2}]`), nil + default: + return 0, io.EOF + } +} + +func TestMoreWithErrors(t *testing.T) { + notIgnoredError := errors.New("not ignored error") + dec := NewDecoder(&moreWithErrorsTestReader{t: t, err: notIgnoredError}) + + if val, err := dec.Token(); err != nil || val != Delim('[') { + t.Fatalf("(*Decoder).Token() = (%v, %v); want = ([, )", val, err) + } + + second := false + for dec.More() { + var msg struct{ Test int } + err := dec.Decode(&msg) + var expectErr error = nil + if second { + expectErr = notIgnoredError + } + if err != expectErr { + t.Fatalf("(*Decoder).Decode() = %v; want = %v", err, expectErr) + } + if err != nil { + return + } + second = true + } + + if val, err := dec.Token(); err != nil || val != Delim(']') { + t.Fatalf("(*Decoder).Token() = (%v, %v); want = (], )", val, err) + } +}