Files
Hotdog/test/test_position_watcher/test_position_watcher.cpp

161 lines
4.4 KiB
C++

#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();
}