Skip to content

Commit 9b21b6a

Browse files
committed
[testing] Add basic unit tests for config parsing/writing.
1 parent d0df0dc commit 9b21b6a

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

tests/config_test.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
3+
import pytest
4+
import tomlkit
5+
from busytag.config import ToolConfig
6+
from busytag.types import LedConfig, LedPin
7+
8+
9+
@pytest.fixture
10+
def testdata_path():
11+
def _testdata_path(name):
12+
return os.path.join(os.path.dirname(__file__), 'testdata',
13+
f"{name}.toml")
14+
15+
return _testdata_path
16+
17+
18+
def test_loads_config_with_device(testdata_path):
19+
config = ToolConfig(testdata_path('config-with-device'))
20+
assert config.device == '/dev/tty.usbmodem'
21+
22+
23+
def test_loads_config_with_led_preset(testdata_path):
24+
config = ToolConfig(testdata_path('config-with-led-patterns'))
25+
expected_rb_pattern = {
26+
LedConfig(
27+
pins=LedPin.PIN_0 | LedPin.PIN_2 | LedPin.PIN_4 | LedPin.PIN_6,
28+
color='FF0000'),
29+
LedConfig(
30+
pins=LedPin.PIN_1 | LedPin.PIN_3 | LedPin.PIN_5,
31+
color='0000FF'
32+
)
33+
}
34+
35+
diff = set(config.get_led_preset('rb')) ^ expected_rb_pattern
36+
assert not diff
37+
38+
39+
40+
def test_writes_config(tmp_path):
41+
p = tmp_path / 'config_with_device.toml'
42+
config = ToolConfig(str(p))
43+
44+
assert config.device is None
45+
config.device = '/dev/tty.somedevice'
46+
config.write_to_file()
47+
48+
c = tomlkit.loads(p.read_text())
49+
assert c['device'] == '/dev/tty.somedevice'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
device = "/dev/tty.usbmodem"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[led_presets.green]]
2+
pins = 127
3+
color = '00FF00'
4+
5+
[[led_presets.rb]]
6+
pins = 85
7+
color = 'FF0000'
8+
9+
[[led_presets.rb]]
10+
pins = 42
11+
color = '0000FF'

0 commit comments

Comments
 (0)