Skip to content

Commit 66e7712

Browse files
committed
Add first unit tests
1 parent 95f2336 commit 66e7712

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ include *.toml
44
include LICENSE
55
include .pylintrc
66
include noxfile.py
7+
recursive-include tests *.py

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ ignore =
2424
C812, # Missing trailing comma
2525
E203, # Whitespace before ':'
2626
D102, # Missing docstring in public method
27+
per-file-ignores =
28+
# Missing docstring in public function
29+
tests/*:D103,
2730

stm32loader/bootloader.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ def go(self, address):
249249

250250
def write_memory(self, address, data):
251251
nr_of_bytes = len(data)
252+
if nr_of_bytes == 0:
253+
return
252254
assert nr_of_bytes <= 256
253255

254256
if not self.command(self.Command.WRITE_MEMORY):

tests/test_bootloader.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
from stm32loader.bootloader import Stm32Bootloader
3+
4+
import pytest
5+
6+
try:
7+
from unittest.mock import MagicMock
8+
except ImportError:
9+
# Python version <= 3.2
10+
from mock import MagicMock
11+
12+
13+
@pytest.fixture
14+
def connection():
15+
connection = MagicMock()
16+
connection.read.return_value = [Stm32Bootloader.Reply.ACK]
17+
return connection
18+
19+
20+
@pytest.fixture
21+
def write(connection):
22+
def write_called_with_data(data):
23+
data_call = (bytearray(data), )
24+
return 1 == sum(args[0] == data_call for args in connection.write.call_args_list)
25+
connection.write.called_with_data = write_called_with_data
26+
return connection.write
27+
28+
29+
@pytest.fixture
30+
def bootloader(connection):
31+
return Stm32Bootloader(connection)
32+
33+
34+
def test_constructor_with_connection_None_passes():
35+
Stm32Bootloader(connection=None)
36+
37+
38+
def test_constructor_does_not_use_connection_directly(connection):
39+
Stm32Bootloader(connection)
40+
assert not connection.method_calls
41+
42+
43+
def test_write_memory_with_zero_bytes_does_not_send_anything(bootloader, connection):
44+
bootloader.write_memory(0, b'')
45+
assert not connection.method_calls
46+
47+
48+
def test_write_memory_with_single_byte_sends_four_data_bytes_padded_with_0xff(bootloader, write):
49+
bootloader.write_memory(0, b'1')
50+
assert write.called_with_data(b'1\xff\xff\xff')
51+
52+
53+
def test_encode_address_returns_correct_bytes_with_checksum():
54+
encoded_address = Stm32Bootloader._encode_address(0x04030201)
55+
assert bytes(encoded_address) == b'\x04\x03\x02\x01\x04'

0 commit comments

Comments
 (0)