File: textview_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,619 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
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 (
	"reflect"
	"testing"

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

var textViewRenderTests = []renderTest{
	{
		Name: "text fits entirely",
		Given: NewTextView(TextViewSpec{State: TextViewState{
			Lines: []string{"line 1", "line 2", "line 3"}}}),
		Width: 10, Height: 4,
		Want: bb(10).
			Write("line 1").Newline().
			Write("line 2").Newline().
			Write("line 3").Buffer(),
	},
	{
		Name: "text cropped horizontally",
		Given: NewTextView(TextViewSpec{State: TextViewState{
			Lines: []string{"a very long line"}}}),
		Width: 10, Height: 4,
		Want: bb(10).
			Write("a very lon").Buffer(),
	},
	{
		Name: "text cropped vertically",
		Given: NewTextView(TextViewSpec{State: TextViewState{
			Lines: []string{"line 1", "line 2", "line 3"}}}),
		Width: 10, Height: 2,
		Want: bb(10).
			Write("line 1").Newline().
			Write("line 2").Buffer(),
	},
	{
		Name: "text cropped vertically, with scrollbar",
		Given: NewTextView(TextViewSpec{
			Scrollable: true,
			State: TextViewState{
				Lines: []string{"line 1", "line 2", "line 3", "line 4"}}}),
		Width: 10, Height: 2,
		Want: bb(10).
			Write("line 1   ").
			Write(" ", ui.Inverse, ui.FgMagenta).Newline().
			Write("line 2   ").
			Write("│", ui.FgMagenta).Buffer(),
	},
	{
		Name: "State.First adjusted to fit text",
		Given: NewTextView(TextViewSpec{State: TextViewState{
			First: 2,
			Lines: []string{"line 1", "line 2", "line 3"}}}),
		Width: 10, Height: 3,
		Want: bb(10).
			Write("line 1").Newline().
			Write("line 2").Newline().
			Write("line 3").Buffer(),
	},
}

func TestTextView_Render(t *testing.T) {
	testRender(t, textViewRenderTests)
}

var textViewHandleTests = []handleTest{
	{
		Name: "up doing nothing when not scrollable",
		Given: NewTextView(TextViewSpec{
			State: TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 1}}),
		Event: term.K(ui.Up),

		WantUnhandled: true,
	},
	{
		Name: "up moving window up when scrollable",
		Given: NewTextView(TextViewSpec{
			Scrollable: true,
			State:      TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 1}}),
		Event: term.K(ui.Up),

		WantNewState: TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 0},
	},
	{
		Name: "up doing nothing when already at top",
		Given: NewTextView(TextViewSpec{
			Scrollable: true,
			State:      TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 0}}),
		Event: term.K(ui.Up),

		WantNewState: TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 0},
	},
	{
		Name: "down moving window down when scrollable",
		Given: NewTextView(TextViewSpec{
			Scrollable: true,
			State:      TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 1}}),
		Event: term.K(ui.Down),

		WantNewState: TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 2},
	},
	{
		Name: "down doing nothing when already at bottom",
		Given: NewTextView(TextViewSpec{
			Scrollable: true,
			State:      TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 3}}),
		Event: term.K(ui.Down),

		WantNewState: TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 3},
	},
	{
		Name: "bindings",
		Given: NewTextView(TextViewSpec{
			Bindings: MapBindings{term.K('a'): func(Widget) {}}}),
		Event: term.K('a'),

		WantNewState: TextViewState{},
	},
}

func TestTextView_Handle(t *testing.T) {
	testHandle(t, textViewHandleTests)
}

func TestTextView_CopyState(t *testing.T) {
	state := TextViewState{Lines: []string{"a", "b", "c"}, First: 1}
	w := NewTextView(TextViewSpec{State: state})
	copied := w.CopyState()
	if !reflect.DeepEqual(copied, state) {
		t.Errorf("Got copied state %v, want %v", copied, state)
	}
}