github.com/elves/elvish@v0.15.0/pkg/cli/addons/listing/listing_test.go (about) 1 package listing 2 3 import ( 4 "testing" 5 6 "github.com/elves/elvish/pkg/cli" 7 . "github.com/elves/elvish/pkg/cli/clitest" 8 "github.com/elves/elvish/pkg/cli/term" 9 "github.com/elves/elvish/pkg/ui" 10 ) 11 12 func fooAndGreenBar(string) ([]Item, int) { 13 return []Item{{"foo", ui.T("foo")}, {"bar", ui.T("bar", ui.FgGreen)}}, 0 14 } 15 16 func TestBasicUI(t *testing.T) { 17 f := Setup() 18 defer f.Stop() 19 20 Start(f.App, Config{ 21 Caption: " TEST ", 22 GetItems: fooAndGreenBar, 23 }) 24 f.TestTTY(t, 25 "\n", 26 " TEST ", Styles, 27 "****** ", term.DotHere, "\n", 28 "foo \n", Styles, 29 "++++++++++++++++++++++++++++++++++++++++++++++++++", 30 "bar ", Styles, 31 "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv", 32 ) 33 } 34 35 func TestAccept_ClosingListing(t *testing.T) { 36 f := Setup() 37 defer f.Stop() 38 39 Start(f.App, Config{ 40 GetItems: fooAndGreenBar, 41 Accept: func(t string) bool { 42 f.App.CodeArea().MutateState(func(s *cli.CodeAreaState) { 43 s.Buffer.InsertAtDot(t) 44 }) 45 return false 46 }, 47 }) 48 // foo will be selected 49 f.TTY.Inject(term.K('\n')) 50 f.TestTTY(t, "foo", term.DotHere) 51 } 52 53 func TestAccept_NotClosingListing(t *testing.T) { 54 f := Setup() 55 defer f.Stop() 56 57 Start(f.App, Config{ 58 GetItems: fooAndGreenBar, 59 Accept: func(t string) bool { 60 f.App.CodeArea().MutateState(func(s *cli.CodeAreaState) { 61 s.Buffer.InsertAtDot(t) 62 }) 63 return true 64 }, 65 }) 66 // foo will be selected 67 f.TTY.Inject(term.K('\n')) 68 f.TestTTY(t, 69 "foo\n", 70 " LISTING ", Styles, 71 "********* ", term.DotHere, "\n", 72 "foo \n", Styles, 73 "++++++++++++++++++++++++++++++++++++++++++++++++++", 74 "bar ", Styles, 75 "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv", 76 ) 77 } 78 79 func TestAccept_DefaultNop(t *testing.T) { 80 f := Setup() 81 defer f.Stop() 82 83 Start(f.App, Config{GetItems: fooAndGreenBar}) 84 f.TTY.Inject(term.K('\n')) 85 f.TestTTY(t /* nothing */) 86 } 87 88 func TestAutoAccept(t *testing.T) { 89 f := Setup() 90 defer f.Stop() 91 92 Start(f.App, Config{ 93 GetItems: func(query string) ([]Item, int) { 94 if query == "" { 95 // Return two items initially. 96 return []Item{ 97 {"foo", ui.T("foo")}, {"bar", ui.T("bar")}, 98 }, 0 99 } 100 return []Item{{"bar", ui.T("bar")}}, 0 101 }, 102 Accept: func(t string) bool { 103 f.App.CodeArea().MutateState(func(s *cli.CodeAreaState) { 104 s.Buffer.InsertAtDot(t) 105 }) 106 return false 107 }, 108 AutoAccept: true, 109 }) 110 f.TTY.Inject(term.K('a')) 111 f.TestTTY(t, "bar", term.DotHere) 112 } 113 114 func TestAbortWhenGetItemsUnspecified(t *testing.T) { 115 f := Setup() 116 defer f.Stop() 117 118 Start(f.App, Config{}) 119 f.TestTTYNotes(t, "internal error: GetItems must be specified") 120 }