[esp32_ble_server] Cover string, typed number, repeated and trigger argument set_value payloads in the component test (#20308)

This commit is contained in:
J. Nick Koston
2026-10-08 11:32:50 -04:00
committed by GitHub
parent 79813a1eac
commit f224cb37d6
2 changed files with 74 additions and 0 deletions
@@ -0,0 +1,34 @@
#ifdef USE_HOST
#include <gtest/gtest.h>
#include <cstdint>
#include <vector>
#include "esphome/components/bytebuffer/bytebuffer.h"
namespace esphome::bytebuffer {
// The expressions esp32_ble_server emits for typed constant values, and the bytes they produce.
// Doubles are written as float literals, so they carry float precision.
TEST(ByteBufferWrapValues, MatchesGeneratedConstants) {
EXPECT_EQ(ByteBuffer::wrap(uint8_t(18), LITTLE).get_data(), (std::vector<uint8_t>{0x12}));
EXPECT_EQ(ByteBuffer::wrap(uint16_t(18), LITTLE).get_data(), (std::vector<uint8_t>{0x12, 0x00}));
EXPECT_EQ(ByteBuffer::wrap(uint16_t(4660), BIG).get_data(), (std::vector<uint8_t>{0x12, 0x34}));
EXPECT_EQ(ByteBuffer::wrap(uint32_t(305419896), LITTLE).get_data(), (std::vector<uint8_t>{0x78, 0x56, 0x34, 0x12}));
EXPECT_EQ(ByteBuffer::wrap(uint64_t(1311768467294899695ULL), BIG).get_data(),
(std::vector<uint8_t>{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}));
EXPECT_EQ(ByteBuffer::wrap(int8_t(-5), LITTLE).get_data(), (std::vector<uint8_t>{0xfb}));
EXPECT_EQ(ByteBuffer::wrap(int16_t(-2), BIG).get_data(), (std::vector<uint8_t>{0xff, 0xfe}));
EXPECT_EQ(ByteBuffer::wrap(int32_t(-2), LITTLE).get_data(), (std::vector<uint8_t>{0xfe, 0xff, 0xff, 0xff}));
EXPECT_EQ(ByteBuffer::wrap(int64_t(-2), LITTLE).get_data(),
(std::vector<uint8_t>{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}));
EXPECT_EQ(ByteBuffer::wrap(float(123.1f), BIG).get_data(), (std::vector<uint8_t>{0x42, 0xf6, 0x33, 0x33}));
EXPECT_EQ(ByteBuffer::wrap(float(0.1f), LITTLE).get_data(), (std::vector<uint8_t>{0xcd, 0xcc, 0xcc, 0x3d}));
EXPECT_EQ(ByteBuffer::wrap(double(0.1f), LITTLE).get_data(),
(std::vector<uint8_t>{0x00, 0x00, 0x00, 0xa0, 0x99, 0x99, 0xb9, 0x3f}));
EXPECT_EQ(ByteBuffer::wrap(double(2.5f), BIG).get_data(),
(std::vector<uint8_t>{0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}));
}
} // namespace esphome::bytebuffer
#endif
@@ -77,6 +77,46 @@ esp32_ble_server:
id: test_change_descriptor
value:
data: [0x01, 0x02, 0x03]
# Identical constants, strings and typed numbers
- ble_server.characteristic.set_value:
id: test_change_characteristic
value:
data: [0xfc, 0xef, 0xfe, 0x86]
- ble_server.characteristic.set_value:
id: test_change_characteristic
value: "Changed"
- ble_server.characteristic.set_value:
id: test_change_characteristic
value:
data: 4660
type: uint16_t
endianness: BIG
- ble_server.characteristic.set_value:
id: test_change_characteristic
value:
data: -2
type: int32_t
- ble_server.characteristic.set_value:
id: test_change_characteristic
value:
data: 0.1
type: double
- ble_server.descriptor.set_value:
id: test_change_descriptor
value:
data: 123.1
type: float
endianness: BIG
- ble_server.descriptor.set_value:
id: test_change_descriptor
value: "Changed"
# Lambdas that read the trigger argument
- ble_server.characteristic.set_value:
id: test_change_characteristic
value: !lambda return x;
- ble_server.descriptor.set_value:
id: test_change_descriptor
value: !lambda return x;
# Regression test for #17142: the set_value action used from a trigger that passes
# its argument by reference (climate on_control supplies ClimateCall&) previously