Adafruit_CircuitPython_PIOASM/tests/pytest_helpers.py
KB Sriram 3e6a8bc392 Add type annotations for library and tests.
Verified that `mypy --strict adafruit_pioasm.py tests` runs without
errors.

Fixes: https://github.com/adafruit/Adafruit_CircuitPython_PIOASM/issues/24
2024-05-27 12:30:52 -07:00

48 lines
1.3 KiB
Python

# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Pytest helper functions
"""
try:
from typing import Any, List, Optional, Type
except ImportError:
pass
import pytest
import adafruit_pioasm
def nice_opcode(opcode: int) -> str:
nice = f"{opcode:016b}"
return nice[:3] + "_" + nice[3:8] + "_" + nice[8:]
def assert_assembles_to(source: str, expected: List[int]) -> None:
actual = adafruit_pioasm.assemble(source)
expected_bin = [nice_opcode(x) for x in expected]
actual_bin = [nice_opcode(x) for x in actual]
assert (
expected_bin == actual_bin
), f"Assembling {source!r}: Expected {expected_bin}, got {actual_bin}"
def assert_assembly_fails(
source: str, match: Optional[str] = None, errtype: Type[Exception] = RuntimeError
) -> None:
with pytest.raises(errtype, match=match):
adafruit_pioasm.assemble(source)
# if match:
# with pytest.raises(errtype, match=match):
# adafruit_pioasm.assemble(source)
# else:
# with pytest.raises(errtype):
# adafruit_pioasm.assemble(source)
def assert_pio_kwargs(source: str, **kw: Any) -> None:
program = adafruit_pioasm.Program(source)
assert kw == program.pio_kwargs