aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/encoding/json/decoder_util_test.go
blob: e4cb38e8b7169cbd83442b597509a1fd6cc20444 (plain) (blame)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
package json

import (
	"bytes"
	"encoding/json"
	"testing"

	smithytesting "github.com/aws/smithy-go/testing"
)

func TestDiscardUnknownField(t *testing.T) {
	cases := map[string][]byte{
		"empty object":  []byte(`{}`),
		"simple object": []byte(`{"foo": "bar"}`),
		"nested object": []byte(`{"foo": {"bar": "baz"}}`),
		"empty list":    []byte(`[]`),
		"simple list":   []byte(`["foo", "bar", "baz"]`),
		"nested list":   []byte(`["foo", ["bar", ["baz"]]]`),
		"number":        []byte(`1`),
		"boolean":       []byte(`true`),
		"null":          []byte(`null`),
		"string":        []byte(`"foo"`),
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			buff := bytes.NewBuffer(c)
			decoder := json.NewDecoder(buff)
			err := DiscardUnknownField(decoder)
			if err != nil {
				t.Fatalf("failed to discard, %v", err)
			}
			if decoder.More() {
				t.Fatalf("failed to discard entire contents")
			}
		})
	}
}

func TestCollectUnknownField(t *testing.T) {
	cases := map[string][]byte{
		"empty object":  []byte(`{}`),
		"simple object": []byte(`{"foo": "bar"}`),
		"nested object": []byte(`{"foo": {"bar": "baz"}}`),
		"empty list":    []byte(`[]`),
		"simple list":   []byte(`["foo", "bar", "baz"]`),
		"nested list":   []byte(`["foo", ["bar", ["baz"]]]`),
		"number":        []byte(`1`),
		"boolean":       []byte(`true`),
		"null":          []byte(`null`),
		"string":        []byte(`"foo"`),
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			buff := bytes.NewBuffer(c)
			decoder := json.NewDecoder(buff)
			actual, err := CollectUnknownField(decoder)
			if err != nil {
				t.Fatalf("failed to collect, %v", err)
			}
			smithytesting.AssertJSONEqual(t, c, actual)
		})
	}
}