-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathext_test.go
65 lines (50 loc) · 1.42 KB
/
ext_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package gcli_test
import (
"testing"
"github.com/gookit/gcli/v3"
"github.com/gookit/goutil/byteutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestHelpReplacer(t *testing.T) {
is := assert.New(t)
vs := gcli.HelpReplacer{}
vs.AddReplaces(map[string]string{
"key0": "val0",
"key1": "val1",
})
is.Len(vs.Replaces(), 2)
is.Contains(vs.Replaces(), "key0")
vs.AddReplaces(map[string]string{"key2": "val2"})
vs.AddReplace("key3", "val3")
is.Eq("val3", vs.GetReplace("key3"))
is.Eq("", vs.GetReplace("not-exist"))
is.Eq("hello val0", vs.ReplacePairs("hello {$key0}"))
is.Eq("hello val0 val2", vs.ReplacePairs("hello {$key0} {$key2}"))
// invalid input
is.Eq("hello {key0}", vs.ReplacePairs("hello {key0}"))
}
func TestHooks_Fire(t *testing.T) {
is := assert.New(t)
buf := byteutil.NewBuffer()
hooks := gcli.Hooks{}
hooks.AddHook("test", func(ctx *gcli.HookCtx) bool {
buf.WriteString("fire the test hook")
return false
})
hooks.Fire("test", nil)
is.Eq("fire the test hook", buf.ResetGet())
hooks.Fire("not-exist", nil)
hooks.On("*", func(ctx *gcli.HookCtx) bool {
buf.WriteString("fire the * hook")
return false
})
// add prefix hook
hooks.On("app.test.*", func(ctx *gcli.HookCtx) bool {
buf.WriteString("fire the app.test.* hook")
return false
})
hooks.Fire("app.test.init", nil)
s := buf.ResetGet()
is.StrContains(s, "fire the app.test.* hook")
is.StrContains(s, "fire the * hook")
}