mirror of
https://github.com/esphome/esphome.git
synced 2026-10-20 21:16:46 +00:00
38 lines
898 B
C++
38 lines
898 B
C++
#include <gtest/gtest.h>
|
|
|
|
#include <vector>
|
|
|
|
#include "esphome/components/switch/switch.h"
|
|
|
|
namespace esphome::switch_::testing {
|
|
|
|
static Switch *last_entity = nullptr;
|
|
static std::vector<bool> writes;
|
|
|
|
static void on_write(Switch *entity, bool state) {
|
|
last_entity = entity;
|
|
writes.push_back(state);
|
|
}
|
|
|
|
TEST(ApplySwitch, WriteRunsTheFunctionWithTheState) {
|
|
last_entity = nullptr;
|
|
writes.clear();
|
|
ApplySwitch<on_write> sw;
|
|
sw.turn_on();
|
|
sw.turn_off();
|
|
EXPECT_EQ(writes, (std::vector<bool>{true, false}));
|
|
EXPECT_EQ(last_entity, &sw);
|
|
}
|
|
|
|
TEST(ApplySwitch, InversionIsAppliedBeforeTheFunction) {
|
|
writes.clear();
|
|
ApplySwitch<on_write> sw;
|
|
sw.set_inverted(true);
|
|
sw.turn_on();
|
|
EXPECT_EQ(writes, (std::vector<bool>{false}));
|
|
}
|
|
|
|
TEST(ApplySwitch, HoldsNoMoreThanTheBase) { EXPECT_EQ(sizeof(ApplySwitch<on_write>), sizeof(Switch)); }
|
|
|
|
} // namespace esphome::switch_::testing
|