mirror of
https://github.com/esphome/esphome.git
synced 2025-10-31 23:21:54 +00:00
over engineered
This commit is contained in:
@@ -198,10 +198,9 @@ class LambdaExpression(Expression):
|
|||||||
self.return_type = safe_exp(return_type) if return_type is not None else None
|
self.return_type = safe_exp(return_type) if return_type is not None else None
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
# Unary + converts stateless lambda to function pointer
|
# Stateless lambdas (empty capture) implicitly convert to function pointers
|
||||||
# This allows implicit conversion to void (*)() or bool (*)()
|
# when assigned to function pointer types - no unary + needed
|
||||||
prefix = "+" if self.capture == "" else ""
|
cpp = f"[{self.capture}]({self.parameters})"
|
||||||
cpp = f"{prefix}[{self.capture}]({self.parameters})"
|
|
||||||
if self.return_type is not None:
|
if self.return_type is not None:
|
||||||
cpp += f" -> {self.return_type}"
|
cpp += f" -> {self.return_type}"
|
||||||
cpp += " {\n"
|
cpp += " {\n"
|
||||||
|
|||||||
@@ -66,5 +66,5 @@ def test_text_config_lamda_is_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
|
# Then
|
||||||
assert "it_4->set_template(+[]() -> esphome::optional<std::string> {" in main_cpp
|
assert "it_4->set_template([]() -> esphome::optional<std::string> {" in main_cpp
|
||||||
assert 'return std::string{"Hello"};' in main_cpp
|
assert 'return std::string{"Hello"};' in main_cpp
|
||||||
|
|||||||
@@ -174,7 +174,7 @@ class TestLambdaExpression:
|
|||||||
)
|
)
|
||||||
|
|
||||||
def test_str__stateless_no_return(self):
|
def test_str__stateless_no_return(self):
|
||||||
"""Test stateless lambda (empty capture) gets unary + prefix"""
|
"""Test stateless lambda (empty capture) generates correctly"""
|
||||||
target = cg.LambdaExpression(
|
target = cg.LambdaExpression(
|
||||||
('ESP_LOGD("main", "Test message");',),
|
('ESP_LOGD("main", "Test message");',),
|
||||||
(), # No parameters
|
(), # No parameters
|
||||||
@@ -183,10 +183,10 @@ class TestLambdaExpression:
|
|||||||
|
|
||||||
actual = str(target)
|
actual = str(target)
|
||||||
|
|
||||||
assert actual == ('+[]() {\n ESP_LOGD("main", "Test message");\n}')
|
assert actual == ('[]() {\n ESP_LOGD("main", "Test message");\n}')
|
||||||
|
|
||||||
def test_str__stateless_with_return(self):
|
def test_str__stateless_with_return(self):
|
||||||
"""Test stateless lambda with return type gets unary + prefix"""
|
"""Test stateless lambda with return type generates correctly"""
|
||||||
target = cg.LambdaExpression(
|
target = cg.LambdaExpression(
|
||||||
("return global_value > 0;",),
|
("return global_value > 0;",),
|
||||||
(), # No parameters
|
(), # No parameters
|
||||||
@@ -196,10 +196,10 @@ class TestLambdaExpression:
|
|||||||
|
|
||||||
actual = str(target)
|
actual = str(target)
|
||||||
|
|
||||||
assert actual == ("+[]() -> bool {\n return global_value > 0;\n}")
|
assert actual == ("[]() -> bool {\n return global_value > 0;\n}")
|
||||||
|
|
||||||
def test_str__stateless_with_params(self):
|
def test_str__stateless_with_params(self):
|
||||||
"""Test stateless lambda with parameters gets unary + prefix"""
|
"""Test stateless lambda with parameters generates correctly"""
|
||||||
target = cg.LambdaExpression(
|
target = cg.LambdaExpression(
|
||||||
("return foo + bar;",),
|
("return foo + bar;",),
|
||||||
((int, "foo"), (float, "bar")),
|
((int, "foo"), (float, "bar")),
|
||||||
@@ -210,11 +210,11 @@ class TestLambdaExpression:
|
|||||||
actual = str(target)
|
actual = str(target)
|
||||||
|
|
||||||
assert actual == (
|
assert actual == (
|
||||||
"+[](int32_t foo, float bar) -> float {\n return foo + bar;\n}"
|
"[](int32_t foo, float bar) -> float {\n return foo + bar;\n}"
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_str__with_capture_no_prefix(self):
|
def test_str__with_capture(self):
|
||||||
"""Test lambda with capture (not stateless) does NOT get + prefix"""
|
"""Test lambda with capture generates correctly"""
|
||||||
target = cg.LambdaExpression(
|
target = cg.LambdaExpression(
|
||||||
("return captured_var + x;",),
|
("return captured_var + x;",),
|
||||||
((int, "x"),),
|
((int, "x"),),
|
||||||
@@ -224,11 +224,9 @@ class TestLambdaExpression:
|
|||||||
|
|
||||||
actual = str(target)
|
actual = str(target)
|
||||||
|
|
||||||
# Should NOT have + prefix
|
|
||||||
assert actual == (
|
assert actual == (
|
||||||
"[captured_var](int32_t x) -> int32_t {\n return captured_var + x;\n}"
|
"[captured_var](int32_t x) -> int32_t {\n return captured_var + x;\n}"
|
||||||
)
|
)
|
||||||
assert not actual.startswith("+")
|
|
||||||
|
|
||||||
|
|
||||||
class TestLiterals:
|
class TestLiterals:
|
||||||
|
|||||||
Reference in New Issue
Block a user