mirror of
https://github.com/esphome/esphome.git
synced 2026-09-24 21:44:04 +00:00
[api] Drop the unused base decode(), group the vtable asserts, and test the decode emitters
A generated decodable message that lost its decode() would now fail to compile instead of silently keeping its defaults. The asserts cover exactly the classes that derive from ProtoDecodableMessage, under one ifdef per run instead of one per line, and the generator tests pin the inline decode() wrapper, its absence on fixed vector messages, and the this-> free static body.
This commit is contained in:
@@ -285,8 +285,9 @@ def test_a_fixed64_field_fails_at_generation_time() -> None:
|
||||
build_message_type(desc, {}, {"Wide": SOURCE_CLIENT})
|
||||
|
||||
|
||||
def test_message_gets_a_single_decode_field_override() -> None:
|
||||
"""All wire types of a decoded message land in one decode_field() switch."""
|
||||
def test_message_decodes_through_one_static_decode_field() -> None:
|
||||
"""All wire types of a decoded message land in one static decode_field() switch that the
|
||||
inline decode() hands to the shared loop."""
|
||||
desc = descriptor_pb2.DescriptorProto(name="Mixed")
|
||||
desc.field.add(name="name", number=1, type=STRING)
|
||||
desc.field.add(name="count", number=2, type=UINT32)
|
||||
@@ -308,3 +309,34 @@ def test_message_gets_a_single_decode_field_override() -> None:
|
||||
(3, "WIRE_TYPE_FIXED32"),
|
||||
):
|
||||
assert f"case proto_tag({number}, {wire_type}):" in cpp, cpp
|
||||
# The static body works on the cast message, never on this
|
||||
assert "auto &msg = *static_cast<Mixed *>(self);" in cpp
|
||||
assert "this->" not in cpp
|
||||
assert (
|
||||
header.count(
|
||||
"ProtoDecodableMessage::decode_fields(this, buffer, length, &decode_field);"
|
||||
)
|
||||
== 1
|
||||
)
|
||||
|
||||
|
||||
def test_fixed_vector_message_keeps_its_own_decode() -> None:
|
||||
"""A message that sizes a FixedVector first decodes through its own decode(), not the inline one."""
|
||||
desc = descriptor_pb2.DescriptorProto(name="Sized")
|
||||
field = desc.field.add(name="values", number=1, type=UINT32)
|
||||
field.label = descriptor_pb2.FieldDescriptorProto.LABEL_REPEATED
|
||||
field.options.Extensions[pb.fixed_vector] = True
|
||||
header, cpp, _ = build_message_type(desc, {}, {"Sized": SOURCE_CLIENT})
|
||||
assert (
|
||||
"ProtoDecodableMessage::decode_fields(this, buffer, length, &decode_field);"
|
||||
not in header
|
||||
)
|
||||
assert header.count("void decode(const uint8_t *buffer, size_t length);") == 1
|
||||
assert "void Sized::decode(const uint8_t *buffer, size_t length) {" in cpp
|
||||
assert "ProtoDecodableMessage::count_repeated_field(buffer, length, 1)" in cpp
|
||||
assert (
|
||||
cpp.count(
|
||||
"ProtoDecodableMessage::decode_fields(this, buffer, length, &decode_field);"
|
||||
)
|
||||
== 1
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user