github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/cli/mode/histlist_test.go (about)

     1  package mode
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  
     7  	"src.elv.sh/pkg/cli"
     8  	. "src.elv.sh/pkg/cli/clitest"
     9  	"src.elv.sh/pkg/cli/histutil"
    10  	"src.elv.sh/pkg/cli/term"
    11  	"src.elv.sh/pkg/store"
    12  	"src.elv.sh/pkg/ui"
    13  )
    14  
    15  func TestNewHistlist_NoStore(t *testing.T) {
    16  	f := Setup()
    17  	defer f.Stop()
    18  
    19  	_, err := NewHistlist(f.App, HistlistSpec{})
    20  	if err != errNoHistoryStore {
    21  		t.Errorf("want errNoHistoryStore")
    22  	}
    23  }
    24  
    25  type faultyStore struct{}
    26  
    27  func (s faultyStore) AllCmds() ([]store.Cmd, error) { return nil, errMock }
    28  
    29  func TestNewHistlist_StoreError(t *testing.T) {
    30  	f := Setup()
    31  	defer f.Stop()
    32  
    33  	_, err := NewHistlist(f.App, HistlistSpec{AllCmds: faultyStore{}.AllCmds})
    34  	if err.Error() != "db error: mock error" {
    35  		t.Errorf("want db error")
    36  	}
    37  }
    38  
    39  func TestHistlist(t *testing.T) {
    40  	f := Setup()
    41  	defer f.Stop()
    42  
    43  	st := histutil.NewMemStore(
    44  		// 0    1      2
    45  		"foo", "bar", "baz")
    46  	startHistlist(f.App, HistlistSpec{AllCmds: st.AllCmds})
    47  
    48  	// Test initial UI - last item selected
    49  	f.TestTTY(t,
    50  		"\n",
    51  		" HISTORY (dedup on)  ", Styles,
    52  		"******************** ", term.DotHere, "\n",
    53  		"   0 foo\n",
    54  		"   1 bar\n",
    55  		"   2 baz                                          ", Styles,
    56  		"++++++++++++++++++++++++++++++++++++++++++++++++++")
    57  
    58  	// Test filtering.
    59  	f.TTY.Inject(term.K('b'))
    60  	f.TestTTY(t,
    61  		"\n",
    62  		" HISTORY (dedup on)  b", Styles,
    63  		"********************  ", term.DotHere, "\n",
    64  		"   1 bar\n",
    65  		"   2 baz                                          ", Styles,
    66  		"++++++++++++++++++++++++++++++++++++++++++++++++++")
    67  
    68  	// Test accepting.
    69  	f.TTY.Inject(term.K(ui.Enter))
    70  	f.TestTTY(t, "baz", term.DotHere)
    71  
    72  	// Test accepting when there is already some text.
    73  	st.AddCmd(store.Cmd{Text: "baz2"})
    74  	startHistlist(f.App, HistlistSpec{AllCmds: st.AllCmds})
    75  	f.TTY.Inject(term.K(ui.Enter))
    76  	f.TestTTY(t, "baz",
    77  		// codearea now contains newly inserted entry on a separate line
    78  		"\n", "baz2", term.DotHere)
    79  }
    80  
    81  func TestHistlist_Dedup(t *testing.T) {
    82  	f := Setup()
    83  	defer f.Stop()
    84  
    85  	st := histutil.NewMemStore(
    86  		// 0    1      2
    87  		"ls", "echo", "ls")
    88  
    89  	// No dedup
    90  	startHistlist(f.App,
    91  		HistlistSpec{AllCmds: st.AllCmds, Dedup: func() bool { return false }})
    92  	f.TestTTY(t,
    93  		"\n",
    94  		" HISTORY  ", Styles,
    95  		"********* ", term.DotHere, "\n",
    96  		"   0 ls\n",
    97  		"   1 echo\n",
    98  		"   2 ls                                           ", Styles,
    99  		"++++++++++++++++++++++++++++++++++++++++++++++++++")
   100  
   101  	// With dedup
   102  	startHistlist(f.App,
   103  		HistlistSpec{AllCmds: st.AllCmds, Dedup: func() bool { return true }})
   104  	f.TestTTY(t,
   105  		"\n",
   106  		" HISTORY (dedup on)  ", Styles,
   107  		"******************** ", term.DotHere, "\n",
   108  		"   1 echo\n",
   109  		"   2 ls                                           ", Styles,
   110  		"++++++++++++++++++++++++++++++++++++++++++++++++++")
   111  }
   112  
   113  func TestHistlist_CustomFilter(t *testing.T) {
   114  	f := Setup()
   115  	defer f.Stop()
   116  
   117  	st := histutil.NewMemStore(
   118  		// 0   1         2
   119  		"vi", "elvish", "nvi")
   120  
   121  	startHistlist(f.App, HistlistSpec{
   122  		AllCmds: st.AllCmds,
   123  		Filter: FilterSpec{
   124  			Maker: func(p string) func(string) bool {
   125  				re, _ := regexp.Compile(p)
   126  				return func(s string) bool {
   127  					return re != nil && re.MatchString(s)
   128  				}
   129  			},
   130  			Highlighter: func(p string) (ui.Text, []error) {
   131  				return ui.T(p, ui.Inverse), nil
   132  			},
   133  		},
   134  	})
   135  	f.TTY.Inject(term.K('v'), term.K('i'), term.K('$'))
   136  	f.TestTTY(t,
   137  		"\n",
   138  		" HISTORY (dedup on)  vi$", Styles,
   139  		"******************** +++", term.DotHere, "\n",
   140  		"   0 vi\n",
   141  		"   2 nvi                                          ", Styles,
   142  		"++++++++++++++++++++++++++++++++++++++++++++++++++")
   143  }
   144  
   145  func startHistlist(app cli.App, spec HistlistSpec) {
   146  	w, err := NewHistlist(app, spec)
   147  	startMode(app, w, err)
   148  }