-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer_test.go
132 lines (114 loc) · 2.36 KB
/
renderer_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
package human
import (
"strings"
"testing"
"time"
)
func MustRender(t *testing.T, value any) string {
str, err := Render(value)
if err != nil {
t.Fatalf("rendering failed: %s", err)
}
return str
}
func AssertRender(t *testing.T, value any, expected string) {
str := MustRender(t, value)
if str == "" && expected == "" {
return
}
if str != expected+"\n" {
t.Fatalf("expected %q, got %q", expected, str)
}
}
func lines(lines ...string) string {
return strings.Join(lines, "\n")
}
func TestNil(t *testing.T) {
AssertRender(t, nil, "")
}
func TestBool(t *testing.T) {
AssertRender(t, false, "false")
AssertRender(t, true, "true")
}
func TestString(t *testing.T) {
AssertRender(t, "hello", "hello")
}
func TestNumbers(t *testing.T) {
AssertRender(t, 42, "42")
AssertRender(t, 3.14, "3.14")
AssertRender(t, -100, "-100")
}
func TestTime(t *testing.T) {
now := time.Now()
AssertRender(t, now, now.String())
}
func TestDuration(t *testing.T) {
AssertRender(t, time.Second, "1s")
AssertRender(t, time.Minute, "1m0s")
AssertRender(t, time.Hour, "1h0m0s")
}
func TestStruct(t *testing.T) {
// Simple struct
type A struct {
A int
Bb string
}
AssertRender(t, A{42, "hello"}, lines(
" A : 42 ",
"Bb : hello",
))
// Nil field
type B struct {
A int
Nil *int
}
AssertRender(t, B{42, nil}, lines(
"A : 42",
))
// Skip field
type C struct {
A int
B bool `human:"skip-field"`
}
AssertRender(t, C{42, true}, lines(
"A : 42",
))
// Unexported field
type D struct {
A int
b bool
}
AssertRender(t, D{42, true}, lines(
"A : 42",
))
type E struct {
B int
Cdef string
}
type F struct {
A E
}
AssertRender(t, F{E{42, "hello"}}, lines(
"A : B : 42 ",
" Cdef : hello",
" ",
))
}
func TestSlice(t *testing.T) {
AssertRender(t, []int{1, 2, 3}, lines("1", "2", "3"))
AssertRender(t, []bool{true, false}, lines("true", "false"))
now := time.Now()
nowStr := now.String()
AssertRender(t, []time.Time{now, now}, lines(nowStr, nowStr))
type A struct {
Alligator int `human:"skip-column"` // Skip column
Beaver string // Simple
Camel bool `human:"skip-field"` // Ignored in a column context
duck time.Time // Unexported
}
AssertRender(t, []A{{1, "hello", true, now}, {2, "world", false, now}}, lines(
"BEAVER CAMEL ",
"hello true ",
"world false ",
))
}