File: colview_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 (135 lines) | stat: -rw-r--r-- 3,397 bytes parent folder | download
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package tk

import (
	"testing"

	"src.elv.sh/pkg/cli/term"
	"src.elv.sh/pkg/tt"
	"src.elv.sh/pkg/ui"
)

var colViewRenderTests = []renderTest{
	{
		Name:  "colview no column",
		Given: NewColView(ColViewSpec{}),
		Width: 10, Height: 24,
		Want: &term.Buffer{Width: 10},
	},
	{
		Name: "colview width < number of columns",
		Given: NewColView(ColViewSpec{State: ColViewState{
			Columns: []Widget{
				makeListbox("x", 2, 0), makeListbox("y", 1, 0),
				makeListbox("z", 3, 0), makeListbox("w", 1, 0),
			},
		}}),
		Width: 3, Height: 24,
		Want: &term.Buffer{Width: 3},
	},
	{
		Name: "colview normal",
		Given: NewColView(ColViewSpec{State: ColViewState{
			Columns: []Widget{
				makeListbox("x", 2, 1),
				makeListbox("y", 1, 0),
				makeListbox("z", 3, -1),
			},
		}}),
		Width: 11, Height: 24,
		Want: term.NewBufferBuilder(11).
			// first line
			Write("x0  ").
			Write("y0 ", ui.Inverse).
			Write(" z0").
			// second line
			Newline().Write("x1 ", ui.Inverse).
			Write("     z1").
			// third line
			Newline().Write("        z2"),
	},
}

func makeListbox(prefix string, n, selected int) Widget {
	return NewListBox(ListBoxSpec{
		State: ListBoxState{
			Items:    TestItems{Prefix: prefix, NItems: n},
			Selected: selected,
		}})
}

func TestColView_Render(t *testing.T) {
	testRender(t, colViewRenderTests)
}

func TestColView_Handle(t *testing.T) {
	// Channel for recording the place an event was handled. -1 for the widget
	// itself, column index for column.
	handledBy := make(chan int, 10)
	w := NewColView(ColViewSpec{
		Bindings: MapBindings{
			term.K('a'): func(Widget) { handledBy <- -1 },
		},
		State: ColViewState{
			Columns: []Widget{
				NewListBox(ListBoxSpec{
					Bindings: MapBindings{
						term.K('a'): func(Widget) { handledBy <- 0 },
						term.K('b'): func(Widget) { handledBy <- 0 },
					}}),
				NewListBox(ListBoxSpec{
					Bindings: MapBindings{
						term.K('a'): func(Widget) { handledBy <- 1 },
						term.K('b'): func(Widget) { handledBy <- 1 },
					}}),
			},
			FocusColumn: 1,
		},
		OnLeft:  func(ColView) { handledBy <- 100 },
		OnRight: func(ColView) { handledBy <- 101 },
	})

	expectHandled := func(event term.Event, wantBy int) {
		t.Helper()
		handled := w.Handle(event)
		if !handled {
			t.Errorf("Handle -> false, want true")
		}
		if by := <-handledBy; by != wantBy {
			t.Errorf("Handled by %d, want %d", by, wantBy)
		}
	}

	expectUnhandled := func(event term.Event) {
		t.Helper()
		handled := w.Handle(event)
		if handled {
			t.Errorf("Handle -> true, want false")
		}
	}

	// Event handled by widget's overlay handler.
	expectHandled(term.K('a'), -1)
	// Event handled by the focused column.
	expectHandled(term.K('b'), 1)
	// Fallback handler for Left
	expectHandled(term.K(ui.Left), 100)
	// Fallback handler for Left
	expectHandled(term.K(ui.Right), 101)
	// No one to handle the event.
	expectUnhandled(term.K('c'))
	// No focused column: event unhandled
	w.MutateState(func(s *ColViewState) { s.FocusColumn = -1 })
	expectUnhandled(term.K('b'))
}

func TestDistribute(t *testing.T) {
	tt.Test(t, distribute,
		// Nice integer distributions.
		Args(10, []int{1, 1}).Rets([]int{5, 5}),
		Args(10, []int{2, 3}).Rets([]int{4, 6}),
		Args(10, []int{1, 2, 2}).Rets([]int{2, 4, 4}),
		// Approximate integer distributions.
		Args(10, []int{1, 1, 1}).Rets([]int{3, 3, 4}),
		Args(5, []int{1, 1, 1}).Rets([]int{1, 2, 2}),
	)
}