github.com/elves/elvish@v0.15.0/pkg/edit/listing_test.go (about)

     1  package edit
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/elves/elvish/pkg/cli/term"
     7  	"github.com/elves/elvish/pkg/store"
     8  	"github.com/elves/elvish/pkg/ui"
     9  )
    10  
    11  /*
    12  func TestInitListing_Binding(t *testing.T) {
    13  	// Test that the binding variable in the returned namespace indeed refers to
    14  	// the BindingMap returned.
    15  	_, binding, ns := initListing(&fakeApp{})
    16  	if ns["binding"].Get() != *binding {
    17  		t.Errorf("The binding var in the ns is not the same as the BindingMap")
    18  	}
    19  }
    20  */
    21  
    22  // Smoke tests for individual addons.
    23  
    24  func TestHistlistAddon(t *testing.T) {
    25  	f := setup(storeOp(func(s store.Store) {
    26  		s.AddCmd("ls")
    27  		s.AddCmd("echo")
    28  		s.AddCmd("ls")
    29  	}))
    30  	defer f.Cleanup()
    31  
    32  	f.TTYCtrl.Inject(term.K('R', ui.Ctrl))
    33  	f.TestTTY(t,
    34  		"~> \n",
    35  		" HISTORY (dedup on)  ", Styles,
    36  		"******************** ", term.DotHere, "\n",
    37  		"   2 echo\n",
    38  		"   3 ls                                           ", Styles,
    39  		"++++++++++++++++++++++++++++++++++++++++++++++++++",
    40  	)
    41  
    42  	evals(f.Evaler, `edit:histlist:toggle-dedup`)
    43  	f.TestTTY(t,
    44  		"~> \n",
    45  		" HISTORY  ", Styles,
    46  		"********* ", term.DotHere, "\n",
    47  		"   1 ls\n",
    48  		"   2 echo\n",
    49  		"   3 ls                                           ", Styles,
    50  		"++++++++++++++++++++++++++++++++++++++++++++++++++",
    51  	)
    52  
    53  	evals(f.Evaler, `edit:histlist:toggle-case-sensitivity`)
    54  	f.TestTTY(t,
    55  		"~> \n",
    56  		" HISTORY (case-insensitive)  ", Styles,
    57  		"**************************** ", term.DotHere, "\n",
    58  		"   1 ls\n",
    59  		"   2 echo\n",
    60  		"   3 ls                                           ", Styles,
    61  		"++++++++++++++++++++++++++++++++++++++++++++++++++",
    62  	)
    63  }
    64  
    65  func TestLastCmdAddon(t *testing.T) {
    66  	f := setup(storeOp(func(s store.Store) {
    67  		s.AddCmd("echo hello world")
    68  	}))
    69  	defer f.Cleanup()
    70  
    71  	f.TTYCtrl.Inject(term.K(',', ui.Alt))
    72  	f.TestTTY(t,
    73  		"~> \n",
    74  		" LASTCMD  ", Styles,
    75  		"********* ", term.DotHere, "\n",
    76  		"    echo hello world                              \n", Styles,
    77  		"++++++++++++++++++++++++++++++++++++++++++++++++++",
    78  		"  0 echo\n",
    79  		"  1 hello\n",
    80  		"  2 world",
    81  	)
    82  }
    83  
    84  func TestCustomListing_PassingList(t *testing.T) {
    85  	f := setup()
    86  	defer f.Cleanup()
    87  
    88  	evals(f.Evaler,
    89  		`items = [[&to-filter=1 &to-accept=echo &to-show=echo]
    90  		          [&to-filter=2  &to-accept=put &to-show=(styled put green)]]`,
    91  		`edit:listing:start-custom $items &accept=$edit:insert-at-dot~ &caption=A`)
    92  	f.TestTTY(t,
    93  		"~> \n",
    94  		"A ", Styles,
    95  		"* ", term.DotHere, "\n",
    96  		"echo                                              \n", Styles,
    97  		"++++++++++++++++++++++++++++++++++++++++++++++++++",
    98  		"put                                               ", Styles,
    99  		"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv",
   100  	)
   101  	// Filter - "put" will be selected.
   102  	f.TTYCtrl.Inject(term.K('2'))
   103  	// Accept.
   104  	f.TTYCtrl.Inject(term.K('\n'))
   105  	f.TestTTY(t,
   106  		"~> put", Styles,
   107  		"   vvv", term.DotHere,
   108  	)
   109  }
   110  
   111  func TestCustomListing_PassingValueCallback(t *testing.T) {
   112  	f := setup()
   113  	defer f.Cleanup()
   114  
   115  	evals(f.Evaler,
   116  		`f = [q]{ put [&to-accept='q '$q &to-show=(styled 'q '$q blue)] }`,
   117  		`edit:listing:start-custom $f &caption=A`)
   118  	// Query.
   119  	f.TTYCtrl.Inject(term.K('x'))
   120  	f.TestTTY(t,
   121  		"~> \n",
   122  		"A x", Styles,
   123  		"*  ", term.DotHere, "\n",
   124  		"q x                                               ", Styles,
   125  		"##################################################",
   126  	)
   127  	// No-op accept.
   128  	f.TTYCtrl.Inject(term.K('\n'))
   129  	f.TestTTY(t, "~> ", term.DotHere)
   130  }
   131  
   132  func TestCustomListing_PassingBytesCallback(t *testing.T) {
   133  	f := setup()
   134  	defer f.Cleanup()
   135  
   136  	evals(f.Evaler,
   137  		`f = [q]{ echo 'q '$q }`,
   138  		`edit:listing:start-custom $f &accept=$edit:insert-at-dot~ &caption=A`)
   139  	// Query.
   140  	f.TTYCtrl.Inject(term.K('x'))
   141  	f.TestTTY(t,
   142  		"~> \n",
   143  		"A x", Styles,
   144  		"*  ", term.DotHere, "\n",
   145  		"q x                                               ", Styles,
   146  		"++++++++++++++++++++++++++++++++++++++++++++++++++",
   147  	)
   148  }