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

     1  package mode
     2  
     3  import (
     4  	"testing"
     5  
     6  	"src.elv.sh/pkg/cli"
     7  	. "src.elv.sh/pkg/cli/clitest"
     8  	"src.elv.sh/pkg/cli/term"
     9  	"src.elv.sh/pkg/cli/tk"
    10  	"src.elv.sh/pkg/ui"
    11  )
    12  
    13  func fooAndGreenBar(string) ([]ListingItem, int) {
    14  	return []ListingItem{{"foo", ui.T("foo")}, {"bar", ui.T("bar", ui.FgGreen)}}, 0
    15  }
    16  
    17  func TestListing_BasicUI(t *testing.T) {
    18  	f := Setup()
    19  	defer f.Stop()
    20  
    21  	startListing(f.App, ListingSpec{
    22  		Caption:  " TEST ",
    23  		GetItems: fooAndGreenBar,
    24  	})
    25  	f.TestTTY(t,
    26  		"\n",
    27  		" TEST  ", Styles,
    28  		"****** ", term.DotHere, "\n",
    29  		"foo                                               \n", Styles,
    30  		"++++++++++++++++++++++++++++++++++++++++++++++++++",
    31  		"bar                                               ", Styles,
    32  		"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv",
    33  	)
    34  }
    35  
    36  func TestListing_Accept_ClosingListing(t *testing.T) {
    37  	f := Setup()
    38  	defer f.Stop()
    39  
    40  	startListing(f.App, ListingSpec{
    41  		GetItems: fooAndGreenBar,
    42  		Accept: func(t string) bool {
    43  			f.App.CodeArea().MutateState(func(s *tk.CodeAreaState) {
    44  				s.Buffer.InsertAtDot(t)
    45  			})
    46  			return false
    47  		},
    48  	})
    49  	// foo will be selected
    50  	f.TTY.Inject(term.K('\n'))
    51  	f.TestTTY(t, "foo", term.DotHere)
    52  }
    53  
    54  func TestListing_Accept_NotClosingListing(t *testing.T) {
    55  	f := Setup()
    56  	defer f.Stop()
    57  
    58  	startListing(f.App, ListingSpec{
    59  		GetItems: fooAndGreenBar,
    60  		Accept: func(t string) bool {
    61  			f.App.CodeArea().MutateState(func(s *tk.CodeAreaState) {
    62  				s.Buffer.InsertAtDot(t)
    63  			})
    64  			return true
    65  		},
    66  	})
    67  	// foo will be selected
    68  	f.TTY.Inject(term.K('\n'))
    69  	f.TestTTY(t,
    70  		"foo\n",
    71  		" LISTING  ", Styles,
    72  		"********* ", term.DotHere, "\n",
    73  		"foo                                               \n", Styles,
    74  		"++++++++++++++++++++++++++++++++++++++++++++++++++",
    75  		"bar                                               ", Styles,
    76  		"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv",
    77  	)
    78  }
    79  
    80  func TestListing_Accept_DefaultNop(t *testing.T) {
    81  	f := Setup()
    82  	defer f.Stop()
    83  
    84  	startListing(f.App, ListingSpec{GetItems: fooAndGreenBar})
    85  	f.TTY.Inject(term.K('\n'))
    86  	f.TestTTY(t /* nothing */)
    87  }
    88  
    89  func TestListing_AutoAccept(t *testing.T) {
    90  	f := Setup()
    91  	defer f.Stop()
    92  
    93  	startListing(f.App, ListingSpec{
    94  		GetItems: func(query string) ([]ListingItem, int) {
    95  			if query == "" {
    96  				// Return two items initially.
    97  				return []ListingItem{
    98  					{"foo", ui.T("foo")}, {"bar", ui.T("bar")},
    99  				}, 0
   100  			}
   101  			return []ListingItem{{"bar", ui.T("bar")}}, 0
   102  		},
   103  		Accept: func(t string) bool {
   104  			f.App.CodeArea().MutateState(func(s *tk.CodeAreaState) {
   105  				s.Buffer.InsertAtDot(t)
   106  			})
   107  			return false
   108  		},
   109  		AutoAccept: true,
   110  	})
   111  	f.TTY.Inject(term.K('a'))
   112  	f.TestTTY(t, "bar", term.DotHere)
   113  }
   114  
   115  func TestNewListing_NoGetItems(t *testing.T) {
   116  	f := Setup()
   117  	defer f.Stop()
   118  
   119  	_, err := NewListing(f.App, ListingSpec{})
   120  	if err != errGetItemsMustBeSpecified {
   121  		t.Error("expect errGetItemsMustBeSpecified")
   122  	}
   123  }
   124  
   125  func startListing(app cli.App, spec ListingSpec) {
   126  	w, err := NewListing(app, spec)
   127  	startMode(app, w, err)
   128  }