-
Notifications
You must be signed in to change notification settings - Fork 2
/
float64_test.go
74 lines (69 loc) · 1.46 KB
/
float64_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
package easy
import (
"encoding/json"
"reflect"
"testing"
)
var float64sMarshalJSONTests = []struct {
float64s Float64s
want string
}{
{
float64s: nil,
want: `null`,
},
{
float64s: Float64s{},
want: `[]`,
},
{
float64s: Float64s{1.23, 0, -2.45, 1.4345531263871263},
want: `["1.23","0","-2.45","1.4345531263871263"]`,
},
}
func TestFloat64s_MarshalJSON(t *testing.T) {
for _, test := range float64sMarshalJSONTests {
got, err := json.Marshal(test.float64s)
if err != nil {
t.Errorf("error occurs while json.Marshal")
continue
}
if !reflect.DeepEqual(string(got), test.want) {
t.Errorf("(%v).MarshalJSON = %v, want %v", test.float64s, string(got), test.want)
}
}
}
var float64sUnmarshalJSONTests = []struct {
json string
want Float64s
}{
{
json: `null`,
want: nil,
},
{
json: `[]`,
want: Float64s{},
},
{
json: `["1.23","0","-2.45","1.4345531263871263"]`,
want: Float64s{1.23, 0, -2.45, 1.4345531263871263},
},
{
json: `["1.23",0,-2.45,"1.4345531263871263"]`,
want: Float64s{1.23, 0, -2.45, 1.4345531263871263},
},
}
func TestFloat64s_UnmarshalJSON(t *testing.T) {
for _, test := range float64sUnmarshalJSONTests {
var float64s Float64s
err := json.Unmarshal([]byte(test.json), &float64s)
if err != nil {
t.Errorf("error occurs while json.Marshal")
continue
}
if !reflect.DeepEqual(float64s, test.want) {
t.Errorf("(%v).UnmarshalJSON = %v, want %v", test.json, float64s, test.want)
}
}
}