MCPcopy
hub / github.com/urfave/cli / TestShowCommandHelp_HelpPrinterCustom

Function TestShowCommandHelp_HelpPrinterCustom

help_test.go:575–653  ·  help_test.go::TestShowCommandHelp_HelpPrinterCustom
(t *testing.T)

Source from the content-addressed store, hash-verified

573}
574
575func TestShowCommandHelp_HelpPrinterCustom(t *testing.T) {
576 doublecho := func(text string) string {
577 return text + " " + text
578 }
579
580 testCases := []struct {
581 name string
582 template string
583 printer HelpPrinterCustomFunc
584 arguments []string
585 wantTemplate string
586 wantOutput string
587 }{
588 {
589 name: "no command",
590 printer: func(w io.Writer, _ string, _ any, _ map[string]any) {
591 fmt.Fprint(w, "yo")
592 },
593 arguments: []string{"my-app", "help"},
594 wantTemplate: RootCommandHelpTemplate,
595 wantOutput: "yo",
596 },
597 {
598 name: "standard command",
599 printer: func(w io.Writer, _ string, _ any, _ map[string]any) {
600 fmt.Fprint(w, "yo")
601 },
602 arguments: []string{"my-app", "help", "my-command"},
603 wantTemplate: CommandHelpTemplate,
604 wantOutput: "yo",
605 },
606 {
607 name: "custom template command",
608 template: "{{doublecho .Name}}",
609 printer: func(w io.Writer, templ string, data any, _ map[string]any) {
610 // Pass a custom function to ensure it gets used
611 fm := map[string]any{"doublecho": doublecho}
612 DefaultPrintHelpCustom(w, templ, data, fm)
613 },
614 arguments: []string{"my-app", "help", "my-command"},
615 wantTemplate: "{{doublecho .Name}}",
616 wantOutput: "my-command my-command",
617 },
618 }
619
620 for _, tc := range testCases {
621 t.Run(tc.name, func(t *testing.T) {
622 r := require.New(t)
623
624 defer func(old HelpPrinterCustomFunc) {
625 HelpPrinterCustom = old
626 }(HelpPrinterCustom)
627
628 HelpPrinterCustom = func(w io.Writer, tmpl string, data any, fm map[string]any) {
629 r.Nil(fm)
630 r.Equal(tc.wantTemplate, tmpl)
631
632 tc.printer(w, tmpl, data, fm)

Callers

nothing calls this directly

Calls 4

RunMethod · 0.95
DefaultPrintHelpCustomFunction · 0.85
buildTestContextFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected