-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser.py
More file actions
317 lines (258 loc) · 10.6 KB
/
test_parser.py
File metadata and controls
317 lines (258 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""Test markdown parsing functionality."""
import pytest
from pathlib import Path
from md2electraone.mdparser import (
parse_cc,
parse_range,
parse_choices,
parse_color,
parse_frontmatter,
infer_mode,
parse_controls_from_md,
)
class TestCCParsing:
"""Test CC number parsing with various formats."""
def test_parse_decimal_cc(self):
"""Test parsing decimal CC numbers."""
msg_type, cc, device_id = parse_cc("10")
assert msg_type == "C"
assert cc == 10
assert device_id is None
def test_parse_hex_cc_with_prefix(self):
"""Test parsing hex CC numbers with 0x prefix."""
msg_type, cc, device_id = parse_cc("0x1A")
assert msg_type == "C"
assert cc == 26
assert device_id is None
def test_parse_hex_cc_without_prefix(self):
"""Test parsing hex CC numbers without prefix."""
msg_type, cc, device_id = parse_cc("1A")
assert msg_type == "C"
assert cc == 26
assert device_id is None
def test_parse_nrpn(self):
"""Test parsing NRPN message type."""
msg_type, cc, device_id = parse_cc("N100")
assert msg_type == "N"
assert cc == 100
assert device_id is None
def test_parse_cc_with_prefix(self):
"""Test parsing CC with explicit C prefix."""
msg_type, cc, device_id = parse_cc("C10")
assert msg_type == "C"
assert cc == 10
assert device_id is None
def test_parse_envelope_ccs(self):
"""Test parsing comma-separated CCs for envelopes."""
msg_type, ccs, device_id = parse_cc("1,2,3,4")
assert msg_type == "C"
assert ccs == [1, 2, 3, 4]
assert device_id is None
def test_parse_invalid_cc(self):
"""Test parsing invalid CC returns None."""
msg_type, cc, device_id = parse_cc("invalid")
assert msg_type == "C"
assert cc is None
assert device_id is None
def test_parse_device_prefix(self):
"""Test parsing CC with device prefix."""
msg_type, cc, device_id = parse_cc("1:10")
assert msg_type == "C"
assert cc == 10
assert device_id == 1
def test_parse_device_prefix_with_message_type(self):
"""Test parsing CC with both device prefix and message type."""
msg_type, cc, device_id = parse_cc("2:N100")
assert msg_type == "N"
assert cc == 100
assert device_id == 2
def test_parse_envelope_ccs_with_device_prefix(self):
"""Test parsing envelope controls with device prefix on each CC."""
msg_type, ccs, device_id = parse_cc("2:14,2:15,2:16,2:17")
assert msg_type == "C"
assert ccs == [14, 15, 16, 17]
assert device_id == 2
def test_parse_envelope_ccs_with_device_prefix_once(self):
"""Test parsing envelope controls with device prefix on first CC only."""
msg_type, ccs, device_id = parse_cc("2:14,15,16,17")
assert msg_type == "C"
assert ccs == [14, 15, 16, 17]
assert device_id == 2
def test_parse_envelope_nrpn_with_device_prefix(self):
"""Test parsing envelope NRPN controls with device prefix on all."""
msg_type, ccs, device_id = parse_cc("2:N:2688,2:N:2689,2:N:2690,2:N:2691")
assert msg_type == "N"
assert ccs == [2688, 2689, 2690, 2691]
assert device_id == 2
def test_parse_envelope_nrpn_with_device_prefix_once(self):
"""Test parsing envelope NRPN controls with device prefix on first only."""
msg_type, ccs, device_id = parse_cc("2:N:2688,2689,2690,2691")
assert msg_type == "N"
assert ccs == [2688, 2689, 2690, 2691]
assert device_id == 2
class TestRangeParsing:
"""Test range parsing with various formats."""
def test_parse_simple_range(self):
"""Test parsing simple range."""
minv, maxv, default = parse_range("0-127")
assert minv == 0
assert maxv == 127
assert default is None
def test_parse_negative_range(self):
"""Test parsing range with negative values."""
minv, maxv, default = parse_range("-64-63")
assert minv == -64
assert maxv == 63
assert default is None
def test_parse_range_with_default(self):
"""Test parsing range with default value."""
minv, maxv, default = parse_range("0-127 (64)")
assert minv == 0
assert maxv == 127
assert default == 64
def test_parse_negative_range_with_default(self):
"""Test parsing negative range with default."""
minv, maxv, default = parse_range("-64-63 (0)")
assert minv == -64
assert maxv == 63
assert default == 0
def test_parse_single_value(self):
"""Test parsing single value as range."""
minv, maxv, default = parse_range("64")
assert minv == 64
assert maxv == 64
assert default is None
class TestChoicesParsing:
"""Test choices/options parsing."""
def test_parse_simple_choices(self):
"""Test parsing simple comma-separated choices."""
choices = parse_choices("Off, On", 0, 127)
assert len(choices) == 2
assert (0, "Off") in choices
assert (1, "On") in choices
def test_parse_choices_with_values(self):
"""Test parsing choices with explicit values."""
choices = parse_choices("Off(0), On(127)", 0, 127)
assert len(choices) == 2
assert (0, "Off") in choices
assert (127, "On") in choices
def test_parse_choices_with_equals(self):
"""Test parsing choices with = syntax."""
choices = parse_choices("0=Off, 127=On", 0, 127)
assert len(choices) == 2
assert (0, "Off") in choices
assert (127, "On") in choices
def test_parse_range_expansion(self):
"""Test parsing range expansion like 2-5=USB1-USB4."""
choices = parse_choices("2-5=USB1-USB4", 0, 127)
assert len(choices) == 4
# Check that values 2-5 are present
values = [v for v, _ in choices]
assert 2 in values
assert 5 in values
class TestColorParsing:
"""Test color value parsing."""
def test_parse_color_with_hash(self):
"""Test parsing color with # prefix."""
color = parse_color("#FF0000")
assert color == "FF0000"
def test_parse_color_without_hash(self):
"""Test parsing color without # prefix."""
color = parse_color("FF0000")
assert color == "FF0000"
def test_parse_lowercase_color(self):
"""Test parsing lowercase color."""
color = parse_color("ff0000")
assert color == "FF0000"
def test_parse_invalid_color(self):
"""Test parsing invalid color returns None."""
color = parse_color("invalid")
assert color is None
def test_parse_empty_color(self):
"""Test parsing empty color returns None."""
color = parse_color("")
assert color is None
class TestFrontmatterParsing:
"""Test YAML frontmatter parsing."""
def test_parse_simple_frontmatter(self):
"""Test parsing simple frontmatter."""
md = """---
name: Test Device
version: 2
port: 1
channel: 5
---
# Test"""
meta, remaining = parse_frontmatter(md)
assert meta["name"] == "Test Device"
assert meta["version"] == 2
assert meta["port"] == 1
assert meta["channel"] == 5
assert "# Test" in remaining
def test_parse_nested_frontmatter(self):
"""Test parsing nested frontmatter."""
md = """---
name: Test
midi:
port: 1
channel: 5
---
# Test"""
meta, remaining = parse_frontmatter(md)
assert meta["name"] == "Test"
assert "midi" in meta
assert meta["midi"]["port"] == 1
assert meta["midi"]["channel"] == 5
def test_parse_no_frontmatter(self):
"""Test parsing markdown without frontmatter."""
md = "# Test\n\nNo frontmatter here"
meta, remaining = parse_frontmatter(md)
assert meta == {}
assert remaining == md
class TestModeInference:
"""Test control mode inference."""
def test_infer_bipolar_mode(self):
"""Test inferring bipolar mode for negative ranges."""
mode = infer_mode(-64, 63, [])
assert mode == "bipolar"
def test_infer_momentary_mode(self):
"""Test inferring momentary mode."""
choices = [(0, "Released"), (127, "Momentary")]
mode = infer_mode(0, 127, choices)
assert mode == "momentary"
def test_infer_no_mode_for_toggle(self):
"""Test that toggle controls return None (handled elsewhere)."""
choices = [(0, "Off"), (127, "On")]
mode = infer_mode(0, 127, choices)
assert mode is None
def test_infer_no_mode_for_positive_range(self):
"""Test that positive ranges return None (use default)."""
mode = infer_mode(0, 127, [])
assert mode is None
class TestGroupColorInheritance:
"""Test that group colors are inherited by group members."""
def test_group_color_inheritance(self):
"""Test that group members without explicit colors inherit the group color."""
fixture_path = Path(__file__).parent / "fixtures" / "test_group_color.md"
md_content = fixture_path.read_text()
title, meta, all_specs, by_section = parse_controls_from_md(md_content)
# Find controls by label
controls = {spec.label: spec for spec in all_specs if not spec.is_group}
# Waveform should inherit group color FF0000
assert controls["Waveform"].color == "FF0000"
assert controls["Waveform"].group_id == "osc"
# Octave should inherit group color FF0000
assert controls["Octave"].color == "FF0000"
assert controls["Octave"].group_id == "osc"
# Filter Cutoff has explicit color 00FF00 (not in group)
assert controls["Filter Cutoff"].color == "00FF00"
assert controls["Filter Cutoff"].group_id is None
# Detune has explicit color 0000FF (overrides group color)
assert controls["Detune"].color == "0000FF"
assert controls["Detune"].group_id == "osc"
# Filter Resonance inherits from previous row's explicit color (current_color)
assert controls["Filter Resonance"].color == "0000FF"
assert controls["Filter Resonance"].group_id is None
# Level should inherit group color FF0000
assert controls["Level"].color == "FF0000"
assert controls["Level"].group_id == "osc"