Skip to content

Commit

Permalink
✨ added servo mock + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Coreyboy1820 authored and kammce committed Jan 4, 2024
1 parent 9d60539 commit a470e8c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
47 changes: 47 additions & 0 deletions include/libhal-mock/servo.hpp
Original file line number Diff line number Diff line change
@@ -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 <libhal/servo.hpp>

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<float> spy_position;

private:
result<position_t> driver_position(float p_position) override
{
HAL_CHECK(spy_position.record(p_position));
return position_t{};
};
};
} // namespace hal::mock
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/main.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
}
41 changes: 41 additions & 0 deletions tests/servo.test.cpp
Original file line number Diff line number Diff line change
@@ -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 <libhal-mock/servo.hpp>
#include <libhal/units.hpp>

#include <boost/ut.hpp>

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

0 comments on commit a470e8c

Please sign in to comment.