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

     1  package histwalk
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/elves/elvish/pkg/cli"
     7  	. "github.com/elves/elvish/pkg/cli/clitest"
     8  	"github.com/elves/elvish/pkg/cli/histutil"
     9  	"github.com/elves/elvish/pkg/cli/term"
    10  	"github.com/elves/elvish/pkg/ui"
    11  )
    12  
    13  func TestHistWalk(t *testing.T) {
    14  	f := Setup()
    15  	defer f.Stop()
    16  
    17  	cli.SetCodeBuffer(f.App, cli.CodeBuffer{Content: "ls", Dot: 2})
    18  	f.App.Redraw()
    19  	buf0 := f.MakeBuffer("ls", term.DotHere)
    20  	f.TTY.TestBuffer(t, buf0)
    21  
    22  	getCfg := func() Config {
    23  		store := histutil.NewMemStore(
    24  			// 0       1        2         3       4         5
    25  			"echo", "ls -l", "echo a", "echo", "echo a", "ls -a")
    26  		return Config{
    27  			Store:  store,
    28  			Prefix: "ls",
    29  			Binding: cli.MapHandler{
    30  				term.K(ui.Up):        func() { Prev(f.App) },
    31  				term.K(ui.Down):      func() { Next(f.App) },
    32  				term.K('[', ui.Ctrl): func() { Close(f.App) },
    33  				term.K(ui.Enter):     func() { Accept(f.App) },
    34  			},
    35  		}
    36  	}
    37  
    38  	Start(f.App, getCfg())
    39  	buf5 := f.MakeBuffer(
    40  		"ls -a", Styles,
    41  		"  ___", term.DotHere, "\n",
    42  		" HISTORY #5 ", Styles,
    43  		"************",
    44  	)
    45  	f.TTY.TestBuffer(t, buf5)
    46  
    47  	f.TTY.Inject(term.K(ui.Up))
    48  	buf1 := f.MakeBuffer(
    49  		"ls -l", Styles,
    50  		"  ___", term.DotHere, "\n",
    51  		" HISTORY #1 ", Styles,
    52  		"************",
    53  	)
    54  	f.TTY.TestBuffer(t, buf1)
    55  
    56  	f.TTY.Inject(term.K(ui.Down))
    57  	f.TTY.TestBuffer(t, buf5)
    58  
    59  	f.TTY.Inject(term.K('[', ui.Ctrl))
    60  	f.TTY.TestBuffer(t, buf0)
    61  
    62  	// Start over and accept.
    63  	Start(f.App, getCfg())
    64  	f.TTY.TestBuffer(t, buf5)
    65  	f.TTY.Inject(term.K(ui.Enter))
    66  	f.TestTTY(t, "ls -a", term.DotHere)
    67  }
    68  
    69  func TestHistWalk_NoWalker(t *testing.T) {
    70  	f := Setup()
    71  	defer f.Stop()
    72  
    73  	Start(f.App, Config{})
    74  	f.TestTTYNotes(t, "no history store")
    75  }
    76  
    77  func TestHistWalk_NoMatch(t *testing.T) {
    78  	f := Setup()
    79  	defer f.Stop()
    80  
    81  	cli.SetCodeBuffer(f.App, cli.CodeBuffer{Content: "ls", Dot: 2})
    82  	f.App.Redraw()
    83  	buf0 := f.MakeBuffer("ls", term.DotHere)
    84  	f.TTY.TestBuffer(t, buf0)
    85  
    86  	store := histutil.NewMemStore("echo 1", "echo 2")
    87  	cfg := Config{Store: store, Prefix: "ls"}
    88  	Start(f.App, cfg)
    89  	// Test that an error message has been written to the notes buffer.
    90  	f.TestTTYNotes(t, "end of history")
    91  	// Test that buffer has not changed - histwalk addon is not active.
    92  	f.TTY.TestBuffer(t, buf0)
    93  }
    94  
    95  func TestHistWalk_FallbackHandler(t *testing.T) {
    96  	f := Setup()
    97  	defer f.Stop()
    98  
    99  	store := histutil.NewMemStore("ls")
   100  	Start(f.App, Config{Store: store, Prefix: ""})
   101  	f.TestTTY(t,
   102  		"ls", Styles,
   103  		"__", term.DotHere, "\n",
   104  		" HISTORY #0 ", Styles,
   105  		"************",
   106  	)
   107  
   108  	f.TTY.Inject(term.K(ui.Backspace))
   109  	f.TestTTY(t, "l", term.DotHere)
   110  }