Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Add embed::mock::temperature #281

Merged
merged 1 commit into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ add_executable(${TEST_NAME}
tests/adc/mock.test.cpp
tests/output_pin/mock.test.cpp
tests/input_pin/mock.test.cpp
tests/temperature/mock.test.cpp

tests/static_memory_resource.test.cpp
tests/frequency.test.cpp
Expand Down
39 changes: 39 additions & 0 deletions include/libembeddedhal/temperature/mock.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <queue>

#include "interface.hpp"

namespace embed::mock {
/**
* @brief Mock temperature sensor implementation for use in unit tests and
* simulations.
*
*/
struct temperature_sensor : public embed::temperature_sensor
{
/**
* @brief Queues the temperature values to be returned for read()
*
* @param p_temperatures - queue of temperatures
*/
void set(std::queue<temperature>& p_temperatures)
{
m_temperatures = p_temperatures;
}

private:
boost::leaf::result<temperature> driver_read() noexcept
{
if (m_temperatures.size() == 0) {
return boost::leaf::new_error(
std::out_of_range("temperatures queue is empty!"));
}
temperature m_current_value = m_temperatures.front();
m_temperatures.pop();
return m_current_value;
}

std::queue<temperature> m_temperatures{};
};
} // namespace embed::mock
29 changes: 29 additions & 0 deletions tests/temperature/mock.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <deque>

#include <boost/ut.hpp>
#include <libembeddedhal/temperature/mock.hpp>

namespace embed {
boost::ut::suite temperature_mock_test = []() {
using namespace boost::ut;

"embed::mock::temperature::read()"_test = []() {
// Setup
embed::mock::temperature_sensor mock;
auto expected1 = temperature(0);
auto expected2 = temperature(1000);
auto expected3 = temperature(2000);
std::deque temps{ expected1, expected2, expected3 };
std::queue queue(temps);

// Exercise
mock.set(queue);

// Verify
expect(expected1 == mock.read().value());
expect(expected2 == mock.read().value());
expect(expected3 == mock.read().value());
expect(throws([&mock] { mock.read().value(); }));
};
};
} // namespace embed