Skip to content

Commit

Permalink
tests: add annotations to TestConsole/read_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
bretello committed Apr 29, 2024
1 parent 6feae56 commit e6688e2
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions tests/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from __future__ import annotations


from typing import Tuple
from typing import Optional, Union, Tuple, List

from pyrepl.console import Console, Event
from pyrepl.reader import Reader
Expand All @@ -32,11 +32,23 @@ def __eq__(self, other):
EA = EqualsAnything()


Command = Union[
Tuple[str, Optional[str]],
Union[Tuple[str], str],
]
ExpectedScreen = Optional[List[str]]
TestSpec = List[Tuple[Command, ExpectedScreen]]


class TestConsole(Console):
def __init__(self, events, verbose=False):
def __init__(
self,
events: List[Tuple[Command, ExpectedScreen]],
verbose: bool = False,
):
super().__init__(width=80, height=24, encoding="utf-8")
self.events = events
self.next_screen = None
self.next_screen: Optional[List[str]] = None
self.verbose = verbose

def refresh(self, screen, xy: Tuple[int, int]):
Expand All @@ -45,34 +57,37 @@ def refresh(self, screen, xy: Tuple[int, int]):
screen == self.next_screen
), f"[ {screen} != {self.next_screen} after {self.last_event_name} ]"

def get_event(self, block=True):
ev, sc = self.events.pop(0)
self.next_screen = sc
if not isinstance(ev, tuple):
ev = (ev, None)
self.last_event_name = ev[0]
def get_event(self, block: bool = True) -> Event:
event: Command
screen: ExpectedScreen
event, screen = self.events.pop(0)
self.next_screen = screen
if not isinstance(event, tuple):
event = (event, None)
self.last_event_name = event[0]
if self.verbose:
print("event", ev)
return Event(*ev)
print(f"{event=}")
return Event(*event)

def getpending(self):
def getpending(self) -> Event:
"""Nothing pending, but do not return None here."""
return Event("key", "", b"")
return Event("key", "", "")


class TestReader(Reader):
__test__ = False

def get_prompt(self, lineno, cursor_on_line):
def get_prompt(self, lineno: int, cursor_on_line) -> str:
return ""

def refresh(self):
Reader.refresh(self)
self.dirty = True


def read_spec(test_spec, reader_class=TestReader):
def read_spec(test_spec: TestSpec, reader_class=TestReader):
# remember to finish your test_spec with 'accept' or similar!
con = TestConsole(test_spec, verbose=True)

reader = reader_class(con)
reader.readline()

0 comments on commit e6688e2

Please sign in to comment.