File: unmarshal_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.49.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312,636 kB
  • sloc: makefile: 120
file content (151 lines) | stat: -rw-r--r-- 3,743 bytes parent folder | download | duplicates (2)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//go:build go1.7
// +build go1.7

package jsonutil_test

import (
	"bytes"
	"math"
	"reflect"
	"testing"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/private/protocol/json/jsonutil"
)

func TestUnmarshalJSON_JSONNumber(t *testing.T) {
	type input struct {
		TimeField  *time.Time `locationName:"timeField"`
		IntField   *int64     `locationName:"intField"`
		FloatField *float64   `locationName:"floatField"`
	}

	cases := map[string]struct {
		JSON       string
		Value      input
		Expected   input
		ExpectedFn func(*testing.T, input)
	}{
		"seconds precision": {
			JSON: `{"timeField":1597094942}`,
			Expected: input{
				TimeField: func() *time.Time {
					dt := time.Date(2020, 8, 10, 21, 29, 02, 00, time.UTC)
					return &dt
				}(),
			},
		},
		"exact milliseconds precision": {
			JSON: `{"timeField":1597094942.123}`,
			Expected: input{
				TimeField: func() *time.Time {
					dt := time.Date(2020, 8, 10, 21, 29, 02, int(123*time.Millisecond), time.UTC)
					return &dt
				}(),
			},
		},
		"microsecond precision truncated": {
			JSON: `{"timeField":1597094942.1235}`,
			Expected: input{
				TimeField: func() *time.Time {
					dt := time.Date(2020, 8, 10, 21, 29, 02, int(123*time.Millisecond), time.UTC)
					return &dt
				}(),
			},
		},
		"nanosecond precision truncated": {
			JSON: `{"timeField":1597094942.123456789}`,
			Expected: input{
				TimeField: func() *time.Time {
					dt := time.Date(2020, 8, 10, 21, 29, 02, int(123*time.Millisecond), time.UTC)
					return &dt
				}(),
			},
		},
		"milliseconds precision as small exponent": {
			JSON: `{"timeField":1.597094942123e9}`,
			Expected: input{
				TimeField: func() *time.Time {
					dt := time.Date(2020, 8, 10, 21, 29, 02, int(123*time.Millisecond), time.UTC)
					return &dt
				}(),
			},
		},
		"milliseconds precision as large exponent": {
			JSON: `{"timeField":1.597094942123E9}`,
			Expected: input{
				TimeField: func() *time.Time {
					dt := time.Date(2020, 8, 10, 21, 29, 02, int(123*time.Millisecond), time.UTC)
					return &dt
				}(),
			},
		},
		"milliseconds precision as exponent with sign": {
			JSON: `{"timeField":1.597094942123e+9}`,
			Expected: input{
				TimeField: func() *time.Time {
					dt := time.Date(2020, 8, 10, 21, 29, 02, int(123*time.Millisecond), time.UTC)
					return &dt
				}(),
			},
		},
		"integer field": {
			JSON: `{"intField":123456789}`,
			Expected: input{
				IntField: aws.Int64(123456789),
			},
		},
		"integer field truncated": {
			JSON: `{"intField":123456789.123}`,
			Expected: input{
				IntField: aws.Int64(123456789),
			},
		},
		"float64 field": {
			JSON: `{"floatField":123456789.123}`,
			Expected: input{
				FloatField: aws.Float64(123456789.123),
			},
		},
		"float64 field NaN": {
			JSON: `{"floatField":"NaN"}`,
			ExpectedFn: func(t *testing.T, input input) {
				if input.FloatField == nil {
					t.Fatal("expect non nil float64")
				}
				if e, a := true, math.IsNaN(*input.FloatField); e != a {
					t.Errorf("expect %v, got %v", e, a)
				}
			},
		},
		"float64 field Infinity": {
			JSON: `{"floatField":"Infinity"}`,
			Expected: input{
				FloatField: aws.Float64(math.Inf(1)),
			},
		},
		"float64 field -Infinity": {
			JSON: `{"floatField":"-Infinity"}`,
			Expected: input{
				FloatField: aws.Float64(math.Inf(-1)),
			},
		},
	}

	for name, tt := range cases {
		t.Run(name, func(t *testing.T) {
			err := jsonutil.UnmarshalJSON(&tt.Value, bytes.NewReader([]byte(tt.JSON)))
			if err != nil {
				t.Errorf("expect no error, got %v", err)
			}
			if tt.ExpectedFn != nil {
				tt.ExpectedFn(t, tt.Value)
				return
			}
			if e, a := tt.Expected, tt.Value; !reflect.DeepEqual(e, a) {
				t.Errorf("expect %v, got %v", e, a)
			}
		})
	}
}