Move extract_packed_value to shared component test helper

Add tests/component_tests/helpers.py with typed extract_packed_value()
and INTERNAL_BIT constant, replacing 5 duplicate copies.
This commit is contained in:
J. Nick Koston
2026-03-06 10:56:51 -10:00
parent c704d97804
commit 41c413d5f7
6 changed files with 31 additions and 64 deletions
+3 -13
View File
@@ -1,16 +1,6 @@
"""Tests for the button component"""
import re
_INTERNAL_BIT = 1 << 24
def _extract_packed_value(main_cpp, var_name):
"""Extract the third (packed) argument from a configure_entity_ call."""
pattern = rf"{re.escape(var_name)}->configure_entity_\([^,]+,\s*\w+,\s*(\d+)\)"
match = re.search(pattern, main_cpp)
assert match, f"configure_entity_ call not found for {var_name}"
return int(match.group(1))
from tests.component_tests.helpers import INTERNAL_BIT, extract_packed_value
def test_button_is_setup(generate_main):
@@ -52,5 +42,5 @@ def test_button_config_value_internal_set(generate_main):
main_cpp = generate_main("tests/component_tests/button/test_button.yaml")
# Then: wol_1 has internal: true, wol_2 has internal: false
assert _extract_packed_value(main_cpp, "wol_1") & _INTERNAL_BIT != 0
assert _extract_packed_value(main_cpp, "wol_2") & _INTERNAL_BIT == 0
assert extract_packed_value(main_cpp, "wol_1") & INTERNAL_BIT != 0
assert extract_packed_value(main_cpp, "wol_2") & INTERNAL_BIT == 0