6
6
"testing"
7
7
8
8
"github.com/gookit/gcli/v3"
9
+ "github.com/gookit/gcli/v3/events"
9
10
"github.com/gookit/goutil/dump"
10
11
"github.com/gookit/goutil/testutil/assert"
11
12
)
@@ -25,32 +26,32 @@ func TestApp_Hooks_EvtAppInit(t *testing.T) {
25
26
buf .Reset ()
26
27
27
28
cli := newNotExitApp ()
28
- cli .On (gcli . EvtAppInit , func (data ... any ) bool {
29
- buf .WriteString ("trigger " + gcli . EvtAppInit )
29
+ cli .On (events . OnAppInit , func (ctx * gcli. HookCtx ) bool {
30
+ buf .WriteString ("trigger " + events . OnAppInit )
30
31
return false
31
32
})
32
33
cli .Add (simpleCmd )
33
- assert .Eq (t , "trigger " + gcli . EvtAppInit , buf .String ())
34
+ assert .Eq (t , "trigger " + events . OnAppInit , buf .String ())
34
35
35
36
buf .Reset ()
36
- cli .On (gcli . EvtGOptionsParsed , func (data ... any ) bool {
37
- buf .WriteString ("trigger " + gcli . EvtGOptionsParsed + ", args:" + fmt .Sprintf ("%v" , data [ 1 ] ))
37
+ cli .On (events . OnGOptionsParsed , func (ctx * gcli. HookCtx ) bool {
38
+ buf .WriteString ("trigger " + ctx . Name () + ", args:" + fmt .Sprintf ("%v" , ctx . Strings ( "args" ) ))
38
39
return false
39
40
})
41
+
40
42
cli .Run ([]string {"simple" })
41
- assert .Eq (t , "trigger " + gcli . EvtGOptionsParsed + ", args:[simple]" , buf .String ())
43
+ assert .Eq (t , "trigger " + events . OnGOptionsParsed + ", args:[simple]" , buf .String ())
42
44
}
43
45
44
46
func TestApp_Hooks_EvtCmdInit (t * testing.T ) {
45
47
buf .Reset ()
46
48
47
49
cli := newNotExitApp ()
48
- cli .On (gcli . EvtCmdInit , func (data ... any ) (stop bool ) {
49
- buf .WriteString (gcli . EvtCmdInit )
50
+ cli .On (events . OnCmdInit , func (ctx * gcli. HookCtx ) (stop bool ) {
51
+ buf .WriteString (events . OnCmdInit )
50
52
buf .WriteString (":" )
51
53
52
- c := data [1 ].(* gcli.Command )
53
- buf .WriteString (c .Name + ";" )
54
+ buf .WriteString (ctx .Cmd .Name + ";" )
54
55
return
55
56
})
56
57
@@ -70,17 +71,17 @@ func TestCommand_Hooks_EvtCmdOptParsed(t *testing.T) {
70
71
Desc : "desc" ,
71
72
Config : func (c * gcli.Command ) {
72
73
buf .WriteString ("run config;" )
73
- c .On (gcli . EvtCmdOptParsed , func (data ... any ) (stop bool ) {
74
- dump .P (data [ 1 ] )
75
- buf .WriteString (gcli . EvtCmdOptParsed )
74
+ c .On (events . OnCmdOptParsed , func (ctx * gcli. HookCtx ) (stop bool ) {
75
+ dump .P (ctx . Strings ( "args" ) )
76
+ buf .WriteString (ctx . Name () )
76
77
return
77
78
})
78
79
},
79
80
})
80
81
assert .Contains (t , buf .String (), "run config;" )
81
82
82
83
cli .Run ([]string {"test" })
83
- assert .Contains (t , buf .String (), gcli . EvtCmdOptParsed )
84
+ assert .Contains (t , buf .String (), events . OnCmdOptParsed )
84
85
}
85
86
86
87
func TestApp_On_CmdNotFound (t * testing.T ) {
@@ -90,9 +91,9 @@ func TestApp_On_CmdNotFound(t *testing.T) {
90
91
cli .Add (simpleCmd )
91
92
92
93
fmt .Println ("--------- will print command tips ----------" )
93
- cli .On (gcli . EvtCmdNotFound , func (data ... any ) bool {
94
- buf .WriteString ("trigger: " + gcli . EvtCmdNotFound )
95
- buf .WriteString ("; command: " + fmt . Sprint ( data [ 1 ] ))
94
+ cli .On (events . OnCmdNotFound , func (ctx * gcli. HookCtx ) bool {
95
+ buf .WriteString ("trigger: " + events . OnCmdNotFound )
96
+ buf .WriteString ("; command: " + ctx . Str ( "name" ))
96
97
return false
97
98
})
98
99
@@ -101,9 +102,9 @@ func TestApp_On_CmdNotFound(t *testing.T) {
101
102
buf .Reset ()
102
103
103
104
fmt .Println ("--------- dont print command tips ----------" )
104
- cli .On (gcli . EvtCmdNotFound , func (data ... any ) bool {
105
- buf .WriteString ("trigger: " + gcli . EvtCmdNotFound )
106
- buf .WriteString ("; command: " + fmt . Sprint ( data [ 1 ] ))
105
+ cli .On (events . OnCmdNotFound , func (ctx * gcli. HookCtx ) bool {
106
+ buf .WriteString ("trigger: " + events . OnCmdNotFound )
107
+ buf .WriteString ("; command: " + ctx . Str ( "name" ))
107
108
return true
108
109
})
109
110
@@ -120,13 +121,12 @@ func TestApp_On_CmdNotFound_redirect(t *testing.T) {
120
121
cli .Add (simpleCmd )
121
122
122
123
fmt .Println ("--------- redirect to run another command ----------" )
123
- cli .On (gcli . EvtCmdNotFound , func (data ... any ) bool {
124
- buf .WriteString ("trigger:" + gcli . EvtCmdNotFound )
125
- buf .WriteString (" - command:" + fmt . Sprint ( data [ 1 ] ))
124
+ cli .On (events . OnCmdNotFound , func (ctx * gcli. HookCtx ) bool {
125
+ buf .WriteString ("trigger:" + events . OnCmdNotFound )
126
+ buf .WriteString (" - command:" + ctx . Str ( "name" ))
126
127
buf .WriteString ("; redirect:simple - " )
127
128
128
- app := data [0 ].(* gcli.App )
129
- err := app .Exec ("simple" , nil )
129
+ err := ctx .App .Exec ("simple" , nil )
130
130
assert .NoErr (t , err )
131
131
buf .WriteString ("value:" + simpleCmd .StrValue ("simple" ))
132
132
return true
0 commit comments