WIP using new PositionWatcher

This commit is contained in:
2025-01-20 22:46:01 +11:00
parent 049037a0a1
commit 94f00c0cef
10 changed files with 446 additions and 115 deletions

View File

@@ -23,7 +23,7 @@ void test_publish_message_count()
HADiscovery haDiscovery = HADiscovery("my_prefix/", callback);
haDiscovery.publish(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
haDiscovery.publish(Shade{0xABCD, "test_shade", "Test Shade", "stopped", -1, -1});
TEST_ASSERT_EQUAL_INT(3, callbackInvokedCount);
}
@@ -47,7 +47,7 @@ void test_publish_cover_message()
HADiscovery haDiscovery = HADiscovery("my_prefix/", callback);
haDiscovery.publish(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
haDiscovery.publish(Shade{0xABCD, "test_shade", "Test Shade", "stopped", -1, -1});
TEST_ASSERT_EQUAL_INT(1, messageAssertedCount);
}
@@ -71,7 +71,7 @@ void test_publish_battery_message()
HADiscovery haDiscovery = HADiscovery("my_prefix/", callback);
haDiscovery.publish(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
haDiscovery.publish(Shade{0xABCD, "test_shade", "Test Shade", "stopped", -1, -1});
TEST_ASSERT_EQUAL_INT(1, messageAssertedCount);
}
@@ -95,7 +95,7 @@ void test_publish_button_message()
HADiscovery haDiscovery = HADiscovery("my_prefix/", callback);
haDiscovery.publish(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
haDiscovery.publish(Shade{0xABCD, "test_shade", "Test Shade", "stopped", -1, -1});
TEST_ASSERT_EQUAL_INT(1, messageAssertedCount);
}

View File

@@ -0,0 +1,161 @@
#include <unity.h>
#include <Arduino.h>
#include <ArduinoFake.h>
#include "PositionWatcher.h"
using namespace fakeit;
void setUp()
{
}
void tearDown()
{
}
void test_position_watcher_should_not_fetch_initially()
{
PositionWatcher positionWatcher = PositionWatcher();
TEST_ASSERT_FALSE(positionWatcher.shouldFetch());
}
void test_position_watcher_should_fetch_after_start()
{
When(Method(ArduinoFake(), millis)).Return(10000);
PositionWatcher positionWatcher = PositionWatcher();
TEST_ASSERT_FALSE(positionWatcher.shouldFetch());
positionWatcher.start(75);
TEST_ASSERT_TRUE(positionWatcher.shouldFetch());
}
void test_position_watcher_should_not_fetch_before_polling_interval()
{
When(Method(ArduinoFake(), millis)).AlwaysReturn(10000);
PositionWatcher positionWatcher = PositionWatcher();
TEST_ASSERT_FALSE(positionWatcher.shouldFetch());
positionWatcher.start(75);
TEST_ASSERT_TRUE(positionWatcher.shouldFetch());
positionWatcher.fetchQueued();
TEST_ASSERT_FALSE(positionWatcher.shouldFetch());
}
void test_position_watcher_should_fetch_after_polling_interval()
{
When(Method(ArduinoFake(), millis)).AlwaysReturn(10000);
PositionWatcher positionWatcher = PositionWatcher();
TEST_ASSERT_FALSE(positionWatcher.shouldFetch());
positionWatcher.start(75);
TEST_ASSERT_TRUE(positionWatcher.shouldFetch());
positionWatcher.fetchQueued();
TEST_ASSERT_FALSE(positionWatcher.shouldFetch());
When(Method(ArduinoFake(), millis)).AlwaysReturn(20000);
TEST_ASSERT_TRUE(positionWatcher.shouldFetch());
}
void test_position_watcher_should_not_fetch_after_max_attempts()
{
When(Method(ArduinoFake(), millis)).AlwaysReturn(10000);
PositionWatcher positionWatcher = PositionWatcher();
TEST_ASSERT_FALSE(positionWatcher.shouldFetch());
positionWatcher.start(75);
TEST_ASSERT_TRUE(positionWatcher.shouldFetch());
positionWatcher.fetchQueued();
TEST_ASSERT_FALSE(positionWatcher.shouldFetch());
When(Method(ArduinoFake(), millis)).AlwaysReturn(20000);
TEST_ASSERT_TRUE(positionWatcher.shouldFetch());
positionWatcher.fetchQueued();
When(Method(ArduinoFake(), millis)).AlwaysReturn(30000);
TEST_ASSERT_TRUE(positionWatcher.shouldFetch());
positionWatcher.fetchQueued();
When(Method(ArduinoFake(), millis)).AlwaysReturn(40000);
TEST_ASSERT_FALSE(positionWatcher.shouldFetch());
}
void test_position_watcher_should_stop_after_enough_duplicates()
{
When(Method(ArduinoFake(), millis)).AlwaysReturn(10000);
PositionWatcher positionWatcher = PositionWatcher();
TEST_ASSERT_FALSE(positionWatcher.shouldFetch());
positionWatcher.start(75);
TEST_ASSERT_TRUE(positionWatcher.shouldFetch());
positionWatcher.fetchQueued();
positionWatcher.fetchReceived(50);
TEST_ASSERT_TRUE(positionWatcher.isWatching());
positionWatcher.fetchQueued();
positionWatcher.fetchReceived(60);
TEST_ASSERT_TRUE(positionWatcher.isWatching());
positionWatcher.fetchQueued();
positionWatcher.fetchReceived(60);
TEST_ASSERT_FALSE(positionWatcher.isWatching());
}
void test_position_watcher_should_stop_after_target_value()
{
When(Method(ArduinoFake(), millis)).AlwaysReturn(10000);
PositionWatcher positionWatcher = PositionWatcher();
TEST_ASSERT_FALSE(positionWatcher.shouldFetch());
positionWatcher.start(75);
TEST_ASSERT_TRUE(positionWatcher.shouldFetch());
positionWatcher.fetchQueued();
positionWatcher.fetchReceived(50);
TEST_ASSERT_TRUE(positionWatcher.isWatching());
positionWatcher.fetchQueued();
positionWatcher.fetchReceived(60);
TEST_ASSERT_TRUE(positionWatcher.isWatching());
positionWatcher.fetchQueued();
positionWatcher.fetchReceived(75);
TEST_ASSERT_FALSE(positionWatcher.isWatching());
}
int runUnityTests(void)
{
UNITY_BEGIN();
RUN_TEST(test_position_watcher_should_not_fetch_initially);
RUN_TEST(test_position_watcher_should_fetch_after_start);
RUN_TEST(test_position_watcher_should_not_fetch_before_polling_interval);
RUN_TEST(test_position_watcher_should_fetch_after_polling_interval);
RUN_TEST(test_position_watcher_should_not_fetch_after_max_attempts);
RUN_TEST(test_position_watcher_should_stop_after_enough_duplicates);
RUN_TEST(test_position_watcher_should_stop_after_target_value);
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();
}

View File

@@ -13,7 +13,7 @@ void tearDown()
void test_shade_is_found_with_id()
{
ShadeRepository shadeRepository = ShadeRepository();
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", "stopped", -1, -1});
auto shade = shadeRepository.findById(0xABCD);
@@ -26,7 +26,7 @@ void test_shade_is_found_with_id()
void test_shade_is_found_with_key()
{
ShadeRepository shadeRepository = ShadeRepository();
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", "stopped", -1, -1});
auto shade = shadeRepository.findByKey("test_shade");
@@ -39,8 +39,8 @@ void test_shade_is_found_with_key()
void test_adding_shade_twice_only_added_once()
{
ShadeRepository shadeRepository = ShadeRepository();
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", "stopped", -1, -1});
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", "stopped", -1, -1});
int count = 0;
@@ -54,13 +54,13 @@ void test_adding_shade_twice_only_added_once()
void test_updating_shade_id()
{
ShadeRepository shadeRepository = ShadeRepository();
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", "stopped", -1, -1});
auto shade1 = shadeRepository.findById(0xABCD);
TEST_ASSERT_EQUAL_HEX16(0xABCD, shade1->ID);
shadeRepository.upsert(Shade{0x1234, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
shadeRepository.upsert(Shade{0x1234, "test_shade", "Test Shade", "stopped", -1, -1});
auto shade2 = shadeRepository.findById(0x1234);
@@ -74,13 +74,13 @@ void test_updating_shade_id()
void test_updating_shade_friendly_name()
{
ShadeRepository shadeRepository = ShadeRepository();
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", "stopped", -1, -1});
auto shade1 = shadeRepository.findByKey("test_shade");
TEST_ASSERT_EQUAL_STRING("Test Shade", shade1->friendlyName.c_str());
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Updated Test Shade", -1, -1, 0, 0, nullptr});
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Updated Test Shade", "stopped", -1, -1});
auto shade2 = shadeRepository.findByKey("test_shade");
@@ -101,7 +101,7 @@ void test_shade_added_callback()
TEST_ASSERT_EQUAL_STRING("test_shade", shade.key.c_str());
});
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", "stopped", -1, -1});
TEST_ASSERT_EQUAL_INT(1, callbackInvokedCount);
}
@@ -119,8 +119,8 @@ void test_shade_changed_callback()
TEST_ASSERT_EQUAL_STRING("test_shade", shade.key.c_str());
});
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
shadeRepository.upsert(Shade{0x1234, "test_shade", "Updated Test Shade", -1, -1, 0, 0, nullptr});
shadeRepository.upsert(Shade{0xABCD, "test_shade", "Test Shade", "stopped", -1, -1});
shadeRepository.upsert(Shade{0x1234, "test_shade", "Updated Test Shade", "stopped", -1, -1});
TEST_ASSERT_EQUAL_INT(1, callbackInvokedCount);
}