feat(rawcli): flexible entry + parser (Phase 2)
- parser.py: classify a line into control / function-call / raw payload; parse loose hex/binary with intermixed 0x/0b tokens (whitespace- insensitive, defaulting to the session entry mode) - entry.py: byte_space() byte-group formatting + key bindings — Ctrl-/ toggles hex/binary entry mode (breadcrumb updates), digits auto-space into byte groups as you type - app.py: dispatch() drives the loop via parse_line (testable without a live prompt); identify/call are stubbed for Phases 3/4 14 new tests (parser tokens/intermix/errors, byte_space, dispatch). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
"""rawcli Phase 1 — session state, breadcrumb, trace rendering, CLI dispatch. Hardware-free."""
|
||||
import re
|
||||
|
||||
import pytest
|
||||
|
||||
from pm3py.cli.main import build_parser, main
|
||||
from pm3py.cli.rawcli.session import RawSession
|
||||
from pm3py.cli.rawcli.trace_view import render_exchange
|
||||
from pm3py.cli.rawcli.parser import parse_token, parse_bytes, parse_line
|
||||
from pm3py.cli.rawcli.entry import byte_space
|
||||
from pm3py.cli.rawcli.app import dispatch
|
||||
|
||||
_ANSI = re.compile(r"\x1b\[[0-9;]*m")
|
||||
|
||||
@@ -65,3 +70,71 @@ class TestCliDispatch:
|
||||
rc = main([])
|
||||
assert rc == 1
|
||||
assert "rawcli" in capsys.readouterr().out
|
||||
|
||||
|
||||
class TestParser:
|
||||
def test_hex_tokens(self):
|
||||
assert parse_token("30") == b"\x30"
|
||||
assert parse_token("0x3004") == b"\x30\x04"
|
||||
assert parse_bytes("30 04") == b"\x30\x04"
|
||||
assert parse_bytes("3004") == b"\x30\x04" # default hex mode
|
||||
|
||||
def test_binary_tokens(self):
|
||||
assert parse_token("0b00110000") == b"\x30"
|
||||
assert parse_token("00110000", "bin") == b"\x30"
|
||||
|
||||
def test_intermixed(self):
|
||||
assert parse_bytes("0b00110000 0x04") == b"\x30\x04"
|
||||
assert parse_bytes("0x30 00000100", "bin") == b"\x30\x04" # bare token uses mode
|
||||
|
||||
def test_bad_tokens(self):
|
||||
with pytest.raises(ValueError):
|
||||
parse_token("303") # odd hex
|
||||
with pytest.raises(ValueError):
|
||||
parse_token("0b0011") # not a whole byte
|
||||
|
||||
def test_classify_control(self):
|
||||
c = parse_line("identify")
|
||||
assert c.kind == "control" and c.name == "identify"
|
||||
assert parse_line("help ntag215").kind == "control"
|
||||
|
||||
def test_classify_call(self):
|
||||
c = parse_line("READ(4)")
|
||||
assert c.kind == "call" and c.name == "READ" and c.args == ["4"]
|
||||
c = parse_line("WRITE(4, 0x00112233)")
|
||||
assert c.args == ["4", "0x00112233"]
|
||||
assert parse_line("GET_DATA()").kind == "call"
|
||||
|
||||
def test_classify_raw(self):
|
||||
c = parse_line("30 04")
|
||||
assert c.kind == "raw" and c.payload == b"\x30\x04"
|
||||
|
||||
|
||||
class TestByteSpace:
|
||||
def test_hex_grouping(self):
|
||||
assert byte_space("3004", "hex") == "30 04"
|
||||
assert byte_space("300", "hex") == "30 0"
|
||||
assert byte_space("30 04", "hex") == "30 04" # idempotent
|
||||
|
||||
def test_binary_grouping(self):
|
||||
assert byte_space("0011000000000100", "bin") == "00110000 00000100"
|
||||
|
||||
def test_prefixed_left_alone(self):
|
||||
assert byte_space("0x3004", "hex") == "0x3004" # explicit prefix not mangled
|
||||
|
||||
|
||||
class TestDispatch:
|
||||
def test_quit_returns_false(self):
|
||||
assert dispatch(RawSession(), "quit") is False
|
||||
|
||||
def test_help_prints(self, capsys):
|
||||
assert dispatch(RawSession(), "help") is True
|
||||
assert "rawcli" in capsys.readouterr().out
|
||||
|
||||
def test_raw_renders_without_device(self, capsys):
|
||||
assert dispatch(RawSession(), "30 04") is True
|
||||
assert "30 04" in _plain(capsys.readouterr().out)
|
||||
|
||||
def test_bad_hex_reports(self, capsys):
|
||||
assert dispatch(RawSession(), "0b0011") is True
|
||||
assert "whole number of bytes" in capsys.readouterr().out
|
||||
|
||||
Reference in New Issue
Block a user