File: encoder_test.go

package info (click to toggle)
golang-github-aws-smithy-go 1.23.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,520 kB
  • sloc: java: 21,276; sh: 131; makefile: 90; xml: 9
file content (114 lines) | stat: -rw-r--r-- 2,370 bytes parent folder | download
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package json_test

import (
	"reflect"
	"testing"
	"time"

	"github.com/aws/smithy-go/document"
	"github.com/aws/smithy-go/document/json"
)

func TestEncoder_Encode(t *testing.T) {
	t.Run("Object", func(t *testing.T) {
		for name, tt := range sharedObjectTests {
			t.Run(name, func(t *testing.T) {
				testEncode(t, tt)
			})
		}
	})
	t.Run("Array", func(t *testing.T) {
		for name, tt := range sharedArrayTestCases {
			t.Run(name, func(t *testing.T) {
				testEncode(t, tt)
			})
		}
	})
	t.Run("Number", func(t *testing.T) {
		for name, tt := range sharedNumberTestCases {
			t.Run(name, func(t *testing.T) {
				testEncode(t, tt)
			})
		}
	})
	t.Run("String", func(t *testing.T) {
		for name, tt := range sharedStringTests {
			t.Run(name, func(t *testing.T) {
				testEncode(t, tt)
			})
		}
	})
}

func TestNewEncoderUnsupportedTypes(t *testing.T) {
	type customTime time.Time
	type noSerde = document.NoSerde
	type NestedThing struct {
		SomeThing string
		noSerde
	}
	type Thing struct {
		OtherThing  string
		NestedThing NestedThing
	}

	cases := []interface{}{
		time.Now().UTC(),
		customTime(time.Now().UTC()),
		Thing{OtherThing: "foo", NestedThing: NestedThing{SomeThing: "bar"}},
	}

	encoder := json.NewEncoder()
	for _, tt := range cases {
		_, err := encoder.Encode(tt)
		if err == nil {
			t.Errorf("expect error, got nil")
		}
	}
}

func TestDeterministicMapOrder(t *testing.T) {
	value := struct {
		Map map[string]string
	}{
		Map: map[string]string{
			"foo": "bar",
			"a":   "b",
			"bar": "baz",
			"b":   "c",
			"baz": "qux",
			"c":   "d",
		},
	}
	expect := `{"Map":{"a":"b","b":"c","bar":"baz","baz":"qux","c":"d","foo":"bar"}}`

	encoder := json.NewEncoder()
	actual, err := encoder.Encode(value)
	if err != nil {
		t.Fatal(err)
	}

	if expect != string(actual) {
		t.Errorf("encode determinstic order:\n%q !=\n%q", expect, actual)
	}
}

func testEncode(t *testing.T, tt testCase) {
	t.Helper()

	e := json.NewEncoder(func(options *json.EncoderOptions) {
		*options = tt.encoderOptions
	})

	encodeBytes, err := e.Encode(tt.actual)
	if (err != nil) != tt.wantErr {
		t.Errorf("Encode() error = %v, wantErr %v", err, tt.wantErr)
	}

	expect := MustJSONUnmarshal(tt.json, !tt.disableJSONNumber)
	got := MustJSONUnmarshal(encodeBytes, !tt.disableJSONNumber)

	if !reflect.DeepEqual(expect, got) {
		t.Errorf("%v != %v", expect, got)
	}
}