forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_test.go
More file actions
163 lines (159 loc) · 3.26 KB
/
template_test.go
File metadata and controls
163 lines (159 loc) · 3.26 KB
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
package export
import (
"bytes"
"fmt"
"io"
"strings"
"testing"
"time"
"github.com/MakeNowJust/heredoc"
)
func Test_jsonScalarToString(t *testing.T) {
tests := []struct {
name string
input interface{}
want string
wantErr bool
}{
{
name: "string",
input: "hello",
want: "hello",
},
{
name: "int",
input: float64(1234),
want: "1234",
},
{
name: "float",
input: float64(12.34),
want: "12.34",
},
{
name: "null",
input: nil,
want: "",
},
{
name: "true",
input: true,
want: "true",
},
{
name: "false",
input: false,
want: "false",
},
{
name: "object",
input: map[string]interface{}{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := jsonScalarToString(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("jsonScalarToString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("jsonScalarToString() = %v, want %v", got, tt.want)
}
})
}
}
func Test_executeTemplate(t *testing.T) {
type args struct {
json io.Reader
template string
colorize bool
}
tests := []struct {
name string
args args
wantW string
wantErr bool
}{
{
name: "color",
args: args{
json: strings.NewReader(`{}`),
template: `{{color "blue+h" "songs are like tattoos"}}`,
colorize: false,
},
wantW: "\x1b[0;94msongs are like tattoos\x1b[0m",
},
{
name: "autocolor enabled",
args: args{
json: strings.NewReader(`{}`),
template: `{{autocolor "red" "stop"}}`,
colorize: true,
},
wantW: "\x1b[0;31mstop\x1b[0m",
},
{
name: "autocolor disabled",
args: args{
json: strings.NewReader(`{}`),
template: `{{autocolor "red" "go"}}`,
colorize: false,
},
wantW: "go",
},
{
name: "timefmt",
args: args{
json: strings.NewReader(`{"created_at":"2008-02-25T20:18:33Z"}`),
template: `{{.created_at | timefmt "Mon Jan 2, 2006"}}`,
colorize: false,
},
wantW: "Mon Feb 25, 2008",
},
{
name: "timeago",
args: args{
json: strings.NewReader(fmt.Sprintf(`{"created_at":"%s"}`, time.Now().Add(-5*time.Minute).Format(time.RFC3339))),
template: `{{.created_at | timeago}}`,
colorize: false,
},
wantW: "5 minutes ago",
},
{
name: "pluck",
args: args{
json: strings.NewReader(heredoc.Doc(`[
{"name": "bug"},
{"name": "feature request"},
{"name": "chore"}
]`)),
template: `{{range(pluck "name" .)}}{{. | printf "%s\n"}}{{end}}`,
colorize: false,
},
wantW: "bug\nfeature request\nchore\n",
},
{
name: "join",
args: args{
json: strings.NewReader(`[ "bug", "feature request", "chore" ]`),
template: `{{join "\t" .}}`,
colorize: false,
},
wantW: "bug\tfeature request\tchore",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
if err := ExecuteTemplate(w, tt.args.json, tt.args.template, tt.args.colorize); (err != nil) != tt.wantErr {
t.Errorf("executeTemplate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotW := w.String(); gotW != tt.wantW {
t.Errorf("executeTemplate() = %q, want %q", gotW, tt.wantW)
}
})
}
}