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

     1  package histlist
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     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  func TestStart_NoStore(t *testing.T) {
    17  	f := Setup()
    18  	defer f.Stop()
    19  
    20  	Start(f.App, Config{})
    21  	f.TestTTYNotes(t, "no history store")
    22  }
    23  
    24  type faultyStore struct{}
    25  
    26  var errMock = errors.New("mock error")
    27  
    28  func (s faultyStore) AllCmds() ([]store.Cmd, error) { return nil, errMock }
    29  
    30  func TestStart_StoreError(t *testing.T) {
    31  	f := Setup()
    32  	defer f.Stop()
    33  
    34  	Start(f.App, Config{Store: faultyStore{}})
    35  	f.TestTTYNotes(t, "db error: mock error")
    36  }
    37  
    38  func TestStart_OK(t *testing.T) {
    39  	f := Setup()
    40  	defer f.Stop()
    41  
    42  	st := histutil.NewMemStore(
    43  		// 0    1      2
    44  		"foo", "bar", "baz")
    45  	Start(f.App, Config{Store: st})
    46  
    47  	// Test UI.
    48  	f.TTY.TestBuffer(t,
    49  		makeListingBuf(
    50  			" HISTORY (dedup on) ", "",
    51  			"   0 foo",
    52  			"   1 bar",
    53  			"   2 baz"))
    54  
    55  	// Test filtering.
    56  	f.TTY.Inject(term.K('b'))
    57  	f.TTY.TestBuffer(t,
    58  		makeListingBuf(
    59  			" HISTORY (dedup on) ", "b",
    60  			"   1 bar",
    61  			"   2 baz"))
    62  
    63  	// Test accepting.
    64  	f.TTY.Inject(term.K(ui.Enter))
    65  	f.TestTTY(t, "baz", term.DotHere)
    66  
    67  	// Test accepting when there is already some text.
    68  	st.AddCmd(store.Cmd{Text: "baz2"})
    69  	Start(f.App, Config{Store: st})
    70  	f.TTY.Inject(term.K(ui.Enter))
    71  	f.TestTTY(t, "baz",
    72  		// codearea now contains newly inserted entry on a separate line
    73  		"\n", "baz2", term.DotHere)
    74  }
    75  
    76  func TestStart_Dedup(t *testing.T) {
    77  	f := Setup()
    78  	defer f.Stop()
    79  
    80  	st := histutil.NewMemStore(
    81  		// 0    1      2
    82  		"ls", "echo", "ls")
    83  
    84  	// No dedup
    85  	Start(f.App, Config{Store: st, Dedup: func() bool { return false }})
    86  	f.TTY.TestBuffer(t,
    87  		makeListingBuf(
    88  			" HISTORY ", "",
    89  			"   0 ls",
    90  			"   1 echo",
    91  			"   2 ls"))
    92  	f.App.MutateState(func(s *cli.State) { s.Addon = nil })
    93  
    94  	// With dedup
    95  	Start(f.App, Config{Store: st, Dedup: func() bool { return true }})
    96  	f.TTY.TestBuffer(t,
    97  		makeListingBuf(
    98  			" HISTORY (dedup on) ", "",
    99  			"   1 echo",
   100  			"   2 ls"))
   101  }
   102  
   103  func TestStart_CaseSensitive(t *testing.T) {
   104  	f := Setup()
   105  	defer f.Stop()
   106  
   107  	st := histutil.NewMemStore(
   108  		// 0  1
   109  		"ls", "LS")
   110  
   111  	// Case sensitive
   112  	Start(f.App, Config{Store: st, CaseSensitive: func() bool { return true }})
   113  	f.TTY.Inject(term.K('l'))
   114  	f.TTY.TestBuffer(t,
   115  		makeListingBuf(
   116  			" HISTORY (dedup on) ", "l",
   117  			"   0 ls"))
   118  	f.App.MutateState(func(s *cli.State) { s.Addon = nil })
   119  
   120  	// Case insensitive
   121  	Start(f.App, Config{Store: st, CaseSensitive: func() bool { return false }})
   122  	f.TTY.Inject(term.K('l'))
   123  	f.TTY.TestBuffer(t,
   124  		makeListingBuf(
   125  			" HISTORY (dedup on) (case-insensitive) ", "l",
   126  			"   0 ls",
   127  			"   1 LS"))
   128  }
   129  
   130  func bb() *term.BufferBuilder { return term.NewBufferBuilder(50) }
   131  
   132  func makeListingBuf(mode, filter string, lines ...string) *term.Buffer {
   133  	b := bb().Newline().
   134  		WriteStyled(cli.ModeLine(mode, true)).
   135  		Write(filter).SetDotHere()
   136  	for i, line := range lines {
   137  		b.Newline()
   138  		if i < len(lines)-1 {
   139  			b.Write(line)
   140  		} else {
   141  			b.WriteStyled(
   142  				ui.T(fmt.Sprintf("%-50s", line), ui.Inverse))
   143  		}
   144  	}
   145  	return b.Buffer()
   146  }