mirror of
https://github.com/esphome/esphome.git
synced 2026-06-01 01:19:45 +08:00
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:
@@ -1,16 +1,6 @@
|
|||||||
"""Tests for the binary sensor component."""
|
"""Tests for the binary sensor component."""
|
||||||
|
|
||||||
import re
|
from tests.component_tests.helpers import INTERNAL_BIT, extract_packed_value
|
||||||
|
|
||||||
_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))
|
|
||||||
|
|
||||||
|
|
||||||
def test_binary_sensor_is_setup(generate_main):
|
def test_binary_sensor_is_setup(generate_main):
|
||||||
@@ -57,8 +47,8 @@ def test_binary_sensor_config_value_internal_set(generate_main):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Then: bs_1 has internal: true, bs_2 has internal: false
|
# Then: bs_1 has internal: true, bs_2 has internal: false
|
||||||
assert _extract_packed_value(main_cpp, "bs_1") & _INTERNAL_BIT != 0
|
assert extract_packed_value(main_cpp, "bs_1") & INTERNAL_BIT != 0
|
||||||
assert _extract_packed_value(main_cpp, "bs_2") & _INTERNAL_BIT == 0
|
assert extract_packed_value(main_cpp, "bs_2") & INTERNAL_BIT == 0
|
||||||
|
|
||||||
|
|
||||||
def test_binary_sensor_config_value_use_raw_set(generate_main):
|
def test_binary_sensor_config_value_use_raw_set(generate_main):
|
||||||
|
|||||||
@@ -1,16 +1,6 @@
|
|||||||
"""Tests for the button component"""
|
"""Tests for the button component"""
|
||||||
|
|
||||||
import re
|
from tests.component_tests.helpers import INTERNAL_BIT, extract_packed_value
|
||||||
|
|
||||||
_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))
|
|
||||||
|
|
||||||
|
|
||||||
def test_button_is_setup(generate_main):
|
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")
|
main_cpp = generate_main("tests/component_tests/button/test_button.yaml")
|
||||||
|
|
||||||
# Then: wol_1 has internal: true, wol_2 has internal: false
|
# 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_1") & INTERNAL_BIT != 0
|
||||||
assert _extract_packed_value(main_cpp, "wol_2") & _INTERNAL_BIT == 0
|
assert extract_packed_value(main_cpp, "wol_2") & INTERNAL_BIT == 0
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
"""Shared helpers for component tests."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
INTERNAL_BIT = 1 << 24
|
||||||
|
|
||||||
|
|
||||||
|
def extract_packed_value(main_cpp: str, var_name: str) -> int:
|
||||||
|
"""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))
|
||||||
@@ -1,14 +1,6 @@
|
|||||||
"""Tests for the sensor component."""
|
"""Tests for the sensor component."""
|
||||||
|
|
||||||
import re
|
from tests.component_tests.helpers import extract_packed_value
|
||||||
|
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
|
|
||||||
def test_sensor_device_class_set(generate_main):
|
def test_sensor_device_class_set(generate_main):
|
||||||
@@ -21,5 +13,5 @@ def test_sensor_device_class_set(generate_main):
|
|||||||
main_cpp = generate_main("tests/component_tests/sensor/test_sensor.yaml")
|
main_cpp = generate_main("tests/component_tests/sensor/test_sensor.yaml")
|
||||||
|
|
||||||
# Then: device_class: voltage means packed value must be non-zero
|
# Then: device_class: voltage means packed value must be non-zero
|
||||||
packed = _extract_packed_value(main_cpp, "s_1")
|
packed = extract_packed_value(main_cpp, "s_1")
|
||||||
assert packed != 0
|
assert packed != 0
|
||||||
|
|||||||
@@ -1,16 +1,6 @@
|
|||||||
"""Tests for the text component."""
|
"""Tests for the text component."""
|
||||||
|
|
||||||
import re
|
from tests.component_tests.helpers import INTERNAL_BIT, extract_packed_value
|
||||||
|
|
||||||
_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))
|
|
||||||
|
|
||||||
|
|
||||||
def test_text_is_setup(generate_main):
|
def test_text_is_setup(generate_main):
|
||||||
@@ -50,8 +40,8 @@ def test_text_config_value_internal_set(generate_main):
|
|||||||
main_cpp = generate_main("tests/component_tests/text/test_text.yaml")
|
main_cpp = generate_main("tests/component_tests/text/test_text.yaml")
|
||||||
|
|
||||||
# Then: it_2 has internal: false, it_3 has internal: true
|
# Then: it_2 has internal: false, it_3 has internal: true
|
||||||
assert _extract_packed_value(main_cpp, "it_2") & _INTERNAL_BIT == 0
|
assert extract_packed_value(main_cpp, "it_2") & INTERNAL_BIT == 0
|
||||||
assert _extract_packed_value(main_cpp, "it_3") & _INTERNAL_BIT != 0
|
assert extract_packed_value(main_cpp, "it_3") & INTERNAL_BIT != 0
|
||||||
|
|
||||||
|
|
||||||
def test_text_config_value_mode_set(generate_main):
|
def test_text_config_value_mode_set(generate_main):
|
||||||
|
|||||||
@@ -1,16 +1,6 @@
|
|||||||
"""Tests for the text sensor component."""
|
"""Tests for the text sensor component."""
|
||||||
|
|
||||||
import re
|
from tests.component_tests.helpers import INTERNAL_BIT, extract_packed_value
|
||||||
|
|
||||||
_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))
|
|
||||||
|
|
||||||
|
|
||||||
def test_text_sensor_is_setup(generate_main):
|
def test_text_sensor_is_setup(generate_main):
|
||||||
@@ -52,8 +42,8 @@ def test_text_sensor_config_value_internal_set(generate_main):
|
|||||||
main_cpp = generate_main("tests/component_tests/text_sensor/test_text_sensor.yaml")
|
main_cpp = generate_main("tests/component_tests/text_sensor/test_text_sensor.yaml")
|
||||||
|
|
||||||
# Then: ts_2 has internal: true, ts_3 has internal: false
|
# Then: ts_2 has internal: true, ts_3 has internal: false
|
||||||
assert _extract_packed_value(main_cpp, "ts_2") & _INTERNAL_BIT != 0
|
assert extract_packed_value(main_cpp, "ts_2") & INTERNAL_BIT != 0
|
||||||
assert _extract_packed_value(main_cpp, "ts_3") & _INTERNAL_BIT == 0
|
assert extract_packed_value(main_cpp, "ts_3") & INTERNAL_BIT == 0
|
||||||
|
|
||||||
|
|
||||||
def test_text_sensor_device_class_set(generate_main):
|
def test_text_sensor_device_class_set(generate_main):
|
||||||
@@ -67,7 +57,7 @@ def test_text_sensor_device_class_set(generate_main):
|
|||||||
|
|
||||||
# Then: ts_2 has device_class: timestamp, ts_3 has device_class: date
|
# Then: ts_2 has device_class: timestamp, ts_3 has device_class: date
|
||||||
# so their packed values must be non-zero
|
# so their packed values must be non-zero
|
||||||
packed_ts_2 = _extract_packed_value(main_cpp, "ts_2")
|
packed_ts_2 = extract_packed_value(main_cpp, "ts_2")
|
||||||
assert packed_ts_2 != 0
|
assert packed_ts_2 != 0
|
||||||
packed_ts_3 = _extract_packed_value(main_cpp, "ts_3")
|
packed_ts_3 = extract_packed_value(main_cpp, "ts_3")
|
||||||
assert packed_ts_3 != 0
|
assert packed_ts_3 != 0
|
||||||
|
|||||||
Reference in New Issue
Block a user