105 lines
2.9 KiB
C++
105 lines
2.9 KiB
C++
#include <unity.h>
|
|
#include <Arduino.h>
|
|
#include "Configurator.h"
|
|
|
|
void setUp()
|
|
{
|
|
}
|
|
|
|
void tearDown()
|
|
{
|
|
}
|
|
|
|
std::string singleBlindJson = R"({"shades": [{"radioId": "ABCD","mqttId": "test_shade","friendly_name": "Test Shade"}]})";
|
|
std::string doubleBlindJson = R"({"shades": [{"radioId": "ABCD","mqttId": "test_shade","friendly_name": "Test Shade"}, {"radioId": "1234","mqttId": "test_shade2","friendly_name": "Test Shade 2"}]})";
|
|
std::string invalidRadioIdBlindJson = R"({"shades": [{"radioId": "XYZ9","mqttId": "test_shade","friendly_name": "Test Shade"}, {"mqttId": "test_shade","friendly_name": "Test Shade"}, {"radioId": "","mqttId": "test_shade","friendly_name": "Test Shade"}, {"radioId": "ABCDE","mqttId": "test_shade","friendly_name": "Test Shade"}]})";
|
|
std::string invalidMqttIdBlindJson = R"({"shades": [{"radioId": "ABCD","mqttId": "","friendly_name": "Test Shade"}, {"radioId": "ABCD","friendly_name": "Test Shade"}]})";
|
|
|
|
void test_shade_is_configured_with_id_and_friendly_name()
|
|
{
|
|
int callbackInvokedCount = 0;
|
|
|
|
Configurator configurator = Configurator();
|
|
configurator.addShadeConfiguredCallback([&] (Shade shade) {
|
|
callbackInvokedCount++;
|
|
|
|
TEST_ASSERT_EQUAL_STRING("Test Shade", shade.friendlyName.c_str());
|
|
TEST_ASSERT_EQUAL_HEX16(0xABCD, shade.ID);
|
|
TEST_ASSERT_EQUAL_STRING("test_shade", shade.key.c_str());
|
|
});
|
|
|
|
configurator.processJson(singleBlindJson);
|
|
|
|
TEST_ASSERT_EQUAL_INT(1, callbackInvokedCount);
|
|
}
|
|
|
|
void test_multiple_shades_are_configured()
|
|
{
|
|
int callbackInvokedCount = 0;
|
|
|
|
Configurator configurator = Configurator();
|
|
configurator.addShadeConfiguredCallback([&] (Shade shade) {
|
|
callbackInvokedCount++;
|
|
});
|
|
|
|
configurator.processJson(doubleBlindJson);
|
|
|
|
TEST_ASSERT_EQUAL_INT(2, callbackInvokedCount);
|
|
}
|
|
|
|
void test_invalid_radio_id_is_ignored()
|
|
{
|
|
int callbackInvokedCount = 0;
|
|
|
|
Configurator configurator = Configurator();
|
|
configurator.addShadeConfiguredCallback([&] (Shade shade) {
|
|
callbackInvokedCount++;
|
|
});
|
|
|
|
configurator.processJson(invalidRadioIdBlindJson);
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, callbackInvokedCount);
|
|
}
|
|
|
|
void test_invalid_mqtt_id_is_ignored()
|
|
{
|
|
int callbackInvokedCount = 0;
|
|
|
|
Configurator configurator = Configurator();
|
|
configurator.addShadeConfiguredCallback([&] (Shade shade) {
|
|
callbackInvokedCount++;
|
|
});
|
|
|
|
configurator.processJson(invalidMqttIdBlindJson);
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, callbackInvokedCount);
|
|
}
|
|
|
|
int runUnityTests(void)
|
|
{
|
|
UNITY_BEGIN();
|
|
RUN_TEST(test_shade_is_configured_with_id_and_friendly_name);
|
|
RUN_TEST(test_multiple_shades_are_configured);
|
|
RUN_TEST(test_invalid_radio_id_is_ignored);
|
|
RUN_TEST(test_invalid_mqtt_id_is_ignored);
|
|
return UNITY_END();
|
|
}
|
|
|
|
/**
|
|
* For Arduino framework
|
|
*/
|
|
|
|
void setup()
|
|
{
|
|
// Wait ~2 seconds before the Unity test runner
|
|
// establishes connection with a board Serial interface
|
|
delay(2000);
|
|
|
|
runUnityTests();
|
|
}
|
|
void loop() {}
|
|
|
|
int main(void)
|
|
{
|
|
return runUnityTests();
|
|
} |