File: listing_test.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (100 lines) | stat: -rw-r--r-- 2,292 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package modes

import (
	"testing"

	"src.elv.sh/pkg/cli"
	. "src.elv.sh/pkg/cli/clitest"
	"src.elv.sh/pkg/cli/term"
	"src.elv.sh/pkg/cli/tk"
	"src.elv.sh/pkg/ui"
)

func fooAndGreenBar(string) ([]ListingItem, int) {
	return []ListingItem{{"foo", ui.T("foo")}, {"bar", ui.T("bar", ui.FgGreen)}}, 0
}

func TestListing_BasicUI(t *testing.T) {
	f := Setup()
	defer f.Stop()

	startListing(f.App, ListingSpec{
		Caption:  " TEST ",
		GetItems: fooAndGreenBar,
	})
	f.TestTTY(t,
		"\n",
		" TEST  ", Styles,
		"****** ", term.DotHere, "\n",
		"foo                                               \n", Styles,
		"++++++++++++++++++++++++++++++++++++++++++++++++++",
		"bar                                               ", Styles,
		"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv",
	)
}

func TestListing_Accept_ClosingListing(t *testing.T) {
	f := Setup()
	defer f.Stop()

	startListing(f.App, ListingSpec{
		GetItems: fooAndGreenBar,
		Accept: func(t string) {
			f.App.ActiveWidget().(tk.CodeArea).MutateState(func(s *tk.CodeAreaState) {
				s.Buffer.InsertAtDot(t)
			})
		},
	})
	// foo will be selected
	f.TTY.Inject(term.K('\n'))
	f.TestTTY(t, "foo", term.DotHere)
}

func TestListing_Accept_DefaultNop(t *testing.T) {
	f := Setup()
	defer f.Stop()

	startListing(f.App, ListingSpec{GetItems: fooAndGreenBar})
	f.TTY.Inject(term.K('\n'))
	f.TestTTY(t /* nothing */)
}

func TestListing_AutoAccept(t *testing.T) {
	f := Setup()
	defer f.Stop()

	startListing(f.App, ListingSpec{
		GetItems: func(query string) ([]ListingItem, int) {
			if query == "" {
				// Return two items initially.
				return []ListingItem{
					{"foo", ui.T("foo")}, {"bar", ui.T("bar")},
				}, 0
			}
			return []ListingItem{{"bar", ui.T("bar")}}, 0
		},
		Accept: func(t string) {
			f.App.ActiveWidget().(tk.CodeArea).MutateState(func(s *tk.CodeAreaState) {
				s.Buffer.InsertAtDot(t)
			})
		},
		AutoAccept: true,
	})
	f.TTY.Inject(term.K('a'))
	f.TestTTY(t, "bar", term.DotHere)
}

func TestNewListing_NoGetItems(t *testing.T) {
	f := Setup()
	defer f.Stop()

	_, err := NewListing(f.App, ListingSpec{})
	if err != errGetItemsMustBeSpecified {
		t.Error("expect errGetItemsMustBeSpecified")
	}
}

func startListing(app cli.App, spec ListingSpec) {
	w, err := NewListing(app, spec)
	startMode(app, w, err)
}