From a470e8ca166ade5f0899886d8389909a37f21947 Mon Sep 17 00:00:00 2001 From: Corey Date: Wed, 3 Jan 2024 15:45:36 -0800 Subject: [PATCH] :sparkles: added servo mock + tests --- conanfile.py | 2 +- include/libhal-mock/servo.hpp | 47 +++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 1 + tests/main.test.cpp | 2 ++ tests/servo.test.cpp | 41 ++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 include/libhal-mock/servo.hpp create mode 100644 tests/servo.test.cpp diff --git a/conanfile.py b/conanfile.py index 6ff351c..f30fea3 100644 --- a/conanfile.py +++ b/conanfile.py @@ -25,7 +25,7 @@ class libhal_mock_conan(ConanFile): name = "libhal-mock" - version = "2.0.2" + version = "2.1.0" license = "Apache-2.0" url = "https://github.com/conan-io/conan-center-index" homepage = "https://libhal.github.io/libhal-mock" diff --git a/include/libhal-mock/servo.hpp b/include/libhal-mock/servo.hpp new file mode 100644 index 0000000..da9f1cd --- /dev/null +++ b/include/libhal-mock/servo.hpp @@ -0,0 +1,47 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include "testing.hpp" +#include + +namespace hal::mock { +/** + * @brief Mock servo implementation for use in unit tests and simulations with a + * spy function for position() + * + */ +struct servo : public hal::servo +{ + /** + * @brief Reset spy information for position() + * + */ + void reset() + { + spy_position.reset(); + } + + /// Spy handler for hal::servo::position() + spy_handler spy_position; + +private: + result driver_position(float p_position) override + { + HAL_CHECK(spy_position.record(p_position)); + return position_t{}; + }; +}; +} // namespace hal::mock diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e8e792f..e66d9f0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -57,6 +57,7 @@ add_executable(${PROJECT_NAME} steady_clock.test.cpp testing.test.cpp timer.test.cpp + servo.test.cpp ) target_include_directories(${PROJECT_NAME} PUBLIC . ../include) diff --git a/tests/main.test.cpp b/tests/main.test.cpp index 1d6458d..e773d04 100644 --- a/tests/main.test.cpp +++ b/tests/main.test.cpp @@ -25,6 +25,7 @@ extern void spi_mock_test(); extern void steady_clock_mock_test(); extern void testing_utilities_test(); extern void timer_mock_test(); +extern void servo_mock_test(); } // namespace hal::mock int main() @@ -41,4 +42,5 @@ int main() hal::mock::steady_clock_mock_test(); hal::mock::testing_utilities_test(); hal::mock::timer_mock_test(); + hal::mock::servo_mock_test(); } diff --git a/tests/servo.test.cpp b/tests/servo.test.cpp new file mode 100644 index 0000000..c8937f1 --- /dev/null +++ b/tests/servo.test.cpp @@ -0,0 +1,41 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include + +namespace hal::mock { +void servo_mock_test() +{ + using namespace boost::ut; + + // Setup + hal::mock::servo mock; + constexpr auto expected1 = hal::degrees(10.0); + constexpr auto expected2 = hal::degrees(-10.0); + mock.spy_position.trigger_error_on_call(3); + + // Exercise + Verify + expect(bool{ mock.position(expected1) }); + expect(expected1 == std::get<0>(mock.spy_position.call_history().at(0))); + + expect(bool{ mock.position(expected2) }); + expect(expected2 == std::get<0>(mock.spy_position.call_history().at(1))); + + expect(!mock.position(expected2)); + expect(expected2 == std::get<0>(mock.spy_position.call_history().at(2))); +}; +} // namespace hal::mock