[packet_interface] Use a local to_vector() in tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
clydebarrow
2026-10-20 08:41:59 +11:00
co-authored by Claude Opus 5.5
parent 13ec5f27e9
commit 19c415f253
2 changed files with 20 additions and 6 deletions
@@ -9,6 +9,13 @@
namespace esphome::packet_interface::addressing_testing {
// Copies a packet into a vector, for comparing in tests
static std::vector<uint8_t> to_vector(const esphome::packet_interface::PacketBuffer &data) {
std::vector<uint8_t> result(data.size());
data.copy_to(result.data(), result.size());
return result;
}
using Packet = std::vector<uint8_t>;
static constexpr uint16_t BROADCAST = BasicAddressing::BROADCAST_ADDRESS;
@@ -18,7 +25,7 @@ class MockLink : public PacketInterface {
public:
MockLink() { this->set_max_packet_size(64); }
bool send_to_interface(const PacketBuffer &data, const PacketMetaData &) override {
this->sent.push_back(data.to_vector());
this->sent.push_back(to_vector(data));
return true;
}
void inject(const Packet &packet) { this->on_receive_from_interface_(PacketBuffer(packet), {}); }
@@ -31,7 +38,7 @@ struct Port {
Port(BasicAddressing *addressing, uint16_t destination, bool only_port) {
this->port = std::make_unique<AddressingPort>(addressing, destination, only_port);
this->port->add_packet_interface_listener([this](const PacketBuffer &data, const PacketMetaData &meta_data) {
this->received.push_back(data.to_vector());
this->received.push_back(to_vector(data));
this->sources.push_back(meta_data.source_address);
});
}
@@ -165,7 +172,7 @@ class AddressingReliabilityTest : public ::testing::Test {
struct ReliablePort {
explicit ReliablePort(AddressingPort *port) : hdlc(port) {
this->hdlc.add_packet_interface_listener(
[this](const PacketBuffer &data, const PacketMetaData &) { this->received.push_back(data.to_vector()); });
[this](const PacketBuffer &data, const PacketMetaData &) { this->received.push_back(to_vector(data)); });
this->hdlc.setup();
}
void send(const Packet &data) { this->hdlc.send_to_interface(PacketBuffer(data), {}); }
@@ -8,6 +8,13 @@
namespace esphome::packet_interface::fragmentation_testing {
// Copies a packet into a vector, for comparing in tests
static std::vector<uint8_t> to_vector(const esphome::packet_interface::PacketBuffer &data) {
std::vector<uint8_t> result(data.size());
data.copy_to(result.data(), result.size());
return result;
}
using Packet = std::vector<uint8_t>;
// A link layer that records what is sent, with a limited send capacity
@@ -15,7 +22,7 @@ class MockLink : public PacketInterface {
public:
MockLink() { this->set_max_packet_size(12); }
bool send_to_interface(const PacketBuffer &data, const PacketMetaData &) override {
this->sent.push_back(data.to_vector());
this->sent.push_back(to_vector(data));
return true;
}
size_t get_send_capacity() const override { return this->capacity; }
@@ -32,7 +39,7 @@ class MockLink : public PacketInterface {
struct Peer {
explicit Peer(PacketInterface *lower) : fragmentation(lower, 100) {
this->fragmentation.add_packet_interface_listener(
[this](const PacketBuffer &data, const PacketMetaData &) { this->received.push_back(data.to_vector()); });
[this](const PacketBuffer &data, const PacketMetaData &) { this->received.push_back(to_vector(data)); });
this->fragmentation.setup();
}
bool send(const Packet &data) { return this->fragmentation.send_to_interface(PacketBuffer(data), {}); }
@@ -183,7 +190,7 @@ TEST_F(BasicFragmentationTest, OversizedSingleFragmentDropped) {
BasicFragmentation fragmentation(&this->link_a, 4);
std::vector<Packet> received;
fragmentation.add_packet_interface_listener(
[&received](const PacketBuffer &data, const PacketMetaData &) { received.push_back(data.to_vector()); });
[&received](const PacketBuffer &data, const PacketMetaData &) { received.push_back(to_vector(data)); });
fragmentation.setup();
this->link_a.inject({0x01, 0x80, 1, 2, 3, 4, 5});
EXPECT_TRUE(received.empty());