File: mouse_test.go

package info (click to toggle)
golang-github-charmbracelet-x 0.0~git20251028.0cf22f8%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,940 kB
  • sloc: sh: 124; makefile: 5
file content (243 lines) | stat: -rw-r--r-- 4,791 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package ansi

import (
	"fmt"
	"testing"
)

func TestMouseButton(t *testing.T) {
	type test struct {
		name                     string
		btn                      MouseButton
		motion, shift, alt, ctrl bool
		want                     byte
	}

	cases := []test{
		{
			name: "mouse release",
			btn:  MouseNone,
			want: 0b0000_0011,
		},
		{
			name: "mouse release with ctrl",
			btn:  MouseNone,
			ctrl: true,
			want: 0b0001_0011,
		},
		{
			name: "mouse left",
			btn:  MouseLeft,
			want: 0b0000_0000,
		},
		{
			name: "mouse right",
			btn:  MouseRight,
			want: 0b0000_0010,
		},
		{
			name: "mouse wheel up",
			btn:  MouseWheelUp,
			want: 0b0100_0000,
		},
		{
			name: "mouse wheel right",
			btn:  MouseWheelRight,
			want: 0b0100_0011,
		},
		{
			name: "mouse backward",
			btn:  MouseBackward,
			want: 0b1000_0000,
		},
		{
			name: "mouse forward",
			btn:  MouseForward,
			want: 0b1000_0001,
		},
		{
			name: "mouse button 10",
			btn:  MouseButton10,
			want: 0b1000_0010,
		},
		{
			name: "mouse button 11",
			btn:  MouseButton11,
			want: 0b1000_0011,
		},
		{
			name:   "mouse middle with motion",
			btn:    MouseMiddle,
			motion: true,
			want:   0b0010_0001,
		},
		{
			name:  "mouse middle with shift",
			btn:   MouseMiddle,
			shift: true,
			want:  0b0000_0101,
		},
		{
			name:   "mouse middle with motion and alt",
			btn:    MouseMiddle,
			motion: true,
			alt:    true,
			want:   0b0010_1001,
		},
		{
			name:  "mouse right with shift, alt, and ctrl",
			btn:   MouseRight,
			shift: true,
			alt:   true,
			ctrl:  true,
			want:  0b0001_1110,
		},
		{
			name:   "mouse button 10 with motion, shift, alt, and ctrl",
			btn:    MouseButton10,
			motion: true,
			shift:  true,
			alt:    true,
			ctrl:   true,
			want:   0b1011_1110,
		},
		{
			name:   "mouse left with motion, shift, and ctrl",
			btn:    MouseLeft,
			motion: true,
			shift:  true,
			ctrl:   true,
			want:   0b0011_0100,
		},
		{
			name: "invalid mouse button",
			btn:  MouseButton(0xff),
			want: 0b1111_1111,
		},
		{
			name:   "mouse wheel down with motion",
			btn:    MouseWheelDown,
			motion: true,
			want:   0b0110_0001,
		},
		{
			name:  "mouse wheel down with shift and ctrl",
			btn:   MouseWheelDown,
			shift: true,
			ctrl:  true,
			want:  0b0101_0101,
		},
		{
			name: "mouse wheel left with alt",
			btn:  MouseWheelLeft,
			alt:  true,
			want: 0b0100_1010,
		},
		{
			name:   "mouse middle with all modifiers",
			btn:    MouseMiddle,
			motion: true,
			shift:  true,
			alt:    true,
			ctrl:   true,
			want:   0b0011_1101,
		},
	}

	for i, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got := EncodeMouseButton(tc.btn, tc.motion, tc.shift, tc.alt, tc.ctrl)
			if got != tc.want {
				t.Errorf("test %d: got %08b; want %08b", i+1, got, tc.want)
			}
		})
	}
}

func TestMouseSgr(t *testing.T) {
	type test struct {
		name    string
		btn     byte
		x, y    int
		release bool
	}

	cases := []test{
		{
			name: "mouse left",
			btn:  EncodeMouseButton(MouseLeft, false, false, false, false),
			x:    0,
			y:    0,
		},
		{
			name: "wheel down",
			btn:  EncodeMouseButton(MouseWheelDown, false, false, false, false),
			x:    1,
			y:    10,
		},
		{
			name: "mouse right with shift, alt, and ctrl",
			btn:  EncodeMouseButton(MouseRight, false, true, true, true),
			x:    10,
			y:    1,
		},
		{
			name:    "mouse release",
			btn:     EncodeMouseButton(MouseNone, false, false, false, false),
			x:       5,
			y:       5,
			release: true,
		},
		{
			name: "mouse button 10 with motion, shift, alt, and ctrl",
			btn:  EncodeMouseButton(MouseButton10, true, true, true, true),
			x:    10,
			y:    10,
		},
		{
			name: "mouse wheel up with motion",
			btn:  EncodeMouseButton(MouseWheelUp, true, false, false, false),
			x:    15,
			y:    15,
		},
		{
			name: "mouse middle with all modifiers",
			btn:  EncodeMouseButton(MouseMiddle, true, true, true, true),
			x:    20,
			y:    20,
		},
		{
			name: "mouse wheel left at max coordinates",
			btn:  EncodeMouseButton(MouseWheelLeft, false, false, false, false),
			x:    223,
			y:    223,
		},
		{
			name:    "mouse forward release",
			btn:     EncodeMouseButton(MouseForward, false, false, false, false),
			x:       100,
			y:       100,
			release: true,
		},
		{
			name: "mouse backward with shift and ctrl",
			btn:  EncodeMouseButton(MouseBackward, false, true, false, true),
			x:    50,
			y:    50,
		},
	}

	for i, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			m := MouseSgr(tc.btn, tc.x, tc.y, tc.release)
			action := 'M'
			if tc.release {
				action = 'm'
			}
			want := fmt.Sprintf("\x1b[<%d;%d;%d%c", tc.btn, tc.x+1, tc.y+1, action)
			if m != want {
				t.Errorf("test %d: got %q; want %q", i+1, m, want)
			}
		})
	}
}