1
0
mirror of https://github.com/esphome/esphome.git synced 2026-02-08 00:31:58 +00:00

make sure new stringref functions work

This commit is contained in:
J. Nick Koston
2026-01-17 08:35:46 -10:00
parent 65cdb97f06
commit 18c3dd8af7
2 changed files with 42 additions and 0 deletions

View File

@@ -37,3 +37,21 @@ select:
# Test 4: Use index parameter (variable name is 'i')
- lambda: |-
ESP_LOGI("test", "Select index: %d", (int)i);
# Test 5: StringRef.length() method
- lambda: |-
ESP_LOGI("test", "Length: %d", (int)x.length());
# Test 6: StringRef.find() method with substring
- lambda: |-
if (x.find("Option") != StringRef::npos) {
ESP_LOGI("test", "Found 'Option' in value");
}
# Test 7: StringRef.find() method with character
- lambda: |-
size_t space_pos = x.find(' ');
if (space_pos != StringRef::npos) {
ESP_LOGI("test", "Space at position: %d", (int)space_pos);
}
# Test 8: StringRef.substr() method
- lambda: |-
std::string prefix = x.substr(0, 6);
ESP_LOGI("test", "Substr prefix: %s", prefix.c_str());

View File

@@ -24,12 +24,20 @@ async def test_select_stringref_trigger(
concatenated_future = loop.create_future()
comparison_future = loop.create_future()
index_logged_future = loop.create_future()
length_future = loop.create_future()
find_substr_future = loop.create_future()
find_char_future = loop.create_future()
substr_future = loop.create_future()
# Patterns to match in logs
value_pattern = re.compile(r"Select value: Option B")
concatenated_pattern = re.compile(r"Concatenated: Option B selected")
comparison_pattern = re.compile(r"Option B was selected")
index_pattern = re.compile(r"Select index: 1")
length_pattern = re.compile(r"Length: 8") # "Option B" is 8 chars
find_substr_pattern = re.compile(r"Found 'Option' in value")
find_char_pattern = re.compile(r"Space at position: 6") # space at index 6
substr_pattern = re.compile(r"Substr prefix: Option")
def check_output(line: str) -> None:
"""Check log output for expected messages."""
@@ -41,6 +49,14 @@ async def test_select_stringref_trigger(
comparison_future.set_result(True)
if not index_logged_future.done() and index_pattern.search(line):
index_logged_future.set_result(True)
if not length_future.done() and length_pattern.search(line):
length_future.set_result(True)
if not find_substr_future.done() and find_substr_pattern.search(line):
find_substr_future.set_result(True)
if not find_char_future.done() and find_char_pattern.search(line):
find_char_future.set_result(True)
if not substr_future.done() and substr_pattern.search(line):
substr_future.set_result(True)
async with (
run_compiled(yaml_config, line_callback=check_output),
@@ -71,6 +87,10 @@ async def test_select_stringref_trigger(
concatenated_future,
comparison_future,
index_logged_future,
length_future,
find_substr_future,
find_char_future,
substr_future,
),
timeout=5.0,
)
@@ -80,5 +100,9 @@ async def test_select_stringref_trigger(
"concatenated": concatenated_future.done(),
"comparison": comparison_future.done(),
"index_logged": index_logged_future.done(),
"length": length_future.done(),
"find_substr": find_substr_future.done(),
"find_char": find_char_future.done(),
"substr": substr_future.done(),
}
pytest.fail(f"StringRef operations failed - received: {results}")