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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
package template
import (
"bytes"
"fmt"
"io"
"log"
"os"
"strings"
"testing"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/cli/go-gh/v2/pkg/text"
"github.com/stretchr/testify/assert"
)
func ExampleTemplate() {
// Information about the terminal can be obtained using the [pkg/term] package.
colorEnabled := true
termWidth := 14
json := strings.NewReader(heredoc.Doc(`[
{"number": 1, "title": "One"},
{"number": 2, "title": "Two"}
]`))
template := "HEADER\n\n{{range .}}{{tablerow .number .title}}{{end}}{{tablerender}}\nFOOTER"
tmpl := New(os.Stdout, termWidth, colorEnabled)
if err := tmpl.Parse(template); err != nil {
log.Fatal(err)
}
if err := tmpl.Execute(json); err != nil {
log.Fatal(err)
}
// Output:
// HEADER
//
// 1 One
// 2 Two
//
// FOOTER
}
func ExampleTemplate_Funcs() {
// Information about the terminal can be obtained using the [pkg/term] package.
colorEnabled := true
termWidth := 14
json := strings.NewReader(heredoc.Doc(`[
{"num": 1, "thing": "apple"},
{"num": 2, "thing": "orange"}
]`))
template := "{{range .}}* {{pluralize .num .thing}}\n{{end}}"
tmpl := New(os.Stdout, termWidth, colorEnabled)
tmpl.Funcs(map[string]interface{}{
"pluralize": func(fields ...interface{}) (string, error) {
if l := len(fields); l != 2 {
return "", fmt.Errorf("wrong number of args for pluralize: want 2 got %d", l)
}
var ok bool
var num float64
var thing string
if num, ok = fields[0].(float64); !ok && num == float64(int(num)) {
return "", fmt.Errorf("invalid value; expected int")
}
if thing, ok = fields[1].(string); !ok {
return "", fmt.Errorf("invalid value; expected string")
}
return text.Pluralize(int(num), thing), nil
},
})
if err := tmpl.Parse(template); err != nil {
log.Fatal(err)
}
if err := tmpl.Execute(json); err != nil {
log.Fatal(err)
}
// Output:
// * 1 apple
// * 2 oranges
}
func TestJsonScalarToString(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 tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
func TestExecute(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"}}`,
},
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"}}`,
},
wantW: "go",
},
{
name: "timefmt",
args: args{
json: strings.NewReader(`{"created_at":"2008-02-25T20:18:33Z"}`),
template: `{{.created_at | timefmt "Mon Jan 2, 2006"}}`,
},
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}}`,
},
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}}`,
},
wantW: "bug\nfeature request\nchore\n",
},
{
name: "join",
args: args{
json: strings.NewReader(`[ "bug", "feature request", "chore" ]`),
template: `{{join "\t" .}}`,
},
wantW: "bug\tfeature request\tchore",
},
{
name: "table",
args: args{
json: strings.NewReader(heredoc.Doc(`[
{"number": 1, "title": "One"},
{"number": 20, "title": "Twenty"},
{"number": 3000, "title": "Three thousand"}
]`)),
template: `{{range .}}{{tablerow (.number | printf "#%v") .title}}{{end}}`,
},
wantW: heredoc.Doc(`#1 One
#20 Twenty
#3000 Three thousand
`),
},
{
name: "table with multiline text",
args: args{
json: strings.NewReader(heredoc.Doc(`[
{"number": 1, "title": "One\ranother line of text"},
{"number": 20, "title": "Twenty\nanother line of text"},
{"number": 3000, "title": "Three thousand\r\nanother line of text"}
]`)),
template: `{{range .}}{{tablerow (.number | printf "#%v") .title}}{{end}}`,
},
wantW: heredoc.Doc(`#1 One...
#20 Twenty...
#3000 Three thousand...
`),
},
{
name: "table with mixed value types",
args: args{
json: strings.NewReader(heredoc.Doc(`[
{"number": 1, "title": null, "float": false},
{"number": 20.1, "title": "Twenty-ish", "float": true},
{"number": 3000, "title": "Three thousand", "float": false}
]`)),
template: `{{range .}}{{tablerow .number .title .float}}{{end}}`,
},
wantW: heredoc.Doc(`1 false
20.10 Twenty-ish true
3000 Three thousand false
`),
},
{
name: "table with color",
args: args{
json: strings.NewReader(heredoc.Doc(`[
{"number": 1, "title": "One"}
]`)),
template: `{{range .}}{{tablerow (.number | color "green") .title}}{{end}}`,
},
wantW: "\x1b[0;32m1\x1b[0m One\n",
},
{
name: "table with header and footer",
args: args{
json: strings.NewReader(heredoc.Doc(`[
{"number": 1, "title": "One"},
{"number": 2, "title": "Two"}
]`)),
template: heredoc.Doc(`HEADER
{{range .}}{{tablerow .number .title}}{{end}}FOOTER
`),
},
wantW: heredoc.Doc(`HEADER
FOOTER
1 One
2 Two
`),
},
{
name: "table with header and footer using endtable",
args: args{
json: strings.NewReader(heredoc.Doc(`[
{"number": 1, "title": "One"},
{"number": 2, "title": "Two"}
]`)),
template: heredoc.Doc(`HEADER
{{range .}}{{tablerow .number .title}}{{end}}{{tablerender}}FOOTER
`),
},
wantW: heredoc.Doc(`HEADER
1 One
2 Two
FOOTER
`),
},
{
name: "multiple tables with different columns",
args: args{
json: strings.NewReader(heredoc.Doc(`{
"issues": [
{"number": 1, "title": "One"},
{"number": 2, "title": "Two"}
],
"prs": [
{"number": 3, "title": "Three", "reviewDecision": "REVIEW_REQUESTED"},
{"number": 4, "title": "Four", "reviewDecision": "CHANGES_REQUESTED"}
]
}`)),
template: heredoc.Doc(`{{tablerow "ISSUE" "TITLE"}}{{range .issues}}{{tablerow .number .title}}{{end}}{{tablerender}}
{{tablerow "PR" "TITLE" "DECISION"}}{{range .prs}}{{tablerow .number .title .reviewDecision}}{{end}}`),
},
wantW: heredoc.Docf(`ISSUE TITLE
1 One
2 Two
PR TITLE DECISION
3 Three REVIEW_REQUESTED
4 Four CHANGES_REQUESTED
`),
},
{
name: "truncate",
args: args{
json: strings.NewReader(`{"title": "This is a long title"}`),
template: `{{truncate 13 .title}}`,
},
wantW: "This is a ...",
},
{
name: "truncate with JSON null",
args: args{
json: strings.NewReader(`{}`),
template: `{{ truncate 13 .title }}`,
},
wantW: "",
},
{
name: "truncate with piped JSON null",
args: args{
json: strings.NewReader(`{}`),
template: `{{ .title | truncate 13 }}`,
},
wantW: "",
},
{
name: "truncate with piped JSON null in parenthetical",
args: args{
json: strings.NewReader(`{}`),
template: `{{ (.title | truncate 13) }}`,
},
wantW: "",
},
{
name: "truncate invalid type",
args: args{
json: strings.NewReader(`{"title": 42}`),
template: `{{ (.title | truncate 13) }}`,
},
wantErr: true,
},
{
name: "hyperlink enabled",
args: args{
json: strings.NewReader(`{"link":"https://github.com"}`),
template: `{{ hyperlink .link "" }}`,
},
wantW: "\x1b]8;;https://github.com\x1b\\https://github.com\x1b]8;;\x1b\\",
},
{
name: "hyperlink with text enabled",
args: args{
json: strings.NewReader(`{"link":"https://github.com","text":"GitHub"}`),
template: `{{ hyperlink .link .text }}`,
},
wantW: "\x1b]8;;https://github.com\x1b\\GitHub\x1b]8;;\x1b\\",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
tmpl := New(w, 80, tt.args.colorize)
err := tmpl.Parse(tt.args.template)
assert.NoError(t, err)
err = tmpl.Execute(tt.args.json)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
err = tmpl.Flush()
assert.NoError(t, err)
assert.Equal(t, tt.wantW, w.String())
})
}
}
func TestTruncateMultiline(t *testing.T) {
type args struct {
max int
s string
}
tests := []struct {
name string
args args
want string
}{
{
name: "exactly minimum width",
args: args{
max: 5,
s: "short",
},
want: "short",
},
{
name: "exactly minimum width with new line",
args: args{
max: 5,
s: "short\n",
},
want: "sh...",
},
{
name: "less than minimum width",
args: args{
max: 4,
s: "short",
},
want: "shor",
},
{
name: "less than minimum width with new line",
args: args{
max: 4,
s: "short\n",
},
want: "shor",
},
{
name: "first line of multiple is short enough",
args: args{
max: 80,
s: "short\n\nthis is a new line",
},
want: "short...",
},
{
name: "using Windows line endings",
args: args{
max: 80,
s: "short\r\n\r\nthis is a new line",
},
want: "short...",
},
{
name: "using older MacOS line endings",
args: args{
max: 80,
s: "short\r\rthis is a new line",
},
want: "short...",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := truncateMultiline(tt.args.max, tt.args.s)
assert.Equal(t, tt.want, got)
})
}
}
func TestFuncs(t *testing.T) {
w := &bytes.Buffer{}
tmpl := New(w, 80, false)
// Override "truncate" and define a new "foo" function.
tmpl.Funcs(map[string]interface{}{
"truncate": func(fields ...interface{}) (string, error) {
if l := len(fields); l != 2 {
return "", fmt.Errorf("wrong number of args for truncate: want 2 got %d", l)
}
var ok bool
var width int
var input string
if width, ok = fields[0].(int); !ok {
return "", fmt.Errorf("invalid value; expected int")
}
if input, ok = fields[1].(string); !ok {
return "", fmt.Errorf("invalid value; expected string")
}
return input[:width], nil
},
"foo": func(fields ...interface{}) (string, error) {
return "test", nil
},
})
err := tmpl.Parse(`{{ .text | truncate 5 }} {{ .status | color "green" }} {{ foo }}`)
assert.NoError(t, err)
r := strings.NewReader(`{"text":"truncated","status":"open"}`)
err = tmpl.Execute(r)
assert.NoError(t, err)
err = tmpl.Flush()
assert.NoError(t, err)
assert.Equal(t, "trunc \x1b[0;32mopen\x1b[0m test", w.String())
}
|