Skip to content

Switch to pytest for testing #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed tests/__init__.py
Empty file.
41 changes: 41 additions & 0 deletions tests/pytest_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
Pytest helper functions
"""

import pytest

import adafruit_pioasm


def nice_opcode(opcode):
opcode = f"{opcode:016b}"
return opcode[:3] + "_" + opcode[3:8] + "_" + opcode[8:]


def assert_assembles_to(source, expected):
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, match=None, errtype=RuntimeError):
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, **kw):
program = adafruit_pioasm.Program(source)
assert kw == program.pio_kwargs
29 changes: 29 additions & 0 deletions tests/test_mov.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
Tests mov
"""

from pytest_helpers import assert_assembles_to, assert_assembly_fails


def test_mov_non_happy():
# non happy path
assert_assembly_fails(
"mov x, blah", match="Invalid mov source 'blah'", errtype=ValueError
)


def test_mov_invert():
# test moving and inverting
assert_assembles_to("mov x, ~ x", [0b101_00000_001_01_001])
assert_assembles_to("mov x, ~x", [0b101_00000_001_01_001])
assert_assembles_to("mov x, !x", [0b101_00000_001_01_001])


def test_mov_reverse():
# test moving and reversing bits
assert_assembles_to("mov x, :: x", [0b101_00000_001_10_001])
assert_assembles_to("mov x, ::x", [0b101_00000_001_10_001])
83 changes: 83 additions & 0 deletions tests/test_nop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
Tests nop
"""

from pytest_helpers import assert_assembles_to, assert_assembly_fails, assert_pio_kwargs


def test_nonsense():
assert_assembly_fails("nope")


def test_nop():
assert_assembles_to("nop", [0b101_00000_010_00_010])
assert_assembles_to("nop\nnop", [0b101_00000_010_00_010, 0b101_00000_010_00_010])
assert_assembles_to("nop [1]", [0b101_00001_010_00_010])
assert_assembles_to("nop [31]", [0b101_11111_010_00_010])
assert_assembles_to(".side_set 1\nnop side 1", [0b101_10000_010_00_010])
assert_assembles_to(".side_set 1\nnop side 1 [15]", [0b101_11111_010_00_010])


def test_sideset_opt():
assert_assembles_to(".side_set 1 opt\nnop side 1", [0b101_11000_010_00_010])
assert_assembles_to(".side_set 1 opt\nnop side 0", [0b101_10000_010_00_010])
assert_assembles_to(".side_set 1 opt\nnop side 0 [1]", [0b101_10001_010_00_010])
assert_assembles_to(".side_set 1 opt\nnop [1]", [0b101_00001_010_00_010])
assert_assembles_to(".side_set 1 opt\nnop [7]", [0b101_00111_010_00_010])
assert_assembles_to(".side_set 1 opt\nnop side 1 [1]", [0b101_11001_010_00_010])
assert_assembles_to(".side_set 1 opt\nnop side 0 [7]", [0b101_10111_010_00_010])


def test_set():
# non happy path
assert_assembly_fails(
"set isr, 1", match="Invalid set destination 'isr'", errtype=ValueError
)


def test_jmp():
assert_assembles_to("l:\njmp l", [0b000_00000_000_00000])
assert_assembles_to("l:\njmp 7", [0b000_00000_000_00111])
assert_assembles_to("jmp l\nl:", [0b000_00000_000_00001])
assert_assembles_to("jmp !x, l\nl:", [0b000_00000_001_00001])
assert_assembles_to("jmp x--, l\nl:", [0b000_00000_010_00001])
assert_assembles_to("jmp !y, l\nl:", [0b000_00000_011_00001])
assert_assembles_to("jmp y--, l\nl:", [0b000_00000_100_00001])
assert_assembles_to("jmp x!=y, l\nl:", [0b000_00000_101_00001])
assert_assembles_to("jmp pin, l\nl:", [0b000_00000_110_00001])
assert_assembles_to("jmp !osre, l\nl:", [0b000_00000_111_00001])
# non happy path
assert_assembly_fails(
"jmp x--., l\nl:", match="Invalid jmp condition 'x--.'", errtype=ValueError
)


def test_wait():
assert_assembles_to("wait 0 gpio 0", [0b001_00000_0_00_00000])
assert_assembles_to("wait 0 gpio 1", [0b001_00000_0_00_00001])
assert_assembles_to("wait 1 gpio 2", [0b001_00000_1_00_00010])
assert_assembles_to("wait 0 pin 0", [0b001_00000_0_01_00000])
assert_assembles_to("wait 0 pin 1", [0b001_00000_0_01_00001])
assert_assembles_to("wait 1 pin 2", [0b001_00000_1_01_00010])
assert_assembles_to("wait 0 irq 0", [0b001_00000_0_10_00000])
assert_assembles_to("wait 0 irq 0 rel", [0b001_00000_0_10_10000])
assert_assembles_to("wait 1 irq 0", [0b001_00000_1_10_00000])
assert_assembles_to("wait 0 irq 1 rel", [0b001_00000_0_10_10001])


def test_limits():
assert_assembly_fails(".side_set 1\nnop side 2")
assert_assembly_fails(".side_set 1\nnop side 2 [1]")
assert_assembly_fails("nop [32]")
assert_assembly_fails(".side_set 1\nnop side 0 [16]")
assert_assembly_fails(".side_set 1 opt\nnop side 0 [8]")


def test_cls():
assert_pio_kwargs("", sideset_enable=False)
assert_pio_kwargs(".side_set 1", sideset_pin_count=1, sideset_enable=False)
assert_pio_kwargs(".side_set 3 opt", sideset_pin_count=3, sideset_enable=True)
21 changes: 21 additions & 0 deletions tests/test_radix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
Tests radix
"""

from pytest_helpers import assert_assembles_to


def test_octal():
assert_assembles_to(".side_set 0o1\nset x, 0o11", [0b111_00000_001_01001])


def test_binary():
assert_assembles_to(".side_set 0b101\nnop side 0b10001", [0b101_10001_010_00_010])


def test_hex():
assert_assembles_to(".side_set 0x0\nnop [0x10]", [0b101_10000_010_00_010])
19 changes: 19 additions & 0 deletions tests/test_wrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
Tests wrap
"""

from pytest_helpers import assert_assembly_fails, assert_pio_kwargs


def test_wrap():
assert_assembly_fails(".wrap")
assert_pio_kwargs(
"nop\n.wrap_target\nnop\nnop\n.wrap",
sideset_enable=False,
wrap=2,
wrap_target=1,
)
161 changes: 0 additions & 161 deletions tests/testpioasm.py

This file was deleted.