-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect_test.go
307 lines (282 loc) · 14.1 KB
/
reflect_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
// Copyright (c) 2020-2021 Markku Rossi
//
// All rights reserved.
//
package tabulate
import (
"crypto/x509"
"encoding/pem"
"fmt"
"strings"
"testing"
)
type Outer struct {
Name string
Comment string `tabulate:"@detail"`
Age int
NPS float64
Address *Address `tabulate:"omitempty"`
Info []*Info
Meta *Info `tabulate:"omitempty"`
Mapping map[string]string
}
type Address struct {
Lines []string
}
type Info struct {
Email string
Work bool
}
func reflectTest(flags Flags, tags []string, v interface{}) (string, error) {
tab := New(Unicode)
tab.Header("Field").SetAlign(MR)
tab.Header("Value").SetAlign(ML)
err := Reflect(tab, flags, tags, v)
if err != nil {
return "", err
}
var sb strings.Builder
tab.Print(&sb)
return sb.String(), nil
}
func TestReflect(t *testing.T) {
result, err := reflectTest(OmitEmpty, nil, &Outer{
Name: "Alyssa P. Hacker",
Age: 45,
Address: &Address{
Lines: []string{"42 Hacker way", "03139 Cambridge", "MA"},
},
Info: []*Info{
{
Email: "[email protected]",
},
{
Email: "[email protected]",
Work: true,
},
},
Mapping: map[string]string{
"First": "1st",
"Second": "2nd",
},
})
if err != nil {
t.Fatalf("Reflect failed: %s", err)
}
match(t, result, `
┏━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Field ┃ Value ┃
┡━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ Name │ Alyssa P. Hacker │
│ Age │ 45 │
│ NPS │ 0 │
│ │ ┌───────┬─────────────────┐ │
│ │ │ Lines │ 42 Hacker way │ │
│ Address │ │ │ 03139 Cambridge │ │
│ │ │ │ MA │ │
│ │ └───────┴─────────────────┘ │
│ │ ┌───────┬────────────┐ │
│ │ │ Email │ [email protected] │ │
│ │ │ Work │ false │ │
│ Info │ └───────┴────────────┘ │
│ │ ┌───────┬────────────────────────┐ │
│ │ │ Email │ [email protected] │ │
│ │ │ Work │ true │ │
│ │ └───────┴────────────────────────┘ │
│ │ ┌────────┬─────┐ │
│ Mapping │ │ First │ 1st │ │
│ │ │ Second │ 2nd │ │
│ │ └────────┴─────┘ │
└─────────┴────────────────────────────────────┘
`, "TestReflect")
data := &Outer{
Name: "Alyssa P. Hacker",
Comment: "Structure and Interpretation of Computer Programs",
Age: 45,
Info: []*Info{
nil,
{
Email: "[email protected]",
Work: true,
},
},
}
result, err = reflectTest(OmitEmpty, nil, data)
if err != nil {
t.Fatalf("Reflect failed: %s", err)
}
match(t, result, `
┏━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Field ┃ Value ┃
┡━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ Name │ Alyssa P. Hacker │
│ Age │ 45 │
│ NPS │ 0 │
│ │ ┌───────┬────────────────────────┐ │
│ Info │ │ Email │ [email protected] │ │
│ │ │ Work │ true │ │
│ │ └───────┴────────────────────────┘ │
└───────┴────────────────────────────────────┘
`, "TestReflect 2")
result, err = reflectTest(0, []string{"detail"}, data)
if err != nil {
t.Fatalf("Reflect failed: %s", err)
}
match(t, result, `
┏━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Field ┃ Value ┃
┡━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ Name │ Alyssa P. Hacker │
│ Comment │ Structure and Interpretation of Computer Programs │
│ Age │ 45 │
│ NPS │ 0 │
│ │ <nil> │
│ │ ┌───────┬────────────────────────┐ │
│ Info │ │ Email │ [email protected] │ │
│ │ │ Work │ true │ │
│ │ └───────┴────────────────────────┘ │
│ Mapping │ │
└─────────┴───────────────────────────────────────────────────┘
`, "TestReflect 3")
}
type Outer2 struct {
Name string
Inner *Inner
Certificate *Certificate
}
type Inner struct {
A int
B int
}
func (in Inner) MarshalText() (text []byte, err error) {
return []byte(fmt.Sprintf("A=%v, B=%v", in.A, in.B)), nil
}
type Certificate struct {
X509 *x509.Certificate
}
func (c Certificate) MarshalText() (text []byte, err error) {
block := &pem.Block{
Type: "CERTIFICATE",
Bytes: c.X509.Raw,
}
return pem.EncodeToMemory(block), nil
}
const certPEM = `-----BEGIN CERTIFICATE-----
MIIFezCCBGOgAwIBAgIQBXgQRGTO9sioGXkO2OhJWjANBgkqhkiG9w0BAQsFADBG
MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRUwEwYDVQQLEwxTZXJ2ZXIg
Q0EgMUIxDzANBgNVBAMTBkFtYXpvbjAeFw0yMDA1MTQwMDAwMDBaFw0yMTA2MTQx
MjAwMDBaMCQxIjAgBgNVBAMTGXByaX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oQxZqjlQfsAwm
j1kDPHlxbvfM2QgrEdFORfMCIQDrHoTNJA/mMqLqjUN1VicPKTCL+jXewGBCNZfV
f+melQB1AFzcQ5L+5qtFRLFemtRW5hA3+9X6R9yhc5SyXub2xw7KAAABchCy4TcA
AAQDAEYwRAIgLh9u1+jarU2ombS6PpGU3fb/UuSSmvcI5VpW4uRpPtUCIDnYvE5+
hnfnkgimPfCnPpfycgDmfRUiO/eG1Avh6xgkMA0GCSqGSIb3DQEBCwUAA4IBAQB+
ntJ6DNnhoiN/kYCDI7+emL9jZ3aZTmC+OPCATZtCAPgNOViv8+kFldYsc3FQZEs4
Nd9lxVj78Pg50Lv0gcRIUkxyIum7bMRG7gl9Cc8A3yPSGdfATIpXccHswi2Yf3JT
GlMuxx+KS/4ixewB77PucbMwqYKJKOEXIowhwL31fNYCxY2X5fPFZCIVI3f+tjJb
BsMnFsKUVVbjsrO7qRs/k7p1JwxKuxc1FjK3TPxrD8j6zs1tc3xDhkB/8TDXTdEm
n9hAr+Ljvi20fbmWITdiqD+1i2Ue6Gwj1+l1EtFUUjFiKYGvxy2sksfNdNDrY1bo
iEk07xyUePVXINXPEnOQ
-----END CERTIFICATE-----
`
func decodeCertificate() (*x509.Certificate, error) {
block, _ := pem.Decode([]byte(certPEM))
if block == nil {
return nil, fmt.Errorf("PEM decode failed")
}
return x509.ParseCertificate(block.Bytes)
}
func TestReflectTextMarshaler(t *testing.T) {
c, err := decodeCertificate()
if err != nil {
t.Fatalf("failed to decode certificate: %s", err)
}
result, err := reflectTest(0, nil, &Outer2{
Name: "ACME Corp.",
Inner: &Inner{
A: 100,
B: 42,
},
Certificate: &Certificate{
X509: c,
},
})
if err != nil {
t.Fatalf("Reflect failed: %s", err)
}
match(t, result, `
┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Field ┃ Value ┃
┡━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ Name │ ACME Corp. │
│ Inner │ A=100, B=42 │
│ │ -----BEGIN CERTIFICATE----- │
│ │ MIIFezCCBGOgAwIBAgIQBXgQRGTO9sioGXkO2OhJWjANBgkqhkiG9w0BAQsFADBG │
│ │ MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRUwEwYDVQQLEwxTZXJ2ZXIg │
│ │ Q0EgMUIxDzANBgNVBAMTBkFtYXpvbjAeFw0yMDA1MTQwMDAwMDBaFw0yMTA2MTQx │
│ │ MjAwMDBaMCQxIjAgBgNVBAMTGXByaXZ4ZGVtby5zc2guZW5naW5lZXJpbmcwggEi │
│ │ MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDj0lLHarwcvVqy33Z4Xb1lM1wM │
│ │ dQpFrc6Sa7L4x4F1H6KGKg9YLZaQGFFu/m7fq01cGhkFXn2A2AEmlcUk0G3Ul/BH │
│ │ irUV91QBkUu2TKqpcOSLeI9JuczOjvVGI5IwJShAWPC7fqHjquKlInX7zlr6KscB │
│ │ r69DiYhoU+rd1sV3Jkimlj1XT/r5VQ6UK3FLXBbhKpDdiNQkRzWqbGLDgXr65VJd │
│ │ UYiatvzxcTOUevwOCEgOaGip0N7jkcXxnG+y5BmXxkLGhumZ24gL6en4ZL2HeQlw │
│ │ kAxnDZb86MnebJxlwdo1ck93qFmz012uqEntRE7x06E8HL0y7dOb8Mv6+RcpAgMB │
│ │ AAGjggKFMIICgTAfBgNVHSMEGDAWgBRZpGYGUqB7lZI8o5QHJ5Z0W/k90DAdBgNV │
│ │ HQ4EFgQUkymG/chDUXc6cmPFimvQX0ursnQwJAYDVR0RBB0wG4IZcHJpdnhkZW1v │
│ │ LnNzaC5lbmdpbmVlcmluZzAOBgNVHQ8BAf8EBAMCBaAwHQYDVR0lBBYwFAYIKwYB │
│ │ BQUHAwEGCCsGAQUFBwMCMDsGA1UdHwQ0MDIwMKAuoCyGKmh0dHA6Ly9jcmwuc2Nh │
│ Certificate │ MWIuYW1hem9udHJ1c3QuY29tL3NjYTFiLmNybDAgBgNVHSAEGTAXMAsGCWCGSAGG │
│ │ /WwBAjAIBgZngQwBAgEwdQYIKwYBBQUHAQEEaTBnMC0GCCsGAQUFBzABhiFodHRw │
│ │ Oi8vb2NzcC5zY2ExYi5hbWF6b250cnVzdC5jb20wNgYIKwYBBQUHMAKGKmh0dHA6 │
│ │ Ly9jcnQuc2NhMWIuYW1hem9udHJ1c3QuY29tL3NjYTFiLmNydDAMBgNVHRMBAf8E │
│ │ AjAAMIIBBAYKKwYBBAHWeQIEAgSB9QSB8gDwAHcAfT7y+I//iFVoJMLAyp5SiXkr │
│ │ xQ54CX8uapdomX4i8NcAAAFyELLhaAAABAMASDBGAiEAh9jeGd6oQxZqjlQfsAwm │
│ │ j1kDPHlxbvfM2QgrEdFORfMCIQDrHoTNJA/mMqLqjUN1VicPKTCL+jXewGBCNZfV │
│ │ f+melQB1AFzcQ5L+5qtFRLFemtRW5hA3+9X6R9yhc5SyXub2xw7KAAABchCy4TcA │
│ │ AAQDAEYwRAIgLh9u1+jarU2ombS6PpGU3fb/UuSSmvcI5VpW4uRpPtUCIDnYvE5+ │
│ │ hnfnkgimPfCnPpfycgDmfRUiO/eG1Avh6xgkMA0GCSqGSIb3DQEBCwUAA4IBAQB+ │
│ │ ntJ6DNnhoiN/kYCDI7+emL9jZ3aZTmC+OPCATZtCAPgNOViv8+kFldYsc3FQZEs4 │
│ │ Nd9lxVj78Pg50Lv0gcRIUkxyIum7bMRG7gl9Cc8A3yPSGdfATIpXccHswi2Yf3JT │
│ │ GlMuxx+KS/4ixewB77PucbMwqYKJKOEXIowhwL31fNYCxY2X5fPFZCIVI3f+tjJb │
│ │ BsMnFsKUVVbjsrO7qRs/k7p1JwxKuxc1FjK3TPxrD8j6zs1tc3xDhkB/8TDXTdEm │
│ │ n9hAr+Ljvi20fbmWITdiqD+1i2Ue6Gwj1+l1EtFUUjFiKYGvxy2sksfNdNDrY1bo │
│ │ iEk07xyUePVXINXPEnOQ │
│ │ -----END CERTIFICATE----- │
└─────────────┴──────────────────────────────────────────────────────────────────┘
`, "TestReflectTextMarshaler")
}
func TestReflectArray(t *testing.T) {
tab, err := Array(New(ASCII), [][]interface{}{
{"a", "b", "c"},
{"1", "2", "3"},
})
if err != nil {
t.Fatalf("Array failed: %s", err)
}
var sb strings.Builder
tab.Print(&sb)
expected := `
+---+---+---+
| a | b | c |
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
`
match(t, sb.String(), expected, "TestReflectArray")
}