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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
package modes
import (
"strings"
"testing"
"src.elv.sh/pkg/cli"
. "src.elv.sh/pkg/cli/clitest"
"src.elv.sh/pkg/cli/histutil"
"src.elv.sh/pkg/cli/term"
"src.elv.sh/pkg/cli/tk"
"src.elv.sh/pkg/store/storedefs"
"src.elv.sh/pkg/ui"
)
func TestNewLastcmd_NoStore(t *testing.T) {
f := Setup()
defer f.Stop()
_, err := NewLastcmd(f.App, LastcmdSpec{})
if err != errNoHistoryStore {
t.Error("expect errNoHistoryStore")
}
}
func TestNewLastcmd_FocusedWidgetNotCodeArea(t *testing.T) {
testFocusedWidgetNotCodeArea(t, func(app cli.App) error {
st := histutil.NewMemStore("foo")
_, err := NewLastcmd(app, LastcmdSpec{Store: st})
return err
})
}
func TestNewLastcmd_StoreError(t *testing.T) {
f := Setup()
defer f.Stop()
db := histutil.NewFaultyInMemoryDB()
store, err := histutil.NewDBStore(db)
if err != nil {
panic(err)
}
db.SetOneOffError(errMock)
_, err = NewLastcmd(f.App, LastcmdSpec{Store: store})
if err.Error() != "db error: mock error" {
t.Error("expect db error")
}
}
func TestLastcmd(t *testing.T) {
f := Setup()
defer f.Stop()
st := histutil.NewMemStore("foo,bar,baz")
startLastcmd(f.App, LastcmdSpec{
Store: st,
Wordifier: func(cmd string) []string {
return strings.Split(cmd, ",")
},
})
// Test UI.
f.TestTTY(t,
"\n", // empty code area
" LASTCMD ", Styles,
"********* ", term.DotHere, "\n",
" foo,bar,baz \n", Styles,
"++++++++++++++++++++++++++++++++++++++++++++++++++",
" 0 foo\n",
" 1 bar\n",
" 2 baz",
)
// Test negative filtering.
f.TTY.Inject(term.K('-'))
f.TestTTY(t,
"\n", // empty code area
" LASTCMD -", Styles,
"********* ", term.DotHere, "\n",
" -3 foo \n", Styles,
"++++++++++++++++++++++++++++++++++++++++++++++++++",
" -2 bar\n",
" -1 baz",
)
// Test automatic submission.
f.TTY.Inject(term.K('2')) // -2 bar
f.TestTTY(t, "bar", term.DotHere)
// Test submission by Enter.
f.App.ActiveWidget().(tk.CodeArea).MutateState(func(s *tk.CodeAreaState) {
*s = tk.CodeAreaState{}
})
startLastcmd(f.App, LastcmdSpec{
Store: st,
Wordifier: func(cmd string) []string {
return strings.Split(cmd, ",")
},
})
f.TTY.Inject(term.K(ui.Enter))
f.TestTTY(t, "foo,bar,baz", term.DotHere)
// Default wordifier.
f.App.ActiveWidget().(tk.CodeArea).MutateState(func(s *tk.CodeAreaState) {
*s = tk.CodeAreaState{}
})
st.AddCmd(storedefs.Cmd{Text: "foo bar baz", Seq: 1})
startLastcmd(f.App, LastcmdSpec{Store: st})
f.TTY.Inject(term.K('0'))
f.TestTTY(t, "foo", term.DotHere)
}
func startLastcmd(app cli.App, spec LastcmdSpec) {
w, err := NewLastcmd(app, spec)
startMode(app, w, err)
}
|