mirror of
https://github.com/esphome/esphome.git
synced 2025-11-19 08:15:49 +00:00
update tests
This commit is contained in:
@@ -29,7 +29,7 @@ def test_binary_sensor_sets_mandatory_fields(generate_main):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Then
|
# Then
|
||||||
assert 'bs_1->set_name("test bs1");' in main_cpp
|
assert 'bs_1->set_name_and_object_id("test bs1", "test_bs1");' in main_cpp
|
||||||
assert "bs_1->set_pin(" in main_cpp
|
assert "bs_1->set_pin(" in main_cpp
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ def test_button_sets_mandatory_fields(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
|
# Then
|
||||||
assert 'wol_1->set_name("wol_test_1");' in main_cpp
|
assert 'wol_1->set_name_and_object_id("wol_test_1", "wol_test_1");' in main_cpp
|
||||||
assert "wol_2->set_macaddr(18, 52, 86, 120, 144, 171);" in main_cpp
|
assert "wol_2->set_macaddr(18, 52, 86, 120, 144, 171);" in main_cpp
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ def test_text_sets_mandatory_fields(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
|
# Then
|
||||||
assert 'it_1->set_name("test 1 text");' in main_cpp
|
assert 'it_1->set_name_and_object_id("test 1 text", "test_1_text");' in main_cpp
|
||||||
|
|
||||||
|
|
||||||
def test_text_config_value_internal_set(generate_main):
|
def test_text_config_value_internal_set(generate_main):
|
||||||
|
|||||||
@@ -25,9 +25,18 @@ def test_text_sensor_sets_mandatory_fields(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
|
# Then
|
||||||
assert 'ts_1->set_name("Template Text Sensor 1");' in main_cpp
|
assert (
|
||||||
assert 'ts_2->set_name("Template Text Sensor 2");' in main_cpp
|
'ts_1->set_name_and_object_id("Template Text Sensor 1", "template_text_sensor_1");'
|
||||||
assert 'ts_3->set_name("Template Text Sensor 3");' in main_cpp
|
in main_cpp
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
'ts_2->set_name_and_object_id("Template Text Sensor 2", "template_text_sensor_2");'
|
||||||
|
in main_cpp
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
'ts_3->set_name_and_object_id("Template Text Sensor 3", "template_text_sensor_3");'
|
||||||
|
in main_cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_text_sensor_config_value_internal_set(generate_main):
|
def test_text_sensor_config_value_internal_set(generate_main):
|
||||||
|
|||||||
@@ -27,8 +27,13 @@ from esphome.helpers import sanitize, snake_case
|
|||||||
|
|
||||||
from .common import load_config_from_fixture
|
from .common import load_config_from_fixture
|
||||||
|
|
||||||
# Pre-compiled regex pattern for extracting object IDs from expressions
|
# Pre-compiled regex patterns for extracting object IDs from expressions
|
||||||
|
# Matches both old format: .set_object_id("obj_id")
|
||||||
|
# and new format: .set_name_and_object_id("name", "obj_id")
|
||||||
OBJECT_ID_PATTERN = re.compile(r'\.set_object_id\(["\'](.*?)["\']\)')
|
OBJECT_ID_PATTERN = re.compile(r'\.set_object_id\(["\'](.*?)["\']\)')
|
||||||
|
COMBINED_PATTERN = re.compile(
|
||||||
|
r'\.set_name_and_object_id\(["\'].*?["\']\s*,\s*["\'](.*?)["\']\)'
|
||||||
|
)
|
||||||
|
|
||||||
FIXTURES_DIR = Path(__file__).parent.parent / "fixtures" / "core" / "entity_helpers"
|
FIXTURES_DIR = Path(__file__).parent.parent / "fixtures" / "core" / "entity_helpers"
|
||||||
|
|
||||||
@@ -273,8 +278,10 @@ def setup_test_environment() -> Generator[list[str], None, None]:
|
|||||||
def extract_object_id_from_expressions(expressions: list[str]) -> str | None:
|
def extract_object_id_from_expressions(expressions: list[str]) -> str | None:
|
||||||
"""Extract the object ID that was set from the generated expressions."""
|
"""Extract the object ID that was set from the generated expressions."""
|
||||||
for expr in expressions:
|
for expr in expressions:
|
||||||
# Look for set_object_id calls with regex to handle various formats
|
# First try new combined format: .set_name_and_object_id("name", "obj_id")
|
||||||
# Matches: var.set_object_id("temperature_2") or var.set_object_id('temperature_2')
|
if match := COMBINED_PATTERN.search(expr):
|
||||||
|
return match.group(1)
|
||||||
|
# Fall back to old format: .set_object_id("obj_id")
|
||||||
if match := OBJECT_ID_PATTERN.search(expr):
|
if match := OBJECT_ID_PATTERN.search(expr):
|
||||||
return match.group(1)
|
return match.group(1)
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user