github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/cli/mode/completion_test.go (about) 1 package mode 2 3 import ( 4 "testing" 5 6 . "src.elv.sh/pkg/cli/clitest" 7 "src.elv.sh/pkg/cli/term" 8 "src.elv.sh/pkg/diag" 9 "src.elv.sh/pkg/ui" 10 ) 11 12 func TestCompletion_Filter(t *testing.T) { 13 f := setupStartedCompletion(t) 14 defer f.Stop() 15 16 f.TTY.Inject(term.K('b'), term.K('a')) 17 f.TestTTY(t, 18 "'foo bar'\n", Styles, 19 "_________", 20 " COMPLETING WORD ba", Styles, 21 "***************** ", term.DotHere, "\n", 22 "foo bar", Styles, 23 "#######", 24 ) 25 } 26 27 func TestCompletion_Accept(t *testing.T) { 28 f := setupStartedCompletion(t) 29 defer f.Stop() 30 31 f.TTY.Inject(term.K(ui.Enter)) 32 f.TestTTY(t, "foo", term.DotHere) 33 } 34 35 func TestCompletion_Close(t *testing.T) { 36 f := setupStartedCompletion(t) 37 defer f.Stop() 38 39 f.App.SetAddon(nil, false) 40 f.App.Redraw() 41 f.TestTTY(t /* nothing */) 42 } 43 44 func TestNewCompletion_NoItems(t *testing.T) { 45 f := Setup() 46 defer f.Stop() 47 _, err := NewCompletion(f.App, CompletionSpec{Items: []CompletionItem{}}) 48 if err != errNoCandidates { 49 t.Errorf("should return errNoCandidates") 50 } 51 } 52 53 func setupStartedCompletion(t *testing.T) *Fixture { 54 f := Setup() 55 w, _ := NewCompletion(f.App, CompletionSpec{ 56 Name: "WORD", 57 Replace: diag.Ranging{From: 0, To: 0}, 58 Items: []CompletionItem{ 59 {ToShow: "foo", ToInsert: "foo"}, 60 {ToShow: "foo bar", ToInsert: "'foo bar'", 61 ShowStyle: ui.Style{Foreground: ui.Blue}}, 62 }, 63 }) 64 f.App.SetAddon(w, false) 65 f.App.Redraw() 66 f.TestTTY(t, 67 "foo\n", Styles, 68 "___", 69 " COMPLETING WORD ", Styles, 70 "***************** ", term.DotHere, "\n", 71 "foo foo bar", Styles, 72 "+++ ///////", 73 ) 74 return f 75 }