github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/cli/help_test.go (about) 1 package cli 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 func Test_ShowAppHelp_NoAuthor(t *testing.T) { 9 output := new(bytes.Buffer) 10 app := NewApp() 11 app.Writer = output 12 13 c := NewContext(app, nil, nil) 14 15 ShowAppHelp(c) 16 17 if bytes.Index(output.Bytes(), []byte("AUTHOR(S):")) != -1 { 18 t.Errorf("expected\n%snot to include %s", output.String(), "AUTHOR(S):") 19 } 20 } 21 22 func Test_ShowAppHelp_NoVersion(t *testing.T) { 23 output := new(bytes.Buffer) 24 app := NewApp() 25 app.Writer = output 26 27 app.Version = "" 28 29 c := NewContext(app, nil, nil) 30 31 ShowAppHelp(c) 32 33 if bytes.Index(output.Bytes(), []byte("VERSION:")) != -1 { 34 t.Errorf("expected\n%snot to include %s", output.String(), "VERSION:") 35 } 36 } 37 38 func Test_ShowAppHelp_HideVersion(t *testing.T) { 39 output := new(bytes.Buffer) 40 app := NewApp() 41 app.Writer = output 42 43 app.HideVersion = true 44 45 c := NewContext(app, nil, nil) 46 47 ShowAppHelp(c) 48 49 if bytes.Index(output.Bytes(), []byte("VERSION:")) != -1 { 50 t.Errorf("expected\n%snot to include %s", output.String(), "VERSION:") 51 } 52 } 53 54 func Test_Help_Custom_Flags(t *testing.T) { 55 oldFlag := HelpFlag 56 defer func() { 57 HelpFlag = oldFlag 58 }() 59 60 HelpFlag = BoolFlag{ 61 Name: "help, x", 62 Usage: "show help", 63 } 64 65 app := App{ 66 Flags: []Flag{ 67 BoolFlag{Name: "foo, h"}, 68 }, 69 Action: func(ctx *Context) { 70 if ctx.Bool("h") != true { 71 t.Errorf("custom help flag not set") 72 } 73 }, 74 } 75 output := new(bytes.Buffer) 76 app.Writer = output 77 app.Run([]string{"test", "-h"}) 78 if output.Len() > 0 { 79 t.Errorf("unexpected output: %s", output.String()) 80 } 81 } 82 83 func Test_Version_Custom_Flags(t *testing.T) { 84 oldFlag := VersionFlag 85 defer func() { 86 VersionFlag = oldFlag 87 }() 88 89 VersionFlag = BoolFlag{ 90 Name: "version, V", 91 Usage: "show version", 92 } 93 94 app := App{ 95 Flags: []Flag{ 96 BoolFlag{Name: "foo, v"}, 97 }, 98 Action: func(ctx *Context) { 99 if ctx.Bool("v") != true { 100 t.Errorf("custom version flag not set") 101 } 102 }, 103 } 104 output := new(bytes.Buffer) 105 app.Writer = output 106 app.Run([]string{"test", "-v"}) 107 if output.Len() > 0 { 108 t.Errorf("unexpected output: %s", output.String()) 109 } 110 }