From 34602643c2d6e65ca7f612c7a40f499c5aba6f56 Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 2 Sep 2024 13:33:30 -0700 Subject: [PATCH] cleaned up interfaces for mocking --- CMakeLists.txt | 1 + src/CMakeLists.txt | 1 - src/timer/timer.h | 13 +++++--- tests/Relays/test_Relays.cpp | 59 +++++++++++++++++++++++++++++++++--- 4 files changed, 64 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bfb49a4..65f0744 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,6 +69,7 @@ endif() include_directories( + /usr/local/avr/include /usr/local/avr/avr/include ./inc ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ed905b1..49ebd28 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -57,4 +57,3 @@ add_subdirectory(timer) add_subdirectory(SuperLoop) add_subdirectory(Relays) -add_subdirectory(TimerMock) diff --git a/src/timer/timer.h b/src/timer/timer.h index 33a42bc..833712b 100644 --- a/src/timer/timer.h +++ b/src/timer/timer.h @@ -14,14 +14,19 @@ #include "stdbool.h" /** - * - * @param a The first argument + * Starts up the AVR timer using a 10KHz frequency */ - void Timer_Start(void); -void Timer_Disable(void); +/** + * Stops the AVR timer. + */ +void Timer_Stop(void); +/** + * Get the number of times the timer's counter setup for 10kHz overflowed. + * @return A uint16_t holding the number of counter overflow events. + */ uint16_t Timer_GetOverflowCount(void); #endif // TIMER diff --git a/tests/Relays/test_Relays.cpp b/tests/Relays/test_Relays.cpp index a6982d7..d63ecdc 100644 --- a/tests/Relays/test_Relays.cpp +++ b/tests/Relays/test_Relays.cpp @@ -6,10 +6,12 @@ */ #include "CppUTest/CommandLineTestRunner.h" +#include "CppUTestExt/MockSupport.h" extern "C" { #include "Relays.h" +#include "TimerMock.h" } void zero_relay_struct(Relay *relay) @@ -48,11 +50,6 @@ TEST(test_Relays, CreateRelayStruct) CHECK(relay.input_port == &fake_input_port); } -TEST(test_Relays, MeasureMakeTimeForRelay) -{ - FAIL_TEST("Not currently ready for testing."); -} - TEST(test_Relays, DisabledForPowerCycleSetsDisabledBit) { Relay_DisableForPowerCycle(&relay); @@ -124,3 +121,55 @@ TEST(test_Relays, ReadStateOfEnabledRelayReadsTrue) CHECK_FALSE(Relay_ReadState(&relay)); } } + + +TEST_GROUP(test_RelaysTimer) +{ + uint8_t fake_input_port; + uint8_t fake_ouput_port; + Relay relay; + + void setup() { + relay.input_port = &fake_input_port; + relay.output_port = &fake_ouput_port; + zero_relay_struct(&relay); + } + + void teardown() { + fake_input_port = 0x00; + fake_ouput_port = 0x00; + mock().clear(); + } +}; + +TEST(test_RelaysTimer, MakeTimeCallsCorrectly) +{ + uint16_t fake_return = 56; + mock().expectOneCall("Timer_Start"); + mock().expectOneCall("Timer_Stop"); + mock().expectOneCall("Timer_GetOverflowCount") + .andReturnValue(fake_return); + + //Set the input hi + *(uint8_t *)relay.input_port |= (1 << relay.input_pin); + + Relay_MeasureMakeTime(&relay); + CHECK_EQUAL(fake_return, relay.make_time); + + mock().checkExpectations(); +} + + +TEST(test_RelaysTimer, BreakTimeCallsCorrectly) +{ + uint16_t fake_return = 56; + mock().expectOneCall("Timer_Start"); + mock().expectOneCall("Timer_Stop"); + mock().expectOneCall("Timer_GetOverflowCount") + .andReturnValue(fake_return); + + Relay_MeasureBreakTime(&relay); + CHECK_EQUAL(fake_return, relay.break_time); + + mock().checkExpectations(); +}