github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zcli/utils_test.go (about) 1 package zcli 2 3 import ( 4 "errors" 5 "flag" 6 "os" 7 "testing" 8 9 zls "github.com/sohaha/zlsgo" 10 ) 11 12 var globalDebug = SetVar("debug", "是否开启调试").Required().Bool() 13 14 // var globalTest = SetVar("test--------------test----------------test", "testtesttesttesttesttesttesttesttesttesttest").Bool(true) 15 16 type testCmd struct { 17 flag1 *bool 18 flag2 *int 19 flag3 *string 20 tt *zls.TestUtil 21 run bool 22 } 23 24 func (cmd *testCmd) Flags(sub *Subcommand) { 25 cmd.flag1 = SetVar("flag1", "Name about flag1").Required().Bool(true) 26 cmd.flag2 = SetVar("flag2", "Name about flag2").Int(1) 27 cmd.flag3 = SetVar("flag333333333333333333333333333333333333", "Name about flag333333333333333333333333333333").String("666") 28 } 29 30 func (cmd *testCmd) Run(args []string) { 31 Log.Debug("run") 32 Log.Debug(Current()) 33 Log.Debug("flag1", *cmd.flag1) 34 cmd.tt.EqualExit(true, *cmd.flag1) 35 cmd.run = true 36 } 37 38 func resetForTesting(args ...string) { 39 os.Args = append([]string{"cmd"}, args...) 40 FirstParameter = os.Args[0] 41 Log.Debugf("resetForTesting: %s\n", os.Args) 42 flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) 43 } 44 45 func testOther(t *testing.T) { 46 tt := zls.NewTest(t) 47 oldOsExit := osExit 48 defer func() { osExit = oldOsExit }() 49 myExit := func(code int) { 50 } 51 osExit = myExit 52 Logo = "test" 53 Version = "1.0.0" 54 HideHelp = true 55 Name = "test" 56 Lang = "zh" 57 s := GetLangText("test-key", "no") 58 tt.Equal("no", s) 59 s = GetLangText("test-key") 60 tt.Equal("test-key", s) 61 SetLangText("zh", "isName", "yes") 62 s = GetLangText("isName") 63 tt.Equal("yes", s) 64 65 tipText("ok") 66 errorText("err") 67 showText("show") 68 warnText("warn") 69 Add("", "", &testCmd{}) 70 Help() 71 } 72 73 func TestUtil(t *testing.T) { 74 BuildGoVersion = "--" 75 BuildTime = "--" 76 showVersionNum(true) 77 Version = "" 78 showVersion() 79 IsDoubleClickStartUp() 80 81 oldOsExit := osExit 82 defer func() { osExit = oldOsExit }() 83 myExit := func(code int) { 84 } 85 osExit = myExit 86 CheckErr(nil, true) 87 CheckErr(errors.New("err"), true) 88 Name = "Name" 89 Logo = "Logo" 90 showHeadr() 91 showFlagsHelp() 92 showLogo() 93 Error("%s", "err") 94 argsIsHelp([]string{"-h"}) 95 } 96 97 func TestDetach(t *testing.T) { 98 t.Log(isDetach("detach")) 99 t.Log(isDetach("dd")) 100 }