File: layout_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 (117 lines) | stat: -rw-r--r-- 2,505 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
package tk

import (
	"reflect"
	"testing"

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

var layoutRenderTests = []struct {
	name     string
	renderer Renderer
	width    int
	height   int
	wantBuf  *term.BufferBuilder
}{
	{
		"empty widget",
		Empty{},
		10, 24,
		bb(10),
	},
	{
		"Label showing all",
		Label{ui.T("label")},
		10, 24,
		bb(10).Write("label"),
	},
	{
		"Label cropping",
		Label{ui.T("label")},
		4, 1,
		bb(4).Write("labe"),
	},
	{
		"VScrollbar showing full thumb",
		VScrollbar{4, 0, 3},
		10, 2,
		bb(1).WriteStyled(vscrollbarThumb).WriteStyled(vscrollbarThumb),
	},
	{
		"VScrollbar showing thumb in first half",
		VScrollbar{4, 0, 1},
		10, 2,
		bb(1).WriteStyled(vscrollbarThumb).WriteStyled(vscrollbarTrough),
	},
	{
		"VScrollbar showing a minimal 1-size thumb at beginning",
		VScrollbar{4, 0, 0},
		10, 2,
		bb(1).WriteStyled(vscrollbarThumb).WriteStyled(vscrollbarTrough),
	},
	{
		"VScrollbar showing a minimal 1-size thumb at end",
		VScrollbar{4, 3, 3},
		10, 2,
		bb(1).WriteStyled(vscrollbarTrough).WriteStyled(vscrollbarThumb),
	},
	{
		"VScrollbarContainer",
		VScrollbarContainer{Label{ui.T("abcd1234")},
			VScrollbar{4, 0, 1}},
		5, 2,
		bb(5).Write("abcd").WriteStyled(vscrollbarThumb).
			Newline().Write("1234").WriteStyled(vscrollbarTrough),
	},
	{
		"HScrollbar showing full thumb",
		HScrollbar{4, 0, 3},
		2, 10,
		bb(2).WriteStyled(hscrollbarThumb).WriteStyled(hscrollbarThumb),
	},
	{
		"HScrollbar showing thumb in first half",
		HScrollbar{4, 0, 1},
		2, 10,
		bb(2).WriteStyled(hscrollbarThumb).WriteStyled(hscrollbarTrough),
	},
	{
		"HScrollbar showing a minimal 1-size thumb at beginning",
		HScrollbar{4, 0, 0},
		2, 10,
		bb(2).WriteStyled(hscrollbarThumb).WriteStyled(hscrollbarTrough),
	},
	{
		"HScrollbar showing a minimal 1-size thumb at end",
		HScrollbar{4, 3, 3},
		2, 10,
		bb(2).WriteStyled(hscrollbarTrough).WriteStyled(hscrollbarThumb),
	},
}

func TestLayout_Render(t *testing.T) {
	for _, test := range layoutRenderTests {
		t.Run(test.name, func(t *testing.T) {
			buf := test.renderer.Render(test.width, test.height)
			wantBuf := test.wantBuf.Buffer()
			if !reflect.DeepEqual(buf, wantBuf) {
				t.Errorf("got buf %v, want %v", buf, wantBuf)
			}
		})
	}
}

var nopHandlers = []Handler{
	Empty{}, Label{ui.T("label")},
}

func TestLayout_Handle(t *testing.T) {
	for _, handler := range nopHandlers {
		handled := handler.Handle(term.K('a'))
		if handled {
			t.Errorf("%v handles event when it shouldn't", handler)
		}
	}
}