src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/cli/modes/listing_test.go (about) 1 package modes 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) { 43 f.App.ActiveWidget().(tk.CodeArea).MutateState(func(s *tk.CodeAreaState) { 44 s.Buffer.InsertAtDot(t) 45 }) 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 TestListing_Accept_DefaultNop(t *testing.T) { 54 f := Setup() 55 defer f.Stop() 56 57 startListing(f.App, ListingSpec{GetItems: fooAndGreenBar}) 58 f.TTY.Inject(term.K('\n')) 59 f.TestTTY(t /* nothing */) 60 } 61 62 func TestListing_AutoAccept(t *testing.T) { 63 f := Setup() 64 defer f.Stop() 65 66 startListing(f.App, ListingSpec{ 67 GetItems: func(query string) ([]ListingItem, int) { 68 if query == "" { 69 // Return two items initially. 70 return []ListingItem{ 71 {"foo", ui.T("foo")}, {"bar", ui.T("bar")}, 72 }, 0 73 } 74 return []ListingItem{{"bar", ui.T("bar")}}, 0 75 }, 76 Accept: func(t string) { 77 f.App.ActiveWidget().(tk.CodeArea).MutateState(func(s *tk.CodeAreaState) { 78 s.Buffer.InsertAtDot(t) 79 }) 80 }, 81 AutoAccept: true, 82 }) 83 f.TTY.Inject(term.K('a')) 84 f.TestTTY(t, "bar", term.DotHere) 85 } 86 87 func TestNewListing_NoGetItems(t *testing.T) { 88 f := Setup() 89 defer f.Stop() 90 91 _, err := NewListing(f.App, ListingSpec{}) 92 if err != errGetItemsMustBeSpecified { 93 t.Error("expect errGetItemsMustBeSpecified") 94 } 95 } 96 97 func startListing(app cli.App, spec ListingSpec) { 98 w, err := NewListing(app, spec) 99 startMode(app, w, err) 100 }