-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjson_test.go
154 lines (134 loc) · 5.03 KB
/
json_test.go
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
152
153
154
// Copyright 2014-2015 Jakub Matys
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"reflect"
"testing"
)
func TestConvertNumberTypes(t *testing.T) {
testConversionOfScalarValue(t)
testRecursiveConversionOfSlice(t)
testRecursiveConversionOfMap(t)
}
func testConversionOfScalarValue(t *testing.T) {
var (
err error
object interface{}
)
for _, num := range []string{"0.0", ".0", "-4.", "3.14159", "-4.8e-8", "34e15"} {
object = json.Number(num)
err = convertNumberTypes(&object)
if err != nil {
t.Fatalf("Conversion of value \"%s\" failed: %s", num, err)
}
if reflect.TypeOf(object).Kind() != reflect.Float64 {
t.Fatalf("Value \"%s\" was was converted to %s type (float64 expected).",
num, reflect.TypeOf(object))
}
}
for _, num := range []string{"0", "256", "-3", "123456789", "1234567890000"} {
object = json.Number(num)
err = convertNumberTypes(&object)
if err != nil {
t.Fatalf("Conversion of value \"%s\" failed: %s", num, err)
}
if reflect.TypeOf(object).Kind() != reflect.Int64 {
t.Fatalf("Value \"%s\" was was converted to %s type (int64 expected).",
num, reflect.TypeOf(object))
}
}
}
func testRecursiveConversionOfSlice(t *testing.T) {
var object interface{}
object = []interface{}{
"string",
json.Number("1234567890000"),
[]interface{}{
json.Number("256"),
json.Number(".0"),
"string",
},
}
checkType := func(object interface{}, expectedType reflect.Kind) {
if reflect.TypeOf(object).Kind() != expectedType {
t.Fatalf("Type %s of value %v doesn't match expected type %s",
reflect.TypeOf(object), object, expectedType)
}
}
if err := convertNumberTypes(&object); err != nil {
t.Fatalf("Conversion of slice %v failed: %s", object, err)
} else {
switch object := object.(type) {
case []interface{}:
checkType(object[0], reflect.String)
checkType(object[1], reflect.Int64)
checkType(object[2], reflect.Slice)
switch inner := object[2].(type) {
case []interface{}:
checkType(inner[0], reflect.Int64)
checkType(inner[1], reflect.Float64)
checkType(inner[2], reflect.String)
default:
t.Fatalf("Conversion function returned incorrect type %s of inner slice (expected: []interface{})",
reflect.TypeOf(inner))
}
default:
t.Fatalf("Conversion function returned incorrect type %s (expected: []interface{})",
reflect.TypeOf(object))
}
}
}
func testRecursiveConversionOfMap(t *testing.T) {
var object interface{}
object = map[string]interface{}{
"aaa": "string",
"bbb": json.Number("1234567890000"),
"ccc": json.Number("4."),
"ddd": map[string]interface{}{
"xxx": json.Number("256"),
"yyy": json.Number(".0"),
"zzz": "string",
},
}
checkType := func(object interface{}, expectedType reflect.Kind) {
if reflect.TypeOf(object).Kind() != expectedType {
t.Fatalf("Type %s of value %v doesn't match expected type %s",
reflect.TypeOf(object), object, expectedType)
}
}
if err := convertNumberTypes(&object); err != nil {
t.Fatalf("Conversion of map %v failed: %s", object, err)
} else {
switch object := object.(type) {
case map[string]interface{}:
checkType(object["aaa"], reflect.String)
checkType(object["bbb"], reflect.Int64)
checkType(object["ccc"], reflect.Float64)
checkType(object["ddd"], reflect.Map)
switch inner := object["ddd"].(type) {
case map[string]interface{}:
checkType(inner["xxx"], reflect.Int64)
checkType(inner["yyy"], reflect.Float64)
checkType(inner["zzz"], reflect.String)
default:
t.Fatalf("Conversion function returned incorrect type %s of inner map (expected: map[string]interface{})",
reflect.TypeOf(inner))
}
default:
t.Fatalf("Conversion function returned incorrect type %s (expected: map[string]interface{})",
reflect.TypeOf(object))
}
}
}