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

     1  package completion
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/elves/elvish/pkg/cli/clitest"
     7  	"github.com/elves/elvish/pkg/cli/term"
     8  	"github.com/elves/elvish/pkg/diag"
     9  	"github.com/elves/elvish/pkg/ui"
    10  )
    11  
    12  func setupStarted(t *testing.T) *Fixture {
    13  	f := Setup()
    14  	Start(f.App, Config{
    15  		Name:    "WORD",
    16  		Replace: diag.Ranging{From: 0, To: 0},
    17  		Items: []Item{
    18  			{ToShow: "foo", ToInsert: "foo"},
    19  			{ToShow: "foo bar", ToInsert: "'foo bar'",
    20  				ShowStyle: ui.Style{Foreground: ui.Blue}},
    21  		},
    22  	})
    23  	f.TestTTY(t,
    24  		"foo\n", Styles,
    25  		"___",
    26  		" COMPLETING WORD  ", Styles,
    27  		"***************** ", term.DotHere, "\n",
    28  		"foo  foo bar", Styles,
    29  		"+++  ///////",
    30  	)
    31  	return f
    32  }
    33  
    34  func TestFilter(t *testing.T) {
    35  	f := setupStarted(t)
    36  	defer f.Stop()
    37  
    38  	f.TTY.Inject(term.K('b'), term.K('a'))
    39  	f.TestTTY(t,
    40  		"'foo bar'\n", Styles,
    41  		"_________",
    42  		" COMPLETING WORD  ba", Styles,
    43  		"*****************   ", term.DotHere, "\n",
    44  		"foo bar", Styles,
    45  		"#######",
    46  	)
    47  }
    48  
    49  func TestAccept(t *testing.T) {
    50  	f := setupStarted(t)
    51  	defer f.Stop()
    52  
    53  	f.TTY.Inject(term.K(ui.Enter))
    54  	f.TestTTY(t, "foo", term.DotHere)
    55  }
    56  
    57  func TestClose(t *testing.T) {
    58  	f := setupStarted(t)
    59  	defer f.Stop()
    60  
    61  	Close(f.App)
    62  	f.TestTTY(t /* nothing */)
    63  }
    64  
    65  func TestStart_NoItems(t *testing.T) {
    66  	f := Setup()
    67  	defer f.Stop()
    68  	Start(f.App, Config{Items: []Item{}})
    69  	f.TestTTYNotes(t, "no candidates")
    70  }