elvish/store/cmd_hist_test.go

69 lines
1.7 KiB
Go
Raw Normal View History

2015-02-25 23:40:48 +08:00
package store
import "testing"
var (
cmds = []string{"echo foo", "put bar", "put lorem", "echo bar"}
searches = []struct {
first bool
seq int
prefix string
wantedSeq int
wantedCmd string
wantedErr error
}{
{false, 5, "echo", 4, "echo bar", nil},
{false, 5, "put", 3, "put lorem", nil},
{false, 4, "echo", 1, "echo foo", nil},
{false, 3, "f", 0, "", ErrNoMatchingCmd},
{true, 1, "echo", 1, "echo foo", nil},
{true, 1, "put", 2, "put bar", nil},
{true, 2, "echo", 4, "echo bar", nil},
{true, 4, "put", 0, "", ErrNoMatchingCmd},
}
)
func TestCmd(t *testing.T) {
startSeq, err := tStore.NextCmdSeq()
2015-02-25 23:40:48 +08:00
if startSeq != 1 || err != nil {
t.Errorf("tStore.NextCmdSeq() => (%v, %v), want (1, nil)",
2015-02-25 23:40:48 +08:00
startSeq, err)
}
for _, cmd := range cmds {
err := tStore.AddCmd(cmd)
if err != nil {
t.Errorf("tStore.AddCmd(%v) => %v, want nil", cmd, err)
}
}
endSeq, err := tStore.NextCmdSeq()
2015-02-25 23:40:48 +08:00
wantedEndSeq := startSeq + len(cmds)
if endSeq != wantedEndSeq || err != nil {
t.Errorf("tStore.NextCmdSeq() => (%v, %v), want (%v, nil)",
2015-02-25 23:40:48 +08:00
endSeq, err, wantedEndSeq)
}
for i, wantedCmd := range cmds {
seq := i + startSeq
cmd, err := tStore.Cmd(seq)
2015-02-25 23:40:48 +08:00
if cmd != wantedCmd || err != nil {
t.Errorf("tStore.Cmd(%v) => (%v, %v), want (%v, nil)",
2015-02-25 23:40:48 +08:00
seq, cmd, err, wantedCmd)
}
}
for _, tt := range searches {
2015-02-26 07:09:53 +08:00
f := tStore.LastCmd
fname := "tStore.LastCmd"
2015-02-25 23:40:48 +08:00
if tt.first {
2015-02-26 07:09:53 +08:00
f = tStore.FirstCmd
fname = "tStore.FirstCmd"
2015-02-25 23:40:48 +08:00
}
2016-02-13 08:08:59 +08:00
seq, cmd, err := f(tt.seq, tt.prefix, true)
2015-02-25 23:40:48 +08:00
if seq != tt.wantedSeq || cmd != tt.wantedCmd || err != tt.wantedErr {
t.Errorf("%s(%v, %v) => (%v, %v, %v), want (%v, %v, %v)",
fname, tt.seq, tt.prefix,
seq, cmd, err,
tt.wantedSeq, tt.wantedCmd, tt.wantedErr)
}
}
}