| 1 |
package ansi |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestMouseButton(t *testing.T) { |
| 9 |
type test struct { |
| 10 |
name string |
| 11 |
btn MouseButton |
| 12 |
motion, shift, alt, ctrl bool |
| 13 |
want byte |
| 14 |
} |
| 15 |
|
| 16 |
cases := []test{ |
| 17 |
{ |
| 18 |
name: "mouse release", |
| 19 |
btn: MouseNone, |
| 20 |
want: 0b0000_0011, |
| 21 |
}, |
| 22 |
{ |
| 23 |
name: "mouse release with ctrl", |
| 24 |
btn: MouseNone, |
| 25 |
ctrl: true, |
| 26 |
want: 0b0001_0011, |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: "mouse left", |
| 30 |
btn: MouseLeft, |
| 31 |
want: 0b0000_0000, |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "mouse right", |
| 35 |
btn: MouseRight, |
| 36 |
want: 0b0000_0010, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "mouse wheel up", |
| 40 |
btn: MouseWheelUp, |
| 41 |
want: 0b0100_0000, |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "mouse wheel right", |
| 45 |
btn: MouseWheelRight, |
| 46 |
want: 0b0100_0011, |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "mouse backward", |
| 50 |
btn: MouseBackward, |
| 51 |
want: 0b1000_0000, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "mouse forward", |
| 55 |
btn: MouseForward, |
| 56 |
want: 0b1000_0001, |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "mouse button 10", |
| 60 |
btn: MouseButton10, |
| 61 |
want: 0b1000_0010, |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: "mouse button 11", |
| 65 |
btn: MouseButton11, |
| 66 |
want: 0b1000_0011, |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "mouse middle with motion", |
| 70 |
btn: MouseMiddle, |
| 71 |
motion: true, |
| 72 |
want: 0b0010_0001, |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "mouse middle with shift", |
| 76 |
btn: MouseMiddle, |
| 77 |
shift: true, |
| 78 |
want: 0b0000_0101, |
| 79 |
}, |
| 80 |
{ |
| 81 |
name: "mouse middle with motion and alt", |
| 82 |
btn: MouseMiddle, |
| 83 |
motion: true, |
| 84 |
alt: true, |
| 85 |
want: 0b0010_1001, |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "mouse right with shift, alt, and ctrl", |
| 89 |
btn: MouseRight, |
| 90 |
shift: true, |
| 91 |
alt: true, |
| 92 |
ctrl: true, |
| 93 |
want: 0b0001_1110, |
| 94 |
}, |
| 95 |
{ |
| 96 |
name: "mouse button 10 with motion, shift, alt, and ctrl", |
| 97 |
btn: MouseButton10, |
| 98 |
motion: true, |
| 99 |
shift: true, |
| 100 |
alt: true, |
| 101 |
ctrl: true, |
| 102 |
want: 0b1011_1110, |
| 103 |
}, |
| 104 |
{ |
| 105 |
name: "mouse left with motion, shift, and ctrl", |
| 106 |
btn: MouseLeft, |
| 107 |
motion: true, |
| 108 |
shift: true, |
| 109 |
ctrl: true, |
| 110 |
want: 0b0011_0100, |
| 111 |
}, |
| 112 |
{ |
| 113 |
name: "invalid mouse button", |
| 114 |
btn: MouseButton(0xff), |
| 115 |
want: 0b1111_1111, |
| 116 |
}, |
| 117 |
{ |
| 118 |
name: "mouse wheel down with motion", |
| 119 |
btn: MouseWheelDown, |
| 120 |
motion: true, |
| 121 |
want: 0b0110_0001, |
| 122 |
}, |
| 123 |
{ |
| 124 |
name: "mouse wheel down with shift and ctrl", |
| 125 |
btn: MouseWheelDown, |
| 126 |
shift: true, |
| 127 |
ctrl: true, |
| 128 |
want: 0b0101_0101, |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "mouse wheel left with alt", |
| 132 |
btn: MouseWheelLeft, |
| 133 |
alt: true, |
| 134 |
want: 0b0100_1010, |
| 135 |
}, |
| 136 |
{ |
| 137 |
name: "mouse middle with all modifiers", |
| 138 |
btn: MouseMiddle, |
| 139 |
motion: true, |
| 140 |
shift: true, |
| 141 |
alt: true, |
| 142 |
ctrl: true, |
| 143 |
want: 0b0011_1101, |
| 144 |
}, |
| 145 |
} |
| 146 |
|
| 147 |
for i, tc := range cases { |
| 148 |
t.Run(tc.name, func(t *testing.T) { |
| 149 |
got := EncodeMouseButton(tc.btn, tc.motion, tc.shift, tc.alt, tc.ctrl) |
| 150 |
if got != tc.want { |
| 151 |
t.Errorf("test %d: got %08b; want %08b", i+1, got, tc.want) |
| 152 |
} |
| 153 |
}) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func TestMouseSgr(t *testing.T) { |
| 158 |
type test struct { |
| 159 |
name string |
| 160 |
btn byte |
| 161 |
x, y int |
| 162 |
release bool |
| 163 |
} |
| 164 |
|
| 165 |
cases := []test{ |
| 166 |
{ |
| 167 |
name: "mouse left", |
| 168 |
btn: EncodeMouseButton(MouseLeft, false, false, false, false), |
| 169 |
x: 0, |
| 170 |
y: 0, |
| 171 |
}, |
| 172 |
{ |
| 173 |
name: "wheel down", |
| 174 |
btn: EncodeMouseButton(MouseWheelDown, false, false, false, false), |
| 175 |
x: 1, |
| 176 |
y: 10, |
| 177 |
}, |
| 178 |
{ |
| 179 |
name: "mouse right with shift, alt, and ctrl", |
| 180 |
btn: EncodeMouseButton(MouseRight, false, true, true, true), |
| 181 |
x: 10, |
| 182 |
y: 1, |
| 183 |
}, |
| 184 |
{ |
| 185 |
name: "mouse release", |
| 186 |
btn: EncodeMouseButton(MouseNone, false, false, false, false), |
| 187 |
x: 5, |
| 188 |
y: 5, |
| 189 |
release: true, |
| 190 |
}, |
| 191 |
{ |
| 192 |
name: "mouse button 10 with motion, shift, alt, and ctrl", |
| 193 |
btn: EncodeMouseButton(MouseButton10, true, true, true, true), |
| 194 |
x: 10, |
| 195 |
y: 10, |
| 196 |
}, |
| 197 |
{ |
| 198 |
name: "mouse wheel up with motion", |
| 199 |
btn: EncodeMouseButton(MouseWheelUp, true, false, false, false), |
| 200 |
x: 15, |
| 201 |
y: 15, |
| 202 |
}, |
| 203 |
{ |
| 204 |
name: "mouse middle with all modifiers", |
| 205 |
btn: EncodeMouseButton(MouseMiddle, true, true, true, true), |
| 206 |
x: 20, |
| 207 |
y: 20, |
| 208 |
}, |
| 209 |
{ |
| 210 |
name: "mouse wheel left at max coordinates", |
| 211 |
btn: EncodeMouseButton(MouseWheelLeft, false, false, false, false), |
| 212 |
x: 223, |
| 213 |
y: 223, |
| 214 |
}, |
| 215 |
{ |
| 216 |
name: "mouse forward release", |
| 217 |
btn: EncodeMouseButton(MouseForward, false, false, false, false), |
| 218 |
x: 100, |
| 219 |
y: 100, |
| 220 |
release: true, |
| 221 |
}, |
| 222 |
{ |
| 223 |
name: "mouse backward with shift and ctrl", |
| 224 |
btn: EncodeMouseButton(MouseBackward, false, true, false, true), |
| 225 |
x: 50, |
| 226 |
y: 50, |
| 227 |
}, |
| 228 |
} |
| 229 |
|
| 230 |
for i, tc := range cases { |
| 231 |
t.Run(tc.name, func(t *testing.T) { |
| 232 |
m := MouseSgr(tc.btn, tc.x, tc.y, tc.release) |
| 233 |
action := 'M' |
| 234 |
if tc.release { |
| 235 |
action = 'm' |
| 236 |
} |
| 237 |
want := fmt.Sprintf("\x1b[<%d;%d;%d%c", tc.btn, tc.x+1, tc.y+1, action) |
| 238 |
if m != want { |
| 239 |
t.Errorf("test %d: got %q; want %q", i+1, m, want) |
| 240 |
} |
| 241 |
}) |
| 242 |
} |
| 243 |
} |