home · contact · privacy
More explicitly name testing code syntax tokens.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 28 Sep 2025 18:32:37 +0000 (20:32 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 28 Sep 2025 18:32:37 +0000 (20:32 +0200)
src/ircplom/client_tui.py
src/ircplom/testing.py

index efd7458b9567cbf954b5e06f5e27cee9eb46fd1b..f92215c00a2d1b23dabd6dee608f22392f5581fd 100644 (file)
@@ -23,7 +23,7 @@ CMD_SHORTCUTS['raw'] = 'window.raw'
 
 _LOG_PREFIX_SERVER = '$'
 _LOG_PREFIX_OUT = '>'
-_LOG_PREFIX_IN = '<'
+LOG_PREFIX_IN = '<'
 
 _PATH_LOGS = Path.home().joinpath('.local', 'share', 'ircplom', 'logs')
 _PATH_CONFIG = Path.home().joinpath('.config', 'ircplom', 'ircplom.toml')
@@ -403,7 +403,7 @@ class _ClientWindowsManager:
         'From parsing scope, kwargs, build prefix before sending to logger.'
         prefix = '$'
         if out is not None:
-            prefix = _LOG_PREFIX_OUT if out else _LOG_PREFIX_IN
+            prefix = _LOG_PREFIX_OUT if out else LOG_PREFIX_IN
         kwargs = {'alert': True} if alert else {}
         kwargs |= {'target': target} if target else {}
         self._tui_log(msg, scope=scope, prefix=prefix, **kwargs)
index 39ac913c1fedacca586b3ea1f4d5dc1c5a137486..a1b6d99d2f6e825b382ac4751e5e2fa931ff87c7 100644 (file)
@@ -5,7 +5,7 @@ from pathlib import Path
 from typing import Callable, Generator, Iterator, Optional
 from ircplom.events import Event, Loop, QueueMixin
 from ircplom.client import IrcConnection, IrcConnSetup
-from ircplom.client_tui import ClientKnowingTui, ClientTui
+from ircplom.client_tui import ClientKnowingTui, ClientTui, LOG_PREFIX_IN
 from ircplom.irc_conn import IrcConnAbortException, IrcMessage
 from ircplom.tui_base import TerminalInterface, TuiEvent
 
@@ -90,6 +90,16 @@ class _TestClientKnowingTui(ClientKnowingTui):
     _cls_conn = _FakeIrcConnection
 
 
+_CHAR_ANCHOR = '|'
+_CHAR_COMMENT = '#'
+_CHAR_CONTEXT_SEP = ' '
+_CHAR_ID_TYPE_SEP = ':'
+_CHAR_PROMPT = '>'
+_CHAR_RANGE = ':'
+_CHAR_RANGE_DATA_SEP = ' '
+_CHAR_WIN_ID_SEP = ','
+
+
 class _Playbook:
     put_keypress: Optional[Callable] = None
 
@@ -101,13 +111,13 @@ class _Playbook:
             inserts: list[str] = []
             anchors: dict[str, int] = {}
             for idx, line in enumerate(self._lines):
-                if line[:1] == '|':
+                if line[:1] == _CHAR_ANCHOR:
                     anchors[line[1:]] = idx
             for idx, line in enumerate(self._lines):
                 split = self._split_active_line(line)
                 if (not split) or split[0] != 'repeat':
                     continue
-                range_data = split[1].split(' ', maxsplit=2)
+                range_data = split[1].split(_CHAR_RANGE_DATA_SEP, maxsplit=2)
                 start_key, end_key = range_data[:2]
                 start = anchors[start_key] + 1
                 end = anchors[end_key]
@@ -134,10 +144,11 @@ class _Playbook:
 
     def next_log(self) -> tuple[int, tuple[int, ...], str]:
         'Return index, win IDs, and context of next expected log line.'
-        context, msg = self._current_line.split(maxsplit=1)
-        if ':' in context:
-            _, context = context.split(':')
-        expected_win_ids = tuple(int(idx) for idx in context.split(',') if idx)
+        context, msg = self._current_line.split(_CHAR_CONTEXT_SEP, maxsplit=1)
+        if _CHAR_RANGE in context:
+            _, context = context.split(_CHAR_RANGE)
+        expected_win_ids = tuple(
+                int(idx) for idx in context.split(_CHAR_WIN_ID_SEP) if idx)
         used_idx = self._idx
         self._play_till_next_log()
         return used_idx, expected_win_ids, msg
@@ -147,14 +158,15 @@ class _Playbook:
             self._idx += 1
             if (result := self._split_active_line(self._current_line)):
                 context, msg = result
-                if context == '>':
+                if context == _CHAR_PROMPT:
                     assert self.put_keypress is not None
                     for c in msg:
                         self.put_keypress(c)
                     self.put_keypress('KEY_ENTER')
                     continue
-                if ':' in context and msg.startswith('< '):
-                    client_id, win_ids = context.split(':')
+                if _CHAR_ID_TYPE_SEP in context\
+                        and msg.startswith(LOG_PREFIX_IN):
+                    client_id, win_ids = context.split(_CHAR_ID_TYPE_SEP)
                     client = self._get_client(int(client_id))
                     assert isinstance(client.conn, _FakeIrcConnection)
                     client.conn.put_server_msg(msg[2:])
@@ -165,9 +177,10 @@ class _Playbook:
     @staticmethod
     def _split_active_line(line: str) -> Optional[tuple[str, ...]]:
         'Return two-items tuple of split line, or None if inactive one.'
-        if line[:1] in '#|' or ' ' not in line:
+        if line[:1] in {_CHAR_COMMENT, _CHAR_ANCHOR}\
+                or _CHAR_CONTEXT_SEP not in line:
             return None
-        return tuple(line.split(' ', maxsplit=1))
+        return tuple(line.split(_CHAR_CONTEXT_SEP, maxsplit=1))
 
 
 class TestingClientTui(ClientTui):
@@ -179,8 +192,7 @@ class TestingClientTui(ClientTui):
     _path_test: Path
 
     def __init__(self, **kwargs) -> None:
-        fname_config = self._path_test.stem + '.toml'
-        path_config = PATH_TESTS.joinpath(fname_config)
+        path_config = PATH_TESTS.joinpath(self._path_test.stem + '.toml')
         if path_config.exists():
             assert path_config.is_file()
             self._path_config = path_config