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
|
package coloredcobra
import (
"fmt"
"testing"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var (
rootCmd = &cobra.Command{
Use: "test",
Short: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
Long: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
Example: "There is an example.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Test")
},
}
cfg *Config
)
type colorTest struct {
colorInt uint8
colorColor *color.Color
}
var colorTests = []colorTest{
{White, color.New(color.FgWhite)},
{Black, color.New(color.FgBlack)},
{Red, color.New(color.FgRed)},
{Green, color.New(color.FgGreen)},
{Yellow, color.New(color.FgYellow)},
{Blue, color.New(color.FgBlue)},
{Magenta, color.New(color.FgMagenta)},
{Cyan, color.New(color.FgCyan)},
{HiRed, color.New(color.FgHiRed)},
{HiGreen, color.New(color.FgHiGreen)},
{HiYellow, color.New(color.FgHiYellow)},
{HiBlue, color.New(color.FgHiBlue)},
{HiMagenta, color.New(color.FgHiMagenta)},
{HiCyan, color.New(color.FgHiCyan)},
{HiWhite, color.New(color.FgHiWhite)},
{White + Bold + Italic + Underline, color.New(color.FgWhite, color.Bold, color.Italic, color.Underline)},
{Red + Bold, color.New(color.FgRed, color.Bold)},
{Yellow + Italic, color.New(color.FgYellow, color.Italic)},
{Blue + Underline, color.New(color.FgBlue, color.Underline)},
{Bold, color.New(color.FgWhite, color.Bold)},
{Italic, color.New(color.FgWhite, color.Italic)},
{Underline, color.New(color.FgWhite, color.Underline)},
}
func TestGetColor(t *testing.T) {
for _, test := range colorTests {
res1 := fmt.Sprintf("%v", getColor(test.colorInt))
res2 := fmt.Sprintf("%v", test.colorColor)
if res1 != res2 {
t.Errorf("got: %s, expected: %s", res1, res2)
}
}
}
type templateTest struct {
in, out string
}
var templateTestHeadings = []templateTest{
{`Usage:`, `{{HeadingStyle "Usage:"}}`},
{`Aliases:`, `{{HeadingStyle "Aliases:"}}`},
{`Examples:`, `{{HeadingStyle "Examples:"}}`},
{`Available Commands:`, `{{HeadingStyle "Available Commands:"}}`},
{`Global Flags:`, `{{HeadingStyle "Global Flags:"}}`},
{`Additional help topics:`, `{{HeadingStyle "Additional help topics:"}}`},
{`Flags:`, `{{HeadingStyle "Flags:"}}`},
}
var templateTests = []templateTest{
{`{{rpad .Name .NamePadding}}`, `{{rpad (CommandStyle .Name) (sum .NamePadding 12)}}`},
{`{{ rpad .Name .NamePadding }}`, `{{rpad (CommandStyle .Name) (sum .NamePadding 12)}}`},
{`{{rpad .CommandPath .CommandPathPadding}}`, `{{rpad (CommandStyle .CommandPath) (sum .CommandPathPadding 12)}}`},
{`{{ rpad .CommandPath .CommandPathPadding }}`, `{{rpad (CommandStyle .CommandPath) (sum .CommandPathPadding 12)}}`},
{`{{range .Commands}}{{.Short}}`, `{{range .Commands}}{{CmdShortStyle .Short}}`},
{`{{range .Commands}}{{ .Short }}`, `{{range .Commands}}{{CmdShortStyle .Short}}`},
{`{{.CommandPath}}`, `{{ExecStyle .CommandPath}}`},
{`{{ .CommandPath }}`, `{{ExecStyle .CommandPath}}`},
{`{{.UseLine}}`, `{{UseLineStyle .UseLine}}`},
{`{{ .useline }}`, `{{UseLineStyle .UseLine}}`},
{`{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}`, `{{FlagStyle .LocalFlags.FlagUsages | trimTrailingWhitespaces}}`},
{`{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}`, `{{FlagStyle .InheritedFlags.FlagUsages | trimTrailingWhitespaces}}`},
{`{{.NameAndAliases}}`, `{{AliasStyle .NameAndAliases}}`},
{`{{ .NameAndAliases }}`, `{{AliasStyle .NameAndAliases}}`},
{`{{.Example}}`, `{{ExampleStyle .Example}}`},
{`{{ .Example}}`, `{{ExampleStyle .Example}}`},
}
func TestTemplateReplaces(t *testing.T) {
cfg = &Config{
RootCmd: rootCmd,
Headings: HiCyan + Bold + Underline,
Commands: HiYellow + Bold,
CmdShortDescr: HiRed,
ExecName: Bold,
Flags: Bold,
FlagsDataType: Italic,
FlagsDescr: HiRed,
Aliases: HiMagenta + Underline,
Example: Italic,
NoExtraNewlines: true,
NoBottomNewline: true,
}
Init(cfg)
rootCmd.PersistentFlags().StringP("flag", "f", "", "Flag description")
rootCmd.UsageString()
// No extra new lines
for _, test := range templateTestHeadings {
makeTemplateTest(test, t)
}
for _, test := range templateTests {
makeTemplateTest(test, t)
}
// New line at the end of template
cfg.NoBottomNewline = false
for _, test := range templateTestHeadings {
test.out = test.out + "\n"
makeTemplateTest(test, t)
}
for _, test := range templateTests {
test.out = test.out + "\n"
makeTemplateTest(test, t)
}
// Extra new lines before and after headings
cfg.NoBottomNewline = true
cfg.NoExtraNewlines = false
for _, test := range templateTestHeadings {
test.out = "\n" + test.out + "\n"
makeTemplateTest(test, t)
}
}
func makeTemplateTest(test templateTest, t *testing.T) {
rootCmd.SetUsageTemplate(test.in)
Init(cfg)
res := rootCmd.UsageTemplate()
if res != test.out {
t.Errorf("got: %v, expected: %v", res, test.out)
}
}
|