File: scrollbar_test.go

package info (click to toggle)
golang-github-jesseduffield-gocui 0.4%2Bgit20250605.fc53879%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 452 kB
  • sloc: makefile: 3
file content (114 lines) | stat: -rw-r--r-- 2,674 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
package gocui

import "testing"

func TestCalcScrollbar(t *testing.T) {
	tests := []struct {
		testName       string
		listSize       int
		pageSize       int
		position       int
		scrollAreaSize int

		expectedStart  int
		expectedHeight int
	}{
		{
			testName:       "page size greater than list size",
			listSize:       5,
			pageSize:       10,
			position:       0,
			scrollAreaSize: 20,

			expectedStart:  0,
			expectedHeight: 20,
		},
		{
			testName:       "page size matches list size",
			listSize:       10,
			pageSize:       10,
			position:       0,
			scrollAreaSize: 20,

			expectedStart:  0,
			expectedHeight: 20,
		},
		{
			testName:       "page size half of list size",
			listSize:       10,
			pageSize:       5,
			position:       0,
			scrollAreaSize: 20,

			expectedStart:  0,
			expectedHeight: 10,
		},
		{
			testName:       "page size half of list size at scroll end",
			listSize:       10,
			pageSize:       5,
			position:       5,
			scrollAreaSize: 20,

			expectedStart:  10,
			expectedHeight: 10,
		},
		{
			testName: "page size third of list size having scrolled half the way",
			listSize: 15,
			// Recall that my max position is listSize - pageSize i.e 15 - 5 i.e. 10.
			// So if I've scrolled to position 5 that means I've done one page and I've got
			// one page to go which means by scrollbar should take up a third of the available
			// space and appear in the centre of the scrollbar area
			pageSize:       5,
			position:       5,
			scrollAreaSize: 21,

			expectedStart:  7,
			expectedHeight: 7,
		},
		{
			testName:       "page size third of list size having scrolled the full way",
			listSize:       15,
			pageSize:       5,
			position:       10,
			scrollAreaSize: 21,

			expectedStart:  14,
			expectedHeight: 7,
		},
		{
			testName:       "page size third of list size having scrolled by one",
			listSize:       15,
			pageSize:       5,
			position:       1,
			scrollAreaSize: 21,

			expectedStart:  2,
			expectedHeight: 7,
		},
		{
			testName:       "page size third of list size having scrolled up from the bottom by one",
			listSize:       15,
			pageSize:       5,
			position:       9,
			scrollAreaSize: 21,

			expectedStart:  12,
			expectedHeight: 7,
		},
	}

	for _, test := range tests {
		t.Run(test.testName, func(t *testing.T) {
			start, height := calcScrollbar(test.listSize, test.pageSize, test.position, test.scrollAreaSize)
			if start != test.expectedStart {
				t.Errorf("expected start to be %d, got %d", test.expectedStart, start)
			}

			if height != test.expectedHeight {
				t.Errorf("expected height to be %d, got %d", test.expectedHeight, height)
			}
		})
	}
}