mirror of
https://github.com/esphome/esphome.git
synced 2025-09-02 11:22:24 +01:00
add-black (#1593)
* Add black Update pre commit Update pre commit add empty line * Format with black
This commit is contained in:
committed by
GitHub
parent
2b60b0f1fa
commit
69879920eb
@@ -9,18 +9,18 @@ from esphome import cpp_types as ct
|
||||
|
||||
|
||||
class TestExpressions:
|
||||
@pytest.mark.parametrize("target, expected", (
|
||||
(cg.RawExpression("foo && bar"), "foo && bar"),
|
||||
|
||||
(cg.AssignmentExpression(None, None, "foo", "bar", None), 'foo = "bar"'),
|
||||
(cg.AssignmentExpression(ct.float_, "*", "foo", 1, None), 'float *foo = 1'),
|
||||
(cg.AssignmentExpression(ct.float_, "", "foo", 1, None), 'float foo = 1'),
|
||||
|
||||
(cg.VariableDeclarationExpression(ct.int32, "*", "foo"), "int32_t *foo"),
|
||||
(cg.VariableDeclarationExpression(ct.int32, "", "foo"), "int32_t foo"),
|
||||
|
||||
(cg.ParameterExpression(ct.std_string, "foo"), "std::string foo"),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"target, expected",
|
||||
(
|
||||
(cg.RawExpression("foo && bar"), "foo && bar"),
|
||||
(cg.AssignmentExpression(None, None, "foo", "bar", None), 'foo = "bar"'),
|
||||
(cg.AssignmentExpression(ct.float_, "*", "foo", 1, None), "float *foo = 1"),
|
||||
(cg.AssignmentExpression(ct.float_, "", "foo", 1, None), "float foo = 1"),
|
||||
(cg.VariableDeclarationExpression(ct.int32, "*", "foo"), "int32_t *foo"),
|
||||
(cg.VariableDeclarationExpression(ct.int32, "", "foo"), "int32_t foo"),
|
||||
(cg.ParameterExpression(ct.std_string, "foo"), "std::string foo"),
|
||||
),
|
||||
)
|
||||
def test_str__simple(self, target: cg.Expression, expected: str):
|
||||
actual = str(target)
|
||||
|
||||
@@ -67,10 +67,7 @@ class TestTemplateArguments:
|
||||
|
||||
class TestCallExpression:
|
||||
def test_str__no_template_args(self):
|
||||
target = cg.CallExpression(
|
||||
cg.RawExpression("my_function"),
|
||||
1, "2", False
|
||||
)
|
||||
target = cg.CallExpression(cg.RawExpression("my_function"), 1, "2", False)
|
||||
|
||||
actual = str(target)
|
||||
|
||||
@@ -80,7 +77,9 @@ class TestCallExpression:
|
||||
target = cg.CallExpression(
|
||||
cg.RawExpression("my_function"),
|
||||
cg.TemplateArguments(int, float),
|
||||
1, "2", False
|
||||
1,
|
||||
"2",
|
||||
False,
|
||||
)
|
||||
|
||||
actual = str(target)
|
||||
@@ -100,36 +99,32 @@ class TestStructInitializer:
|
||||
|
||||
actual = str(target)
|
||||
|
||||
assert actual == 'foo::MyStruct{\n' \
|
||||
' .state = "on",\n' \
|
||||
' .min_length = 1,\n' \
|
||||
' .max_length = 5,\n' \
|
||||
'}'
|
||||
assert (
|
||||
actual == "foo::MyStruct{\n"
|
||||
' .state = "on",\n'
|
||||
" .min_length = 1,\n"
|
||||
" .max_length = 5,\n"
|
||||
"}"
|
||||
)
|
||||
|
||||
|
||||
class TestArrayInitializer:
|
||||
def test_str__empty(self):
|
||||
target = cg.ArrayInitializer(
|
||||
None, None
|
||||
)
|
||||
target = cg.ArrayInitializer(None, None)
|
||||
|
||||
actual = str(target)
|
||||
|
||||
assert actual == "{}"
|
||||
|
||||
def test_str__not_multiline(self):
|
||||
target = cg.ArrayInitializer(
|
||||
1, 2, 3, 4
|
||||
)
|
||||
target = cg.ArrayInitializer(1, 2, 3, 4)
|
||||
|
||||
actual = str(target)
|
||||
|
||||
assert actual == "{1, 2, 3, 4}"
|
||||
|
||||
def test_str__multiline(self):
|
||||
target = cg.ArrayInitializer(
|
||||
1, 2, 3, 4, multiline=True
|
||||
)
|
||||
target = cg.ArrayInitializer(1, 2, 3, 4, multiline=True)
|
||||
|
||||
actual = str(target)
|
||||
|
||||
@@ -169,7 +164,7 @@ class TestLambdaExpression:
|
||||
|
||||
def test_str__with_return(self):
|
||||
target = cg.LambdaExpression(
|
||||
("return (foo == 5) && (bar < 10));", ),
|
||||
("return (foo == 5) && (bar < 10));",),
|
||||
cg.ParameterListExpression((int, "foo"), (float, "bar")),
|
||||
"=",
|
||||
bool,
|
||||
@@ -185,27 +180,26 @@ class TestLambdaExpression:
|
||||
|
||||
|
||||
class TestLiterals:
|
||||
@pytest.mark.parametrize("target, expected", (
|
||||
(cg.StringLiteral("foo"), '"foo"'),
|
||||
|
||||
(cg.IntLiteral(0), "0"),
|
||||
(cg.IntLiteral(42), "42"),
|
||||
(cg.IntLiteral(4304967295), "4304967295ULL"),
|
||||
(cg.IntLiteral(2150483647), "2150483647UL"),
|
||||
(cg.IntLiteral(-2150083647), "-2150083647LL"),
|
||||
|
||||
(cg.BoolLiteral(True), "true"),
|
||||
(cg.BoolLiteral(False), "false"),
|
||||
|
||||
(cg.HexIntLiteral(0), "0x00"),
|
||||
(cg.HexIntLiteral(42), "0x2A"),
|
||||
(cg.HexIntLiteral(682), "0x2AA"),
|
||||
|
||||
(cg.FloatLiteral(0.0), "0.0f"),
|
||||
(cg.FloatLiteral(4.2), "4.2f"),
|
||||
(cg.FloatLiteral(1.23456789), "1.23456789f"),
|
||||
(cg.FloatLiteral(math.nan), "NAN"),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"target, expected",
|
||||
(
|
||||
(cg.StringLiteral("foo"), '"foo"'),
|
||||
(cg.IntLiteral(0), "0"),
|
||||
(cg.IntLiteral(42), "42"),
|
||||
(cg.IntLiteral(4304967295), "4304967295ULL"),
|
||||
(cg.IntLiteral(2150483647), "2150483647UL"),
|
||||
(cg.IntLiteral(-2150083647), "-2150083647LL"),
|
||||
(cg.BoolLiteral(True), "true"),
|
||||
(cg.BoolLiteral(False), "false"),
|
||||
(cg.HexIntLiteral(0), "0x00"),
|
||||
(cg.HexIntLiteral(42), "0x2A"),
|
||||
(cg.HexIntLiteral(682), "0x2AA"),
|
||||
(cg.FloatLiteral(0.0), "0.0f"),
|
||||
(cg.FloatLiteral(4.2), "4.2f"),
|
||||
(cg.FloatLiteral(1.23456789), "1.23456789f"),
|
||||
(cg.FloatLiteral(math.nan), "NAN"),
|
||||
),
|
||||
)
|
||||
def test_str__simple(self, target: cg.Literal, expected: str):
|
||||
actual = str(target)
|
||||
|
||||
@@ -216,7 +210,9 @@ FAKE_ENUM_VALUE = cg.EnumValue()
|
||||
FAKE_ENUM_VALUE.enum_value = "foo"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("obj, expected_type", (
|
||||
@pytest.mark.parametrize(
|
||||
"obj, expected_type",
|
||||
(
|
||||
(cg.RawExpression("foo"), cg.RawExpression),
|
||||
(FAKE_ENUM_VALUE, cg.StringLiteral),
|
||||
(True, cg.BoolLiteral),
|
||||
@@ -230,49 +226,59 @@ FAKE_ENUM_VALUE.enum_value = "foo"
|
||||
(cg.TimePeriodMinutes(minutes=42), cg.IntLiteral),
|
||||
((1, 2, 3), cg.ArrayInitializer),
|
||||
([1, 2, 3], cg.ArrayInitializer),
|
||||
))
|
||||
),
|
||||
)
|
||||
def test_safe_exp__allowed_values(obj, expected_type):
|
||||
actual = cg.safe_exp(obj)
|
||||
|
||||
assert isinstance(actual, expected_type)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("obj, expected_type", (
|
||||
@pytest.mark.parametrize(
|
||||
"obj, expected_type",
|
||||
(
|
||||
(bool, ct.bool_),
|
||||
(int, ct.int32),
|
||||
(float, ct.float_),
|
||||
))
|
||||
),
|
||||
)
|
||||
def test_safe_exp__allowed_types(obj, expected_type):
|
||||
actual = cg.safe_exp(obj)
|
||||
|
||||
assert actual is expected_type
|
||||
|
||||
|
||||
@pytest.mark.parametrize("obj, expected_error", (
|
||||
@pytest.mark.parametrize(
|
||||
"obj, expected_error",
|
||||
(
|
||||
(cg.ID("foo"), "Object foo is an ID."),
|
||||
((x for x in "foo"), r"Object <.*> is a coroutine."),
|
||||
(None, "Object is not an expression"),
|
||||
))
|
||||
),
|
||||
)
|
||||
def test_safe_exp__invalid_values(obj, expected_error):
|
||||
with pytest.raises(ValueError, match=expected_error):
|
||||
cg.safe_exp(obj)
|
||||
|
||||
|
||||
class TestStatements:
|
||||
@pytest.mark.parametrize("target, expected", (
|
||||
(cg.RawStatement("foo && bar"), "foo && bar"),
|
||||
|
||||
(cg.ExpressionStatement("foo"), '"foo";'),
|
||||
(cg.ExpressionStatement(42), '42;'),
|
||||
|
||||
(cg.LineComment("The point of foo is..."), "// The point of foo is..."),
|
||||
(cg.LineComment("Help help\nI'm being repressed"), "// Help help\n// I'm being repressed"),
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"target, expected",
|
||||
(
|
||||
cg.ProgmemAssignmentExpression(ct.uint16, "foo", "bar", None),
|
||||
'static const uint16_t foo[] PROGMEM = "bar"'
|
||||
)
|
||||
))
|
||||
(cg.RawStatement("foo && bar"), "foo && bar"),
|
||||
(cg.ExpressionStatement("foo"), '"foo";'),
|
||||
(cg.ExpressionStatement(42), "42;"),
|
||||
(cg.LineComment("The point of foo is..."), "// The point of foo is..."),
|
||||
(
|
||||
cg.LineComment("Help help\nI'm being repressed"),
|
||||
"// Help help\n// I'm being repressed",
|
||||
),
|
||||
(
|
||||
cg.ProgmemAssignmentExpression(ct.uint16, "foo", "bar", None),
|
||||
'static const uint16_t foo[] PROGMEM = "bar"',
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_str__simple(self, target: cg.Statement, expected: str):
|
||||
actual = str(target)
|
||||
|
||||
|
Reference in New Issue
Block a user