Skip to content
Myr

Simplify code and add tests for `mouse.go` (#30)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Aleksandr Krivoshchekov · committed by GitHub · parent 3aa0024 · Browse files · 2 files+430 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+430 added−25 removed

  • other:+430 added−25 removed

2 files+430 added−25 removed

added:mouse_test.go+387 added
1package tea23import "testing"45func TestMouseEvent_String(t *testing.T) {6 tt := []struct {7 name string8 event MouseEvent9 expected string10 }{11 {12 name: "unknown",13 event: MouseEvent{Type: MouseUnknown},14 expected: "unknown",15 },16 {17 name: "left",18 event: MouseEvent{Type: MouseLeft},19 expected: "left",20 },21 {22 name: "right",23 event: MouseEvent{Type: MouseRight},24 expected: "right",25 },26 {27 name: "middle",28 event: MouseEvent{Type: MouseMiddle},29 expected: "middle",30 },31 {32 name: "release",33 event: MouseEvent{Type: MouseRelease},34 expected: "release",35 },36 {37 name: "wheel up",38 event: MouseEvent{Type: MouseWheelUp},39 expected: "wheel up",40 },41 {42 name: "wheel down",43 event: MouseEvent{Type: MouseWheelDown},44 expected: "wheel down",45 },46 {47 name: "motion",48 event: MouseEvent{Type: MouseMotion},49 expected: "motion",50 },51 {52 name: "alt+left",53 event: MouseEvent{54 Type: MouseLeft,55 Alt: true,56 },57 expected: "alt+left",58 },59 {60 name: "ctrl+left",61 event: MouseEvent{62 Type: MouseLeft,63 Ctrl: true,64 },65 expected: "ctrl+left",66 },67 {68 name: "ctrl+alt+left",69 event: MouseEvent{70 Type: MouseLeft,71 Alt: true,72 Ctrl: true,73 },74 expected: "ctrl+alt+left",75 },76 {77 name: "ignore coordinates",78 event: MouseEvent{79 X: 100,80 Y: 200,81 Type: MouseLeft,82 },83 expected: "left",84 },85 {86 name: "broken type",87 event: MouseEvent{88 Type: MouseEventType(-1000),89 },90 expected: "",91 },92 }9394 for i := range tt {95 tc := tt[i]9697 t.Run(tc.name, func(t *testing.T) {98 actual := tc.event.String()99100 if tc.expected != actual {101 t.Fatalf("expected %q but got %q",102 tc.expected,103 actual,104 )105 }106 })107 }108}109110func TestParseX10MouseEvent(t *testing.T) {111 encode := func(b byte, x, y int) []byte {112 return []byte{113 '\x1b',114 '[',115 'M',116 byte(32) + b,117 byte(x + 32 + 1),118 byte(y + 32 + 1),119 }120 }121122 tt := []struct {123 name string124 buf []byte125 expected MouseEvent126 }{127 // Position.128 {129 name: "zero position",130 buf: encode(0b0010_0000, 0, 0),131 expected: MouseEvent{132 X: 0,133 Y: 0,134 Type: MouseLeft,135 },136 },137 {138 name: "max position",139 buf: encode(0b0010_0000, 222, 222), // Because 255 (max int8) - 32 - 1.140 expected: MouseEvent{141 X: 222,142 Y: 222,143 Type: MouseLeft,144 },145 },146 // Simple.147 {148 name: "left",149 buf: encode(0b0000_0000, 32, 16),150 expected: MouseEvent{151 X: 32,152 Y: 16,153 Type: MouseLeft,154 },155 },156 {157 name: "left in motion",158 buf: encode(0b0010_0000, 32, 16),159 expected: MouseEvent{160 X: 32,161 Y: 16,162 Type: MouseLeft,163 },164 },165 {166 name: "middle",167 buf: encode(0b0000_0001, 32, 16),168 expected: MouseEvent{169 X: 32,170 Y: 16,171 Type: MouseMiddle,172 },173 },174 {175 name: "middle in motion",176 buf: encode(0b0010_0001, 32, 16),177 expected: MouseEvent{178 X: 32,179 Y: 16,180 Type: MouseMiddle,181 },182 },183 {184 name: "right",185 buf: encode(0b0000_0010, 32, 16),186 expected: MouseEvent{187 X: 32,188 Y: 16,189 Type: MouseRight,190 },191 },192 {193 name: "right in motion",194 buf: encode(0b0010_0010, 32, 16),195 expected: MouseEvent{196 X: 32,197 Y: 16,198 Type: MouseRight,199 },200 },201 {202 name: "motion",203 buf: encode(0b0010_0011, 32, 16),204 expected: MouseEvent{205 X: 32,206 Y: 16,207 Type: MouseMotion,208 },209 },210 {211 name: "wheel up",212 buf: encode(0b0100_0000, 32, 16),213 expected: MouseEvent{214 X: 32,215 Y: 16,216 Type: MouseWheelUp,217 },218 },219 {220 name: "wheel down",221 buf: encode(0b0100_0001, 32, 16),222 expected: MouseEvent{223 X: 32,224 Y: 16,225 Type: MouseWheelDown,226 },227 },228 {229 name: "release",230 buf: encode(0b0000_0011, 32, 16),231 expected: MouseEvent{232 X: 32,233 Y: 16,234 Type: MouseRelease,235 },236 },237 // Combinations.238 {239 name: "alt+right",240 buf: encode(0b0010_1010, 32, 16),241 expected: MouseEvent{242 X: 32,243 Y: 16,244 Type: MouseRight,245 Alt: true,246 },247 },248 {249 name: "ctrl+right",250 buf: encode(0b0011_0010, 32, 16),251 expected: MouseEvent{252 X: 32,253 Y: 16,254 Type: MouseRight,255 Ctrl: true,256 },257 },258 {259 name: "ctrl+alt+right",260 buf: encode(0b0011_1010, 32, 16),261 expected: MouseEvent{262 X: 32,263 Y: 16,264 Type: MouseRight,265 Alt: true,266 Ctrl: true,267 },268 },269 {270 name: "alt+wheel down",271 buf: encode(0b0100_1001, 32, 16),272 expected: MouseEvent{273 X: 32,274 Y: 16,275 Type: MouseWheelDown,276 Alt: true,277 },278 },279 {280 name: "ctrl+wheel down",281 buf: encode(0b0101_0001, 32, 16),282 expected: MouseEvent{283 X: 32,284 Y: 16,285 Type: MouseWheelDown,286 Ctrl: true,287 },288 },289 {290 name: "ctrl+alt+wheel down",291 buf: encode(0b0101_1001, 32, 16),292 expected: MouseEvent{293 X: 32,294 Y: 16,295 Type: MouseWheelDown,296 Alt: true,297 Ctrl: true,298 },299 },300 // Unknown.301 {302 name: "wheel with unknown bit",303 buf: encode(0b0100_0010, 32, 16),304 expected: MouseEvent{305 X: 32,306 Y: 16,307 Type: MouseUnknown,308 },309 },310 {311 name: "unknown with modifier",312 buf: encode(0b0100_1010, 32, 16),313 expected: MouseEvent{314 X: 32,315 Y: 16,316 Type: MouseUnknown,317 Alt: true,318 },319 },320 // Overflow position.321 {322 name: "overflow position",323 buf: encode(0b0010_0000, 250, 223), // Because 255 (max int8) - 32 - 1.324 expected: MouseEvent{325 X: -6,326 Y: -33,327 Type: MouseLeft,328 },329 },330 }331332 for i := range tt {333 tc := tt[i]334335 t.Run(tc.name, func(t *testing.T) {336 actual, err := parseX10MouseEvent(tc.buf)337 if err != nil {338 t.Fatalf("unexpected error: %v",339 err,340 )341 }342343 if tc.expected != actual {344 t.Fatalf("expected %#v but got %#v",345 tc.expected,346 actual,347 )348 }349 })350 }351}352353func TestParseX10MouseEvent_error(t *testing.T) {354 tt := []struct {355 name string356 buf []byte357 }{358 {359 name: "empty buf",360 buf: nil,361 },362 {363 name: "wrong high bit",364 buf: []byte("\x1a[M@A1"),365 },366 {367 name: "short buf",368 buf: []byte("\x1b[M@A"),369 },370 {371 name: "long buf",372 buf: []byte("\x1b[M@A11"),373 },374 }375376 for i := range tt {377 tc := tt[i]378379 t.Run(tc.name, func(t *testing.T) {380 _, err := parseX10MouseEvent(tc.buf)381382 if err == nil {383 t.Fatalf("expected error but got nil")384 }385 })386 }387}
modified:mouse.go+43 added-25 removed
56 unmodified lines
57// X10 mouse events look like:58//59// ESC [M Cb Cx Cy60//61// See: http://www.xfree86.org/current/ctlseqs.html#Mouse%20Tracking62func parseX10MouseEvent(buf []byte) (m MouseEvent, err error) {63 if len(buf) != 6 || string(buf[:3]) != "\x1b[M" {64 return m, errors.New("not an X10 mouse event")65 }6667 e := buf[3] - 326868 switch e {69 case 35:70 m.Type = MouseMotion71 case 64:72 m.Type = MouseWheelUp73 case 65:74 m.Type = MouseWheelDown75 default:76 switch e & 3 {77 case 0:78 if e&64 != 0 {79 m.Type = MouseWheelUp69 const (70 bitShift = 0b0000_010071 bitAlt = 0b0000_100072 bitCtrl = 0b0001_000073 bitMotion = 0b0010_000074 bitWheel = 0b0100_00007576 bitsMask = 0b0000_00117778 bitsLeft = 0b0000_000079 bitsMiddle = 0b0000_000180 bitsRight = 0b0000_001081 bitsRelease = 0b0000_00118283 bitsWheelUp = 0b0000_000084 bitsWheelDown = 0b0000_000185 )8687 if e&bitWheel != 0 {88 // Check the low two bits.89 switch e & bitsMask {90 case bitsWheelUp:91 m.Type = MouseWheelUp92 case bitsWheelDown:93 m.Type = MouseWheelDown94 }95 } else {96 // Check the low two bits.97 // We do not separate clicking and dragging.98 switch e & bitsMask {99 case bitsLeft:100 m.Type = MouseLeft101 case bitsMiddle:102 m.Type = MouseMiddle103 case bitsRight:104 m.Type = MouseRight105 case bitsRelease:106 if e&bitMotion != 0 {107 m.Type = MouseMotion108 } else {81 m.Type = MouseLeft109 m.Type = MouseRelease110 }83 case 1:84 if e&64 != 0 {85 m.Type = MouseWheelDown86 } else {87 m.Type = MouseMiddle88 }89 case 2:90 m.Type = MouseRight91 case 3:92 m.Type = MouseRelease111 }112 }11396 if e&8 != 0 {114 if e&bitAlt != 0 {115 m.Alt = true116 }99 if e&16 != 0 {117 if e&bitCtrl != 0 {118 m.Ctrl = true119 }120121 // (1,1) is the upper left. We subtract 1 to normalize it to (0,0).
5 unmodified lines