github.com/WindomZ/go-commander@v1.2.2/command_test.go (about) 1 package commander 2 3 import ( 4 "github.com/WindomZ/testify/assert" 5 "testing" 6 ) 7 8 func TestCommand_CommandsString(t *testing.T) { 9 c := newCommand(true) 10 11 c.Command("test"). 12 Version("0.0.1"). 13 Description("this is a test cli.") 14 15 c.Command("add <x> <y>"). 16 Description("this addition operation") 17 18 c.Command("sub <x> <y>"). 19 Description("this subtraction operation"). 20 Command("again"). 21 Description("this subtraction operation again") 22 23 assert.Equal(t, c.CommandsString(""), 24 []string{ 25 "add this addition operation", 26 "sub this subtraction operation", 27 "sub again this subtraction operation again", 28 }) 29 } 30 31 func TestCommand_HelpMessage(t *testing.T) { 32 c := newCommand(true) 33 assert.Equal(t, c.HelpMessage(), "") 34 35 c.Usage("cmd <x>", "this is description"). 36 Option("-c, --config", "config description"). 37 Option("-d, --drop", "drop description") 38 39 c.Command("cmd2"). 40 Option("-a, --about", "cmd2 about description"). 41 Option("-t, --test", "cmd2 test description") 42 43 c.Command("cmd3 [y]"). 44 Option("-b=<kn>, --bold=<kn>", "cmd3 bold description"). 45 Option("-c, --count", "cmd3 count description") 46 47 assert.Equal(t, c.UsagesString(), 48 []string{ 49 "cmd <x> [-c|--config] [-d|--drop]", 50 "cmd cmd2 [-a|--about] [-t|--test]", 51 "cmd cmd3 [y] [-b=<kn>|--bold=<kn>] [-c|--count]", 52 }) 53 opts := c.OptionsString() 54 assert.Equal(t, opts["-c|--config"], "-c --config config description") 55 assert.Equal(t, opts["-d|--drop"], "-d --drop drop description") 56 assert.Equal(t, opts["-a|--about"], "-a --about cmd2 about description") 57 assert.Equal(t, opts["-t|--test"], "-t --test cmd2 test description") 58 assert.Equal(t, opts["-b|--bold"], "-b=<kn> --bold=<kn>\n cmd3 bold description") 59 assert.Equal(t, opts["-c|--count"], "-c --count cmd3 count description") 60 61 assert.Equal(t, c.HelpMessage(), 62 ` this is description 63 64 Usage: 65 cmd <x> [-c|--config] [-d|--drop] 66 cmd cmd2 [-a|--about] [-t|--test] 67 cmd cmd3 [y] [-b=<kn>|--bold=<kn>] [-c|--count] 68 69 Options: 70 -a --about cmd2 about description 71 -b=<kn> --bold=<kn> 72 cmd3 bold description 73 -c --config config description 74 -c --count cmd3 count description 75 -d --drop drop description 76 -t --test cmd2 test description 77 `) 78 79 c.Annotation("Example", []string{ 80 "cmd xxx -c", 81 "cmd cmd2 -a", 82 "cmd cmd3 y -b", 83 }) 84 assert.Equal(t, c.HelpMessage(), 85 ` this is description 86 87 Usage: 88 cmd <x> [-c|--config] [-d|--drop] 89 cmd cmd2 [-a|--about] [-t|--test] 90 cmd cmd3 [y] [-b=<kn>|--bold=<kn>] [-c|--count] 91 92 Options: 93 -a --about cmd2 about description 94 -b=<kn> --bold=<kn> 95 cmd3 bold description 96 -c --config config description 97 -c --count cmd3 count description 98 -d --drop drop description 99 -t --test cmd2 test description 100 101 Example: 102 cmd xxx -c 103 cmd cmd2 -a 104 cmd cmd3 y -b 105 `) 106 107 c.Doc(` Usage: 108 cmd <x> [-c|--config] [-d|--drop] 109 cmd cmd2 [-a|--about] [-t|--test] 110 cmd cmd3 [y] [-b=<kn>|--bold=<kn>] [-c|--count] 111 `) 112 113 assert.Equal(t, c.HelpMessage(), ` Usage: 114 cmd <x> [-c|--config] [-d|--drop] 115 cmd cmd2 [-a|--about] [-t|--test] 116 cmd cmd3 [y] [-b=<kn>|--bold=<kn>] [-c|--count] 117 `) 118 } 119 120 func TestCommand_Aliases(t *testing.T) { 121 c := newCommand(true) 122 c.Command("cmd"). 123 Aliases([]string{"c0", "cmd0"}). 124 Option("-a, --about", "cmd about description"). 125 Option("-t, --test", "cmd test description") 126 assert.Equal(t, c.HelpMessage(), ` Usage: 127 (cmd|c0|cmd0) [-a|--about] [-t|--test] 128 129 Options: 130 -a --about cmd about description 131 -t --test cmd test description 132 `) 133 } 134 135 func TestCommand_ShowVersion(t *testing.T) { 136 c := newCommand(true) 137 c.Version("v0.0.1") 138 139 assert.Empty(t, c.ShowVersion()) 140 } 141 142 func TestCommand_ShowHelpMessage(t *testing.T) { 143 c := newCommand(true) 144 c.Version("v0.0.1").Description("this is a test cli.") 145 146 assert.NotEmpty(t, c.ShowHelpMessage()) 147 148 assert.Equal(t, c.HelpMessage(), 149 ` this is a test cli. 150 151 `) 152 } 153 154 func TestCommand_ErrorHandling(t *testing.T) { 155 c := newCommand(true) 156 c.ErrorHandling(func(err error) { 157 assert.NoError(t, err) 158 }) 159 160 c.Command("test").Action(func() error { 161 return newError("this is a test error") 162 }) 163 164 if _, err := c.Parse([]string{"go-commander", "test"}); err != nil { 165 assert.Error(t, err) 166 } 167 } 168 169 func TestCommand_Command(t *testing.T) { 170 c := newCommand(true) 171 172 assert.Empty(t, c.Name()) 173 174 c.Command("go-commander") 175 c.Version("0.0.1") 176 177 c.Command("test") 178 179 c.Line("[opt]").Command("cmd") 180 181 defer func() { 182 assert.NotEmpty(t, recover()) 183 }() 184 c.Command("") 185 }