github.com/elves/elvish@v0.15.0/pkg/cli/addons/lastcmd/lastcmd_test.go (about)

     1  package lastcmd
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/elves/elvish/pkg/cli"
     9  	. "github.com/elves/elvish/pkg/cli/clitest"
    10  	"github.com/elves/elvish/pkg/cli/histutil"
    11  	"github.com/elves/elvish/pkg/cli/term"
    12  	"github.com/elves/elvish/pkg/store"
    13  	"github.com/elves/elvish/pkg/ui"
    14  )
    15  
    16  var errMock = errors.New("mock error")
    17  
    18  func TestStart_NoStore(t *testing.T) {
    19  	f := Setup()
    20  	defer f.Stop()
    21  
    22  	Start(f.App, Config{})
    23  	f.TestTTYNotes(t, "no history store")
    24  }
    25  
    26  func TestStart_StoreError(t *testing.T) {
    27  	f := Setup()
    28  	defer f.Stop()
    29  
    30  	db := histutil.NewFaultyInMemoryDB()
    31  	store, err := histutil.NewDBStore(db)
    32  	if err != nil {
    33  		panic(err)
    34  	}
    35  	db.SetOneOffError(errMock)
    36  
    37  	Start(f.App, Config{Store: store})
    38  	f.TestTTYNotes(t, "db error: mock error")
    39  }
    40  
    41  func TestStart_OK(t *testing.T) {
    42  	f := Setup()
    43  	defer f.Stop()
    44  
    45  	st := histutil.NewMemStore("foo,bar,baz")
    46  	Start(f.App, Config{
    47  		Store: st,
    48  		Wordifier: func(cmd string) []string {
    49  			return strings.Split(cmd, ",")
    50  		},
    51  	})
    52  
    53  	// Test UI.
    54  	f.TestTTY(t,
    55  		"\n", // empty code area
    56  		" LASTCMD  ", Styles,
    57  		"********* ", term.DotHere, "\n",
    58  		"    foo,bar,baz                                   \n", Styles,
    59  		"++++++++++++++++++++++++++++++++++++++++++++++++++",
    60  		"  0 foo\n",
    61  		"  1 bar\n",
    62  		"  2 baz",
    63  	)
    64  
    65  	// Test negative filtering.
    66  	f.TTY.Inject(term.K('-'))
    67  	f.TestTTY(t,
    68  		"\n", // empty code area
    69  		" LASTCMD  -", Styles,
    70  		"*********  ", term.DotHere, "\n",
    71  		" -3 foo                                           \n", Styles,
    72  		"++++++++++++++++++++++++++++++++++++++++++++++++++",
    73  		" -2 bar\n",
    74  		" -1 baz",
    75  	)
    76  
    77  	// Test automatic submission.
    78  	f.TTY.Inject(term.K('2')) // -2 bar
    79  	f.TestTTY(t, "bar", term.DotHere)
    80  
    81  	// Test submission by Enter.
    82  	f.App.CodeArea().MutateState(func(s *cli.CodeAreaState) {
    83  		*s = cli.CodeAreaState{}
    84  	})
    85  	Start(f.App, Config{
    86  		Store: st,
    87  		Wordifier: func(cmd string) []string {
    88  			return strings.Split(cmd, ",")
    89  		},
    90  	})
    91  	f.TTY.Inject(term.K(ui.Enter))
    92  	f.TestTTY(t, "foo,bar,baz", term.DotHere)
    93  
    94  	// Default wordifier.
    95  	f.App.CodeArea().MutateState(func(s *cli.CodeAreaState) {
    96  		*s = cli.CodeAreaState{}
    97  	})
    98  	st.AddCmd(store.Cmd{Text: "foo bar baz", Seq: 1})
    99  	Start(f.App, Config{Store: st})
   100  	f.TTY.Inject(term.K('0'))
   101  	f.TestTTY(t, "foo", term.DotHere)
   102  }