add tests for all components
This commit is contained in:
parent
803d969de6
commit
9e7f2dc065
210
test/native/test_ha/components.cpp
Normal file
210
test/native/test_ha/components.cpp
Normal file
@ -0,0 +1,210 @@
|
||||
#include <unity.h>
|
||||
#include <unordered_map>
|
||||
|
||||
#define MAIN_DEVICE_ID "test"
|
||||
|
||||
#include "ha.h"
|
||||
|
||||
using namespace Ha;
|
||||
|
||||
void setUp(void) {
|
||||
// set stuff up here
|
||||
}
|
||||
|
||||
void tearDown(void) {
|
||||
// clean stuff up here
|
||||
}
|
||||
|
||||
void testDevice(void) {
|
||||
auto d = DeviceConfig::create("1").withName("name").withModel("model").withArea("area").withManufacturer("man");
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
d.buildConfig(doc);
|
||||
|
||||
JsonObject dev = doc["device"];
|
||||
TEST_ASSERT_TRUE(doc.containsKey("device"));
|
||||
TEST_ASSERT_EQUAL_STRING("name", dev["name"]);
|
||||
TEST_ASSERT_EQUAL_STRING("model", dev["model"]);
|
||||
TEST_ASSERT_EQUAL_STRING("area", dev["suggested_area"]);
|
||||
TEST_ASSERT_EQUAL_STRING("man", dev["manufacturer"]);
|
||||
TEST_ASSERT_TRUE(dev.containsKey("identifiers"));
|
||||
TEST_ASSERT_EQUAL_STRING("1", dev["identifiers"][0]);
|
||||
}
|
||||
|
||||
void testButton(void) {
|
||||
Button b("a_name", "id");
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
b.buildConfig(doc);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING(MAIN_DEVICE_ID"_id", doc["unique_id"]);
|
||||
TEST_ASSERT_EQUAL_STRING("homeassistant/button/" MAIN_DEVICE_ID "/id/set", doc["command_topic"]);
|
||||
TEST_ASSERT_FALSE(doc["retain"]);
|
||||
TEST_ASSERT_EQUAL_STRING("a_name", doc["name"]);
|
||||
TEST_ASSERT_EQUAL_STRING(NULL, doc["device_class"]);
|
||||
TEST_ASSERT_EQUAL_STRING(NULL, doc["entity_category"]);
|
||||
TEST_ASSERT_NOT_NULL(Command::mapCommandTopics[doc["command_topic"]]);
|
||||
}
|
||||
|
||||
void testSensor(void) {
|
||||
Sensor s("a_name", "id");
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
s.buildConfig(doc);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING(MAIN_DEVICE_ID"_id", doc["unique_id"]);
|
||||
TEST_ASSERT_EQUAL_STRING("homeassistant/sensor/" MAIN_DEVICE_ID "/id/state", doc["state_topic"]);
|
||||
TEST_ASSERT_EQUAL_STRING("a_name", doc["name"]);
|
||||
TEST_ASSERT_EQUAL_STRING(NULL, doc["device_class"]);
|
||||
TEST_ASSERT_EQUAL_STRING(NULL, doc["entity_category"]);
|
||||
TEST_ASSERT_EQUAL_STRING(NULL, doc["unit_of_measurement"]);
|
||||
TEST_ASSERT_EQUAL_STRING(NULL, doc["value_template"]);
|
||||
TEST_ASSERT_EQUAL_INT(0, doc["suggested_display_precision"]);
|
||||
TEST_ASSERT_NOT_NULL(Sensor::mapSensors["id"]);
|
||||
}
|
||||
|
||||
void testNumericSensor1(void) {
|
||||
Sensor s("a_name", "id");
|
||||
s.deviceClass = "class";
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
s.buildConfig(doc);
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(2, doc["suggested_display_precision"]);
|
||||
}
|
||||
|
||||
void testNumericSensor2(void) {
|
||||
Sensor s("a_name", "id");
|
||||
s.unitMeasure = "%";
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
s.buildConfig(doc);
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(2, doc["suggested_display_precision"]);
|
||||
}
|
||||
|
||||
void testSwitch(void) {
|
||||
Switch s("a_name", "id");
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
s.buildConfig(doc);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING(MAIN_DEVICE_ID"_id", doc["unique_id"]);
|
||||
TEST_ASSERT_EQUAL_STRING("homeassistant/switch/" MAIN_DEVICE_ID "/id/set", doc["command_topic"]);
|
||||
TEST_ASSERT_FALSE(doc["retain"]);
|
||||
TEST_ASSERT_EQUAL_STRING("a_name", doc["name"]);
|
||||
TEST_ASSERT_EQUAL_STRING(NULL, doc["device_class"]);
|
||||
TEST_ASSERT_EQUAL_STRING(NULL, doc["entity_category"]);
|
||||
TEST_ASSERT_NOT_NULL(Command::mapCommandTopics[doc["command_topic"]]);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING(NULL, doc["state_topic"]);
|
||||
}
|
||||
|
||||
void testSwitchWithState(void) {
|
||||
Switch s("a_name", "id");
|
||||
s.withStateTopic();
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
s.buildConfig(doc);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("homeassistant/switch/" MAIN_DEVICE_ID "/id/state", doc["state_topic"]);
|
||||
}
|
||||
|
||||
void testNumber(void) {
|
||||
Number n("a_name", "id", nullptr);
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
n.buildConfig(doc);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING(MAIN_DEVICE_ID"_id", doc["unique_id"]);
|
||||
TEST_ASSERT_EQUAL_INT16(1, doc["min"]);
|
||||
TEST_ASSERT_EQUAL_INT16(100, doc["max"]);
|
||||
TEST_ASSERT_EQUAL_INT16(1, doc["step"]);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("homeassistant/number/" MAIN_DEVICE_ID "/id/set", doc["command_topic"]);
|
||||
TEST_ASSERT_FALSE(doc["retain"]);
|
||||
TEST_ASSERT_EQUAL_STRING("a_name", doc["name"]);
|
||||
TEST_ASSERT_EQUAL_STRING(NULL, doc["device_class"]);
|
||||
TEST_ASSERT_EQUAL_STRING(NULL, doc["entity_category"]);
|
||||
TEST_ASSERT_NOT_NULL(Command::mapCommandTopics[doc["command_topic"]]);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING(NULL, doc["state_topic"]);
|
||||
}
|
||||
|
||||
void testTemperatureSensor(void) {
|
||||
TemperatureSensor s("id");
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
s.buildConfig(doc);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("Temperature", doc["name"]);
|
||||
TEST_ASSERT_EQUAL_STRING("temperature", doc["device_class"]);
|
||||
TEST_ASSERT_EQUAL_STRING("°C", doc["unit_of_measurement"]);
|
||||
TEST_ASSERT_EQUAL_INT(2, doc["suggested_display_precision"]);
|
||||
}
|
||||
|
||||
void testHumiditySensor(void) {
|
||||
HumiditySensor s("id");
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
s.buildConfig(doc);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("Humidity", doc["name"]);
|
||||
TEST_ASSERT_EQUAL_STRING("humidity", doc["device_class"]);
|
||||
TEST_ASSERT_EQUAL_STRING("%", doc["unit_of_measurement"]);
|
||||
TEST_ASSERT_EQUAL_INT(2, doc["suggested_display_precision"]);
|
||||
}
|
||||
|
||||
void testPressureSensor(void) {
|
||||
PressureSensor s("id");
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
s.buildConfig(doc);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("Pressure", doc["name"]);
|
||||
TEST_ASSERT_EQUAL_STRING("pressure", doc["device_class"]);
|
||||
TEST_ASSERT_EQUAL_STRING("hPa", doc["unit_of_measurement"]);
|
||||
TEST_ASSERT_EQUAL_INT(2, doc["suggested_display_precision"]);
|
||||
}
|
||||
|
||||
void testVoltageSensor(void) {
|
||||
VoltageSensor s("id", "a_name", nullptr);
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
s.buildConfig(doc);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("a_name", doc["name"]);
|
||||
TEST_ASSERT_EQUAL_STRING("voltage", doc["device_class"]);
|
||||
TEST_ASSERT_EQUAL_STRING("V", doc["unit_of_measurement"]);
|
||||
TEST_ASSERT_EQUAL_INT(2, doc["suggested_display_precision"]);
|
||||
}
|
||||
|
||||
void testBatterySensor(void) {
|
||||
BatterySensor s("id", "a_name", nullptr);
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
s.buildConfig(doc);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("a_name", doc["name"]);
|
||||
TEST_ASSERT_EQUAL_STRING("battery", doc["device_class"]);
|
||||
TEST_ASSERT_EQUAL_STRING("%", doc["unit_of_measurement"]);
|
||||
TEST_ASSERT_EQUAL_INT(2, doc["suggested_display_precision"]);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(testDevice);
|
||||
RUN_TEST(testButton);
|
||||
RUN_TEST(testSensor);
|
||||
RUN_TEST(testTemperatureSensor);
|
||||
RUN_TEST(testHumiditySensor);
|
||||
RUN_TEST(testPressureSensor);
|
||||
RUN_TEST(testVoltageSensor);
|
||||
RUN_TEST(testBatterySensor);
|
||||
RUN_TEST(testNumericSensor1);
|
||||
RUN_TEST(testNumericSensor2);
|
||||
RUN_TEST(testSwitch);
|
||||
RUN_TEST(testSwitchWithState);
|
||||
RUN_TEST(testNumber);
|
||||
return UNITY_END();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user