| 1 |
import sys |
| 2 |
import unittest |
| 3 |
|
| 4 |
if sys.platform != "win32": |
| 5 |
raise unittest.SkipTest("test only relevant on win32") |
| 6 |
|
| 7 |
|
| 8 |
import itertools |
| 9 |
from functools import partial |
| 10 |
from typing import Iterable |
| 11 |
from unittest import TestCase |
| 12 |
from unittest.mock import MagicMock, call |
| 13 |
|
| 14 |
from .support import handle_all_events, code_to_events |
| 15 |
|
| 16 |
try: |
| 17 |
from pyrepl.console import Event, Console |
| 18 |
from pyrepl.windows_console import ( |
| 19 |
WindowsConsole, |
| 20 |
MOVE_LEFT, |
| 21 |
MOVE_RIGHT, |
| 22 |
MOVE_UP, |
| 23 |
MOVE_DOWN, |
| 24 |
ERASE_IN_LINE, |
| 25 |
) |
| 26 |
except ImportError: |
| 27 |
pass |
| 28 |
|
| 29 |
|
| 30 |
class WindowsConsoleTests(TestCase): |
| 31 |
def console(self, events, **kwargs) -> Console: |
| 32 |
console = WindowsConsole() |
| 33 |
console.get_event = MagicMock(side_effect=events) |
| 34 |
console._scroll = MagicMock() |
| 35 |
console._hide_cursor = MagicMock() |
| 36 |
console._show_cursor = MagicMock() |
| 37 |
console._getscrollbacksize = MagicMock(42) |
| 38 |
console.out = MagicMock() |
| 39 |
|
| 40 |
height = kwargs.get("height", 25) |
| 41 |
width = kwargs.get("width", 80) |
| 42 |
console.getheightwidth = MagicMock(side_effect=lambda: (height, width)) |
| 43 |
|
| 44 |
console.prepare() |
| 45 |
for key, val in kwargs.items(): |
| 46 |
setattr(console, key, val) |
| 47 |
return console |
| 48 |
|
| 49 |
def handle_events(self, events: Iterable[Event], **kwargs): |
| 50 |
return handle_all_events(events, partial(self.console, **kwargs)) |
| 51 |
|
| 52 |
def handle_events_narrow(self, events): |
| 53 |
return self.handle_events(events, width=5) |
| 54 |
|
| 55 |
def handle_events_short(self, events): |
| 56 |
return self.handle_events(events, height=1) |
| 57 |
|
| 58 |
def handle_events_height_3(self, events): |
| 59 |
return self.handle_events(events, height=3) |
| 60 |
|
| 61 |
def test_simple_addition(self): |
| 62 |
code = "12+34" |
| 63 |
events = code_to_events(code) |
| 64 |
_, con = self.handle_events(events) |
| 65 |
con.out.write.assert_any_call(b"1") |
| 66 |
con.out.write.assert_any_call(b"2") |
| 67 |
con.out.write.assert_any_call(b"+") |
| 68 |
con.out.write.assert_any_call(b"3") |
| 69 |
con.out.write.assert_any_call(b"4") |
| 70 |
con.restore() |
| 71 |
|
| 72 |
def test_wrap(self): |
| 73 |
code = "12+34" |
| 74 |
events = code_to_events(code) |
| 75 |
_, con = self.handle_events_narrow(events) |
| 76 |
con.out.write.assert_any_call(b"1") |
| 77 |
con.out.write.assert_any_call(b"2") |
| 78 |
con.out.write.assert_any_call(b"+") |
| 79 |
con.out.write.assert_any_call(b"3") |
| 80 |
con.out.write.assert_any_call(b"\\") |
| 81 |
con.out.write.assert_any_call(b"\n") |
| 82 |
con.out.write.assert_any_call(b"4") |
| 83 |
con.restore() |
| 84 |
|
| 85 |
def test_resize_wider(self): |
| 86 |
code = "1234567890" |
| 87 |
events = code_to_events(code) |
| 88 |
reader, console = self.handle_events_narrow(events) |
| 89 |
|
| 90 |
console.height = 20 |
| 91 |
console.width = 80 |
| 92 |
console.getheightwidth = MagicMock(lambda _: (20, 80)) |
| 93 |
|
| 94 |
def same_reader(_): |
| 95 |
return reader |
| 96 |
|
| 97 |
def same_console(events): |
| 98 |
console.get_event = MagicMock(side_effect=events) |
| 99 |
return console |
| 100 |
|
| 101 |
_, con = handle_all_events( |
| 102 |
[Event(evt="resize", data=None)], |
| 103 |
prepare_reader=same_reader, |
| 104 |
prepare_console=same_console, |
| 105 |
) |
| 106 |
|
| 107 |
con.out.write.assert_any_call(self.move_right(2)) |
| 108 |
con.out.write.assert_any_call(self.move_up(2)) |
| 109 |
con.out.write.assert_any_call(b"567890") |
| 110 |
|
| 111 |
con.restore() |
| 112 |
|
| 113 |
def test_resize_narrower(self): |
| 114 |
code = "1234567890" |
| 115 |
events = code_to_events(code) |
| 116 |
reader, console = self.handle_events(events) |
| 117 |
|
| 118 |
console.height = 20 |
| 119 |
console.width = 4 |
| 120 |
console.getheightwidth = MagicMock(lambda _: (20, 4)) |
| 121 |
|
| 122 |
def same_reader(_): |
| 123 |
return reader |
| 124 |
|
| 125 |
def same_console(events): |
| 126 |
console.get_event = MagicMock(side_effect=events) |
| 127 |
return console |
| 128 |
|
| 129 |
_, con = handle_all_events( |
| 130 |
[Event(evt="resize", data=None)], |
| 131 |
prepare_reader=same_reader, |
| 132 |
prepare_console=same_console, |
| 133 |
) |
| 134 |
|
| 135 |
con.out.write.assert_any_call(b"456\\") |
| 136 |
con.out.write.assert_any_call(b"789\\") |
| 137 |
|
| 138 |
con.restore() |
| 139 |
|
| 140 |
def test_cursor_left(self): |
| 141 |
code = "1" |
| 142 |
events = itertools.chain( |
| 143 |
code_to_events(code), |
| 144 |
[Event(evt="key", data="left", raw=bytearray(b"\x1bOD"))], |
| 145 |
) |
| 146 |
_, con = self.handle_events(events) |
| 147 |
con.out.write.assert_any_call(self.move_left()) |
| 148 |
con.restore() |
| 149 |
|
| 150 |
def test_cursor_left_right(self): |
| 151 |
code = "1" |
| 152 |
events = itertools.chain( |
| 153 |
code_to_events(code), |
| 154 |
[ |
| 155 |
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")), |
| 156 |
Event(evt="key", data="right", raw=bytearray(b"\x1bOC")), |
| 157 |
], |
| 158 |
) |
| 159 |
_, con = self.handle_events(events) |
| 160 |
con.out.write.assert_any_call(self.move_left()) |
| 161 |
con.out.write.assert_any_call(self.move_right()) |
| 162 |
con.restore() |
| 163 |
|
| 164 |
def test_cursor_up(self): |
| 165 |
code = "1\n2+3" |
| 166 |
events = itertools.chain( |
| 167 |
code_to_events(code), |
| 168 |
[Event(evt="key", data="up", raw=bytearray(b"\x1bOA"))], |
| 169 |
) |
| 170 |
_, con = self.handle_events(events) |
| 171 |
con.out.write.assert_any_call(self.move_up()) |
| 172 |
con.restore() |
| 173 |
|
| 174 |
def test_cursor_up_down(self): |
| 175 |
code = "1\n2+3" |
| 176 |
events = itertools.chain( |
| 177 |
code_to_events(code), |
| 178 |
[ |
| 179 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 180 |
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), |
| 181 |
], |
| 182 |
) |
| 183 |
_, con = self.handle_events(events) |
| 184 |
con.out.write.assert_any_call(self.move_up()) |
| 185 |
con.out.write.assert_any_call(self.move_down()) |
| 186 |
con.restore() |
| 187 |
|
| 188 |
def test_cursor_back_write(self): |
| 189 |
events = itertools.chain( |
| 190 |
code_to_events("1"), |
| 191 |
[Event(evt="key", data="left", raw=bytearray(b"\x1bOD"))], |
| 192 |
code_to_events("2"), |
| 193 |
) |
| 194 |
_, con = self.handle_events(events) |
| 195 |
con.out.write.assert_any_call(b"1") |
| 196 |
con.out.write.assert_any_call(self.move_left()) |
| 197 |
con.out.write.assert_any_call(b"21") |
| 198 |
con.restore() |
| 199 |
|
| 200 |
def test_multiline_function_move_up_short_terminal(self): |
| 201 |
|
| 202 |
code = ( |
| 203 |
"def f():\n" |
| 204 |
" foo" |
| 205 |
) |
| 206 |
|
| 207 |
|
| 208 |
events = itertools.chain( |
| 209 |
code_to_events(code), |
| 210 |
[ |
| 211 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 212 |
Event(evt="scroll", data=None), |
| 213 |
], |
| 214 |
) |
| 215 |
_, con = self.handle_events_short(events) |
| 216 |
con.out.write.assert_any_call(self.move_left(5)) |
| 217 |
con.out.write.assert_any_call(self.move_up()) |
| 218 |
con.restore() |
| 219 |
|
| 220 |
def test_multiline_function_move_up_down_short_terminal(self): |
| 221 |
|
| 222 |
code = ( |
| 223 |
"def f():\n" |
| 224 |
" foo" |
| 225 |
) |
| 226 |
|
| 227 |
|
| 228 |
events = itertools.chain( |
| 229 |
code_to_events(code), |
| 230 |
[ |
| 231 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 232 |
Event(evt="scroll", data=None), |
| 233 |
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), |
| 234 |
Event(evt="scroll", data=None), |
| 235 |
], |
| 236 |
) |
| 237 |
_, con = self.handle_events_short(events) |
| 238 |
con.out.write.assert_any_call(self.move_left(8)) |
| 239 |
con.out.write.assert_any_call(self.erase_in_line()) |
| 240 |
con.restore() |
| 241 |
|
| 242 |
def test_resize_bigger_on_multiline_function(self): |
| 243 |
|
| 244 |
code = ( |
| 245 |
"def f():\n" |
| 246 |
" foo" |
| 247 |
) |
| 248 |
|
| 249 |
|
| 250 |
events = itertools.chain(code_to_events(code)) |
| 251 |
reader, console = self.handle_events_short(events) |
| 252 |
|
| 253 |
console.height = 2 |
| 254 |
console.getheightwidth = MagicMock(lambda _: (2, 80)) |
| 255 |
|
| 256 |
def same_reader(_): |
| 257 |
return reader |
| 258 |
|
| 259 |
def same_console(events): |
| 260 |
console.get_event = MagicMock(side_effect=events) |
| 261 |
return console |
| 262 |
|
| 263 |
_, con = handle_all_events( |
| 264 |
[Event(evt="resize", data=None)], |
| 265 |
prepare_reader=same_reader, |
| 266 |
prepare_console=same_console, |
| 267 |
) |
| 268 |
con.out.write.assert_has_calls( |
| 269 |
[ |
| 270 |
call(self.move_left(5)), |
| 271 |
call(self.move_up()), |
| 272 |
call(b"def f():"), |
| 273 |
call(self.move_left(3)), |
| 274 |
call(self.move_down()), |
| 275 |
] |
| 276 |
) |
| 277 |
console.restore() |
| 278 |
con.restore() |
| 279 |
|
| 280 |
def test_resize_smaller_on_multiline_function(self): |
| 281 |
|
| 282 |
code = ( |
| 283 |
"def f():\n" |
| 284 |
" foo" |
| 285 |
) |
| 286 |
|
| 287 |
|
| 288 |
events = itertools.chain(code_to_events(code)) |
| 289 |
reader, console = self.handle_events_height_3(events) |
| 290 |
|
| 291 |
console.height = 1 |
| 292 |
console.getheightwidth = MagicMock(lambda _: (1, 80)) |
| 293 |
|
| 294 |
def same_reader(_): |
| 295 |
return reader |
| 296 |
|
| 297 |
def same_console(events): |
| 298 |
console.get_event = MagicMock(side_effect=events) |
| 299 |
return console |
| 300 |
|
| 301 |
_, con = handle_all_events( |
| 302 |
[Event(evt="resize", data=None)], |
| 303 |
prepare_reader=same_reader, |
| 304 |
prepare_console=same_console, |
| 305 |
) |
| 306 |
con.out.write.assert_has_calls( |
| 307 |
[ |
| 308 |
call(self.move_left(5)), |
| 309 |
call(self.move_up()), |
| 310 |
call(self.erase_in_line()), |
| 311 |
call(b" foo"), |
| 312 |
] |
| 313 |
) |
| 314 |
console.restore() |
| 315 |
con.restore() |
| 316 |
|
| 317 |
def move_up(self, lines=1): |
| 318 |
return MOVE_UP.format(lines).encode("utf8") |
| 319 |
|
| 320 |
def move_down(self, lines=1): |
| 321 |
return MOVE_DOWN.format(lines).encode("utf8") |
| 322 |
|
| 323 |
def move_left(self, cols=1): |
| 324 |
return MOVE_LEFT.format(cols).encode("utf8") |
| 325 |
|
| 326 |
def move_right(self, cols=1): |
| 327 |
return MOVE_RIGHT.format(cols).encode("utf8") |
| 328 |
|
| 329 |
def erase_in_line(self): |
| 330 |
return ERASE_IN_LINE.encode("utf8") |
| 331 |
|
| 332 |
|
| 333 |
if __name__ == "__main__": |
| 334 |
unittest.main() |