Skip to content

Commit 0d58eb4

Browse files
authored
Merge pull request #57 from tekktrik/dev/use-pytest
Switch to pytest for testing
2 parents fdc9da9 + 66db505 commit 0d58eb4

File tree

7 files changed

+193
-161
lines changed

7 files changed

+193
-161
lines changed

tests/__init__.py

Whitespace-only changes.

tests/pytest_helpers.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
Pytest helper functions
7+
"""
8+
9+
import pytest
10+
11+
import adafruit_pioasm
12+
13+
14+
def nice_opcode(opcode):
15+
opcode = f"{opcode:016b}"
16+
return opcode[:3] + "_" + opcode[3:8] + "_" + opcode[8:]
17+
18+
19+
def assert_assembles_to(source, expected):
20+
actual = adafruit_pioasm.assemble(source)
21+
expected_bin = [nice_opcode(x) for x in expected]
22+
actual_bin = [nice_opcode(x) for x in actual]
23+
assert (
24+
expected_bin == actual_bin
25+
), f"Assembling {source!r}: Expected {expected_bin}, got {actual_bin}"
26+
27+
28+
def assert_assembly_fails(source, match=None, errtype=RuntimeError):
29+
with pytest.raises(errtype, match=match):
30+
adafruit_pioasm.assemble(source)
31+
# if match:
32+
# with pytest.raises(errtype, match=match):
33+
# adafruit_pioasm.assemble(source)
34+
# else:
35+
# with pytest.raises(errtype):
36+
# adafruit_pioasm.assemble(source)
37+
38+
39+
def assert_pio_kwargs(source, **kw):
40+
program = adafruit_pioasm.Program(source)
41+
assert kw == program.pio_kwargs

tests/test_mov.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
Tests mov
7+
"""
8+
9+
from pytest_helpers import assert_assembles_to, assert_assembly_fails
10+
11+
12+
def test_mov_non_happy():
13+
# non happy path
14+
assert_assembly_fails(
15+
"mov x, blah", match="Invalid mov source 'blah'", errtype=ValueError
16+
)
17+
18+
19+
def test_mov_invert():
20+
# test moving and inverting
21+
assert_assembles_to("mov x, ~ x", [0b101_00000_001_01_001])
22+
assert_assembles_to("mov x, ~x", [0b101_00000_001_01_001])
23+
assert_assembles_to("mov x, !x", [0b101_00000_001_01_001])
24+
25+
26+
def test_mov_reverse():
27+
# test moving and reversing bits
28+
assert_assembles_to("mov x, :: x", [0b101_00000_001_10_001])
29+
assert_assembles_to("mov x, ::x", [0b101_00000_001_10_001])

tests/test_nop.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
Tests nop
7+
"""
8+
9+
from pytest_helpers import assert_assembles_to, assert_assembly_fails, assert_pio_kwargs
10+
11+
12+
def test_nonsense():
13+
assert_assembly_fails("nope")
14+
15+
16+
def test_nop():
17+
assert_assembles_to("nop", [0b101_00000_010_00_010])
18+
assert_assembles_to("nop\nnop", [0b101_00000_010_00_010, 0b101_00000_010_00_010])
19+
assert_assembles_to("nop [1]", [0b101_00001_010_00_010])
20+
assert_assembles_to("nop [31]", [0b101_11111_010_00_010])
21+
assert_assembles_to(".side_set 1\nnop side 1", [0b101_10000_010_00_010])
22+
assert_assembles_to(".side_set 1\nnop side 1 [15]", [0b101_11111_010_00_010])
23+
24+
25+
def test_sideset_opt():
26+
assert_assembles_to(".side_set 1 opt\nnop side 1", [0b101_11000_010_00_010])
27+
assert_assembles_to(".side_set 1 opt\nnop side 0", [0b101_10000_010_00_010])
28+
assert_assembles_to(".side_set 1 opt\nnop side 0 [1]", [0b101_10001_010_00_010])
29+
assert_assembles_to(".side_set 1 opt\nnop [1]", [0b101_00001_010_00_010])
30+
assert_assembles_to(".side_set 1 opt\nnop [7]", [0b101_00111_010_00_010])
31+
assert_assembles_to(".side_set 1 opt\nnop side 1 [1]", [0b101_11001_010_00_010])
32+
assert_assembles_to(".side_set 1 opt\nnop side 0 [7]", [0b101_10111_010_00_010])
33+
34+
35+
def test_set():
36+
# non happy path
37+
assert_assembly_fails(
38+
"set isr, 1", match="Invalid set destination 'isr'", errtype=ValueError
39+
)
40+
41+
42+
def test_jmp():
43+
assert_assembles_to("l:\njmp l", [0b000_00000_000_00000])
44+
assert_assembles_to("l:\njmp 7", [0b000_00000_000_00111])
45+
assert_assembles_to("jmp l\nl:", [0b000_00000_000_00001])
46+
assert_assembles_to("jmp !x, l\nl:", [0b000_00000_001_00001])
47+
assert_assembles_to("jmp x--, l\nl:", [0b000_00000_010_00001])
48+
assert_assembles_to("jmp !y, l\nl:", [0b000_00000_011_00001])
49+
assert_assembles_to("jmp y--, l\nl:", [0b000_00000_100_00001])
50+
assert_assembles_to("jmp x!=y, l\nl:", [0b000_00000_101_00001])
51+
assert_assembles_to("jmp pin, l\nl:", [0b000_00000_110_00001])
52+
assert_assembles_to("jmp !osre, l\nl:", [0b000_00000_111_00001])
53+
# non happy path
54+
assert_assembly_fails(
55+
"jmp x--., l\nl:", match="Invalid jmp condition 'x--.'", errtype=ValueError
56+
)
57+
58+
59+
def test_wait():
60+
assert_assembles_to("wait 0 gpio 0", [0b001_00000_0_00_00000])
61+
assert_assembles_to("wait 0 gpio 1", [0b001_00000_0_00_00001])
62+
assert_assembles_to("wait 1 gpio 2", [0b001_00000_1_00_00010])
63+
assert_assembles_to("wait 0 pin 0", [0b001_00000_0_01_00000])
64+
assert_assembles_to("wait 0 pin 1", [0b001_00000_0_01_00001])
65+
assert_assembles_to("wait 1 pin 2", [0b001_00000_1_01_00010])
66+
assert_assembles_to("wait 0 irq 0", [0b001_00000_0_10_00000])
67+
assert_assembles_to("wait 0 irq 0 rel", [0b001_00000_0_10_10000])
68+
assert_assembles_to("wait 1 irq 0", [0b001_00000_1_10_00000])
69+
assert_assembles_to("wait 0 irq 1 rel", [0b001_00000_0_10_10001])
70+
71+
72+
def test_limits():
73+
assert_assembly_fails(".side_set 1\nnop side 2")
74+
assert_assembly_fails(".side_set 1\nnop side 2 [1]")
75+
assert_assembly_fails("nop [32]")
76+
assert_assembly_fails(".side_set 1\nnop side 0 [16]")
77+
assert_assembly_fails(".side_set 1 opt\nnop side 0 [8]")
78+
79+
80+
def test_cls():
81+
assert_pio_kwargs("", sideset_enable=False)
82+
assert_pio_kwargs(".side_set 1", sideset_pin_count=1, sideset_enable=False)
83+
assert_pio_kwargs(".side_set 3 opt", sideset_pin_count=3, sideset_enable=True)

tests/test_radix.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
Tests radix
7+
"""
8+
9+
from pytest_helpers import assert_assembles_to
10+
11+
12+
def test_octal():
13+
assert_assembles_to(".side_set 0o1\nset x, 0o11", [0b111_00000_001_01001])
14+
15+
16+
def test_binary():
17+
assert_assembles_to(".side_set 0b101\nnop side 0b10001", [0b101_10001_010_00_010])
18+
19+
20+
def test_hex():
21+
assert_assembles_to(".side_set 0x0\nnop [0x10]", [0b101_10000_010_00_010])

tests/test_wrap.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
Tests wrap
7+
"""
8+
9+
from pytest_helpers import assert_assembly_fails, assert_pio_kwargs
10+
11+
12+
def test_wrap():
13+
assert_assembly_fails(".wrap")
14+
assert_pio_kwargs(
15+
"nop\n.wrap_target\nnop\nnop\n.wrap",
16+
sideset_enable=False,
17+
wrap=2,
18+
wrap_target=1,
19+
)

tests/testpioasm.py

-161
This file was deleted.

0 commit comments

Comments
 (0)