Skip to content

Commit df06622

Browse files
committed
make my exceptions more consistent, and add tests.
1 parent b065422 commit df06622

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ bundles
1616
dist
1717
**/*.egg-info
1818
.vscode
19+
.venv

adafruit_pioasm.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ def assemble(text_program):
106106
try:
107107
assembled[-1] |= CONDITIONS.index(instruction[1]) << 5
108108
except ValueError as exc:
109-
raise SyntaxError(
110-
f"Invalid jmp condition {instruction[1]}"
109+
raise ValueError(
110+
f"Invalid jmp condition '{instruction[1]}'"
111111
) from exc
112112

113113
elif instruction[0] == "wait":
@@ -159,7 +159,7 @@ def assemble(text_program):
159159
try:
160160
assembled[-1] |= MOV_SOURCES.index(source)
161161
except ValueError as exc:
162-
raise RuntimeError("Invalid mov source:", source) from exc
162+
raise ValueError(f"Invalid mov source '{source}'") from exc
163163
else:
164164
assembled[-1] |= MOV_SOURCES.index(source_split[1])
165165
if source[:1] == "!":
@@ -194,7 +194,7 @@ def assemble(text_program):
194194
try:
195195
assembled[-1] |= SET_DESTINATIONS.index(instruction[1]) << 5
196196
except ValueError as exc:
197-
raise RuntimeError(f"Unknown set destination {instruction[1]}") from exc
197+
raise ValueError(f"Invalid set destination '{instruction[1]}'") from exc
198198
value = int(instruction[-1])
199199
if not 0 <= value <= 31:
200200
raise RuntimeError("Set value out of range")

tests/testpioasm.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ def assertAssemblesTo(self, source, expected):
2929
f"Assembling {source!r}: Expected {expected_bin}, got {actual_bin}",
3030
)
3131

32-
def assertAssemblyFails(self, source):
33-
self.assertRaises(RuntimeError, adafruit_pioasm.assemble, source)
32+
def assertAssemblyFails(self, source, match=None, errtype=RuntimeError):
33+
if match:
34+
self.assertRaisesRegex(errtype, match, adafruit_pioasm.assemble, source)
35+
else:
36+
self.assertRaises(errtype, adafruit_pioasm.assemble, source)
3437

3538
def testNonsense(self):
3639
self.assertAssemblyFails("nope")
@@ -52,6 +55,18 @@ def testSidesetOpt(self):
5255
)
5356
self.assertAssemblesTo(".side_set 1 opt\nnop [1]", [0b101_00001_010_00_010])
5457

58+
def testMov(self):
59+
# non happy path
60+
self.assertAssemblyFails(
61+
"mov x, blah", match="Invalid mov source 'blah'", errtype=ValueError
62+
)
63+
64+
def testSet(self):
65+
# non happy path
66+
self.assertAssemblyFails(
67+
"set isr, 1", match="Invalid set destination 'isr'", errtype=ValueError
68+
)
69+
5570
def testJmp(self):
5671
self.assertAssemblesTo("l:\njmp l", [0b000_00000_000_00000])
5772
self.assertAssemblesTo("l:\njmp 7", [0b000_00000_000_00111])
@@ -63,6 +78,10 @@ def testJmp(self):
6378
self.assertAssemblesTo("jmp x!=y, l\nl:", [0b000_00000_101_00001])
6479
self.assertAssemblesTo("jmp pin, l\nl:", [0b000_00000_110_00001])
6580
self.assertAssemblesTo("jmp !osre, l\nl:", [0b000_00000_111_00001])
81+
# non happy path
82+
self.assertAssemblyFails(
83+
"jmp x--., l\nl:", match="Invalid jmp condition 'x--.'", errtype=ValueError
84+
)
6685

6786
def testWait(self):
6887
self.assertAssemblesTo("wait 0 gpio 0", [0b001_00000_0_00_00000])

0 commit comments

Comments
 (0)