github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/cmds/core/elvish/store/cmd_test.go (about)

     1  package store
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/u-root/u-root/cmds/core/elvish/store/storedefs"
     7  )
     8  
     9  var (
    10  	cmds     = []string{"echo foo", "put bar", "put lorem", "echo bar"}
    11  	searches = []struct {
    12  		next      bool
    13  		seq       int
    14  		prefix    string
    15  		wantedSeq int
    16  		wantedCmd string
    17  		wantedErr error
    18  	}{
    19  		{false, 5, "echo", 4, "echo bar", nil},
    20  		{false, 5, "put", 3, "put lorem", nil},
    21  		{false, 4, "echo", 1, "echo foo", nil},
    22  		{false, 3, "f", 0, "", storedefs.ErrNoMatchingCmd},
    23  		{false, 1, "", 0, "", storedefs.ErrNoMatchingCmd},
    24  
    25  		{true, 1, "echo", 1, "echo foo", nil},
    26  		{true, 1, "put", 2, "put bar", nil},
    27  		{true, 2, "echo", 4, "echo bar", nil},
    28  		{true, 4, "put", 0, "", storedefs.ErrNoMatchingCmd},
    29  	}
    30  )
    31  
    32  func TestCmd(t *testing.T) {
    33  	tStore := NewCmdHistory()
    34  	startSeq, err := tStore.Next()
    35  	if startSeq != 1 || err != nil {
    36  		t.Errorf("tStore.NextCmdSeq() => (%v, %v), want (1, nil)",
    37  			startSeq, err)
    38  	}
    39  	for i, cmd := range cmds {
    40  		wantSeq := startSeq + i
    41  		seq, err := tStore.Add(cmd)
    42  		if seq != wantSeq || err != nil {
    43  			t.Errorf("tStore.Add(%v) => (%v, %v), want (%v, nil)",
    44  				cmd, seq, err, wantSeq)
    45  		}
    46  	}
    47  	endSeq, err := tStore.Next()
    48  	wantedEndSeq := startSeq + len(cmds)
    49  	if endSeq != wantedEndSeq || err != nil {
    50  		t.Errorf("tStore.Next() => (%v, %v), want (%v, nil)",
    51  			endSeq, err, wantedEndSeq)
    52  	}
    53  	for i, wantedCmd := range cmds {
    54  		seq := i + startSeq
    55  		cmd, err := tStore.One(seq)
    56  		if cmd != wantedCmd || err != nil {
    57  			t.Errorf("tStore.Cmd(%v) => (%v, %v), want (%v, nil)",
    58  				seq, cmd, err, wantedCmd)
    59  		}
    60  	}
    61  	for _, tt := range searches {
    62  		f := tStore.RSearch
    63  		funcname := "tStore.RSearch"
    64  		if tt.next {
    65  			f = tStore.Search
    66  			funcname = "tStore.Search"
    67  		}
    68  		seq, cmd, err := f(tt.seq, tt.prefix)
    69  		if seq != tt.wantedSeq || cmd != tt.wantedCmd || err != tt.wantedErr {
    70  			t.Errorf("%s(%v, %v) in (%v), => (%v, %v, %v), want (%v, %v, %v)",
    71  				funcname, tt.seq, tt.prefix,
    72  				tStore,
    73  				seq, cmd, err,
    74  				tt.wantedSeq, tt.wantedCmd, tt.wantedErr)
    75  		}
    76  	}
    77  
    78  	if err := tStore.Remove(1); err != nil {
    79  		t.Error("Failed to remove cmd")
    80  	}
    81  	if seq, err := tStore.One(1); err != storedefs.ErrNoMatchingCmd {
    82  		t.Errorf("Cmd(1) => (%v, %v), want (%v, %v)",
    83  			seq, err, "", storedefs.ErrNoMatchingCmd)
    84  	}
    85  }