tests now leverage the built in test suite, and are much less custom

This commit is contained in:
Steve Domino 2015-09-08 12:57:31 -06:00
parent 8be60dc5cd
commit dfa8ea9685
2 changed files with 36 additions and 73 deletions

View file

@ -18,8 +18,8 @@ var _ = os.Stderr
var tp, te, tt, t1, tr []string
var rootPersPre, echoPre, echoPersPre, timesPersPre []string
var flagb1, flagb2, flagb3, flagbr, flagbp bool
var flags1, flags2a, flags2b, flags3 string
var flagb1, flagb2, flagb3, flagbr, flagbp, flagbh, flagbph bool
var flags1, flags2a, flags2b, flags3, outs string
var flagi1, flagi2, flagi3, flagir int
var globalFlag1 bool
var flagEcho, rootcalled bool
@ -28,6 +28,18 @@ var versionUsed int
const strtwoParentHelp = "help message for parent flag strtwo"
const strtwoChildHelp = "help message for child flag strtwo"
var cmdHiddenFlags = &Command{
Use: "hidden [string to set]",
Short: "set a string value to string var",
Long: `an utterly useless command for testing.`,
Run: func(cmd *Command, args []string) {
outs = "visible"
if flagbh {
outs = "hidden"
}
},
}
var cmdPrint = &Command{
Use: "print [string to print]",
Short: "Print anything to the screen",
@ -976,15 +988,15 @@ func TestFlagOnPflagCommandLine(t *testing.T) {
func TestAddTemplateFunctions(t *testing.T) {
AddTemplateFunc("t", func() bool { return true })
AddTemplateFuncs(template.FuncMap{
"f": func() bool { return false },
"h": func() string { return "Hello," },
"f": func() bool { return false },
"h": func() string { return "Hello," },
"w": func() string { return "world." }})
const usage = "Hello, world."
c := &Command{}
c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`)
if us := c.UsageString(); us != usage {
t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us)
}

View file

@ -5,82 +5,33 @@ import (
"testing"
)
// test to ensure hidden flags run as intended
func init() {
cmdHiddenFlags.Flags().BoolVarP(&flagbh, "boolh", "", false, "")
cmdHiddenFlags.Flags().MarkHidden("boolh")
cmdHiddenFlags.PersistentFlags().BoolVarP(&flagbph, "boolph", "", false, "")
cmdHiddenFlags.PersistentFlags().MarkHidden("boolph")
}
// test to ensure hidden flags run as intended; if the the hidden flag fails to
// run, the output will be incorrect
func TestHiddenFlagExecutes(t *testing.T) {
var out string
var secretFlag bool
boringCmd := &Command{
Use: "boring",
Short: "Do something boring...",
Long: `Not a flashy command, just does boring stuff`,
Run: func(cmd *Command, args []string) {
out = "boring output"
if secretFlag {
out = "super secret NOT boring output!"
}
},
}
//
boringCmd.Flags().BoolVarP(&secretFlag, "secret", "s", false, "makes this command run in super secret mode")
boringCmd.Flags().MarkHidden("secret")
//
boringCmd.Execute()
if out != "boring output" {
t.Errorf("Command with hidden flag failed to run!")
}
//
boringCmd.execute([]string{"-s"})
if out != "super secret NOT boring output!" {
cmdHiddenFlags.execute([]string{"--boolh"})
if outs != "hidden" {
t.Errorf("Hidden flag failed to run!")
}
}
// test to ensure hidden flags do not show up in usage/help text
// test to ensure hidden flags do not show up in usage/help text; if a flag is
// found by Lookup() it will be visible in usage/help text
func TestHiddenFlagsAreHidden(t *testing.T) {
var out string
var secretFlag bool
var persistentSecretFlag bool
boringCmd := &Command{
Use: "boring",
Short: "Do something boring...",
Long: `Not a flashy command, just does boring stuff`,
Run: func(cmd *Command, args []string) {
out = "boring output"
if secretFlag {
out = "super secret NOT boring output!"
}
if persistentSecretFlag {
out = "you have no idea what you're getting yourself into!"
}
},
if cmdHiddenFlags.LocalFlags().Lookup("boolh") != nil {
t.Errorf("unexpected flag 'boolh'")
}
//
boringCmd.Flags().BoolVarP(&secretFlag, "secret", "s", false, "run this command in super secret mode")
boringCmd.Flags().MarkHidden("secret")
// if a command has local flags, they will appear in usage/help text
if boringCmd.HasLocalFlags() {
t.Errorf("Hidden flag found!")
}
//
boringCmd.PersistentFlags().BoolVarP(&persistentSecretFlag, "Secret", "S", false, "run any sub command in super secret mode")
boringCmd.PersistentFlags().MarkHidden("Secret")
// if a command has inherited flags, they will appear in usage/help text
if boringCmd.HasInheritedFlags() {
t.Errorf("Hidden flag found!")
if cmdHiddenFlags.InheritedFlags().Lookup("boolph") != nil {
t.Errorf("unexpected flag 'boolph'")
}
}