-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
138 lines (128 loc) · 2.61 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
//
// Copyright (c) 2020-2021 Markku Rossi
//
// All rights reserved.
//
package tabulate
import (
"encoding/json"
"fmt"
"testing"
)
func TestJSONTimeSeries(t *testing.T) {
rows := `Year,Income,Expenses
2018,100,90
2019,110,85
2020,107,50`
tab := tabulate(New(Plain), TL, rows)
data, err := json.MarshalIndent(tab, "", " ")
if err != nil {
t.Fatalf("JSON marshal time series failed: %s", err)
}
expected := `
{
"2018": [
"100",
"90"
],
"2019": [
"110",
"85"
],
"2020": [
"107",
"50"
]
}
`
match(t, string(data), expected, "TestJSONTimeSeries")
}
func TestJSONReflect(t *testing.T) {
tab := New(Plain)
tab.Header("Field")
tab.Header("Value")
err := Reflect(tab, OmitEmpty, nil, &Outer{
Name: "Alyssa P. Hacker",
Age: 45,
NPS: 9.9,
Address: &Address{
Lines: []string{"42 Hacker way", "03139 Cambridge", "MA"},
},
Info: []*Info{
{
Email: "[email protected]",
},
{
Email: "[email protected]",
Work: true,
},
},
Meta: &Info{
Email: "[email protected]",
},
Mapping: map[string]string{
"First": "1st",
"Second": "2nd",
},
})
if err != nil {
t.Fatalf("Reflect failed: %s", err)
}
data, err := json.MarshalIndent(tab, "", " ")
if err != nil {
t.Fatalf("JSON marshal reflect failed: %s", err)
}
expected := `
{
"Address": {
"Lines": [
"42 Hacker way",
"03139 Cambridge",
"MA"
]
},
"Age": 45,
"Info": [
{
"Email": "[email protected]",
"Work": false
},
{
"Email": "[email protected]",
"Work": true
}
],
"Mapping": {
"First": "1st",
"Second": "2nd"
},
"Meta": {
"Email": "[email protected]",
"Work": false
},
"NPS": 9.9,
"Name": "Alyssa P. Hacker"
}
`
match(t, string(data), expected, "TestJSONReflect")
}
func TestJSONCertReflect(t *testing.T) {
c, err := decodeCertificate()
if err != nil {
t.Fatalf("Failed to decode certificate: %s", err)
}
tab := New(Plain)
tab.Header("Field")
tab.Header("Value")
err = Reflect(tab, OmitEmpty, nil, c)
if err != nil {
t.Fatalf("Reflect failed: %s", err)
}
data, err := json.MarshalIndent(tab, "", " ")
if err != nil {
t.Fatalf("JSON marshal cert reflect failed: %s", err)
}
if false {
fmt.Printf("JSON marshal cert reflect:\n%s\n", string(data))
}
}