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

Commit 4da8b69

Browse files
Khalil Estellkammce
authored andcommitted
🎨 Use libhal_unit_test for unit tests
Simplifies and standardizes the process for building libhal package unit tests. Also utilizes clang-tidy in the unit test process
1 parent dcbabdf commit 4da8b69

17 files changed

Lines changed: 84 additions & 149 deletions

CMakeLists.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cmake_minimum_required(VERSION 3.15)
16+
17+
# Because libhal is a header only library, this cmake file only builds and
18+
# executes the unit tests
19+
project(libhal LANGUAGES CXX)
20+
21+
libhal_unit_test(SOURCES
22+
tests/helpers.cpp
23+
tests/can.test.cpp
24+
tests/pwm.test.cpp
25+
tests/timer.test.cpp
26+
tests/i2c.test.cpp
27+
tests/spi.test.cpp
28+
tests/adc.test.cpp
29+
tests/dac.test.cpp
30+
tests/input_pin.test.cpp
31+
tests/interrupt_pin.test.cpp
32+
tests/output_pin.test.cpp
33+
tests/serial.test.cpp
34+
tests/steady_clock.test.cpp
35+
tests/motor.test.cpp
36+
tests/timeout.test.cpp
37+
tests/error.test.cpp
38+
tests/accelerometer.test.cpp
39+
tests/distance_sensor.test.cpp
40+
tests/gyroscope.test.cpp
41+
tests/magnetometer.test.cpp
42+
tests/rotation_sensor.test.cpp
43+
tests/temperature_sensor.test.cpp
44+
tests/servo.test.cpp
45+
tests/g_force.test.cpp
46+
tests/lengths.test.cpp
47+
tests/main.test.cpp
48+
49+
PACKAGES
50+
boost-leaf
51+
tl-function-ref
52+
53+
LINK_LIBRARIES
54+
boost::leaf
55+
tl::function-ref)

conanfile.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class libhal_conan(ConanFile):
3434
"peripherals and devices using modern C++")
3535
topics = ("peripherals", "hardware", "abstraction", "devices", "hal")
3636
settings = "compiler", "build_type", "os", "arch"
37-
exports_sources = "include/*", "tests/*", "LICENSE"
37+
exports_sources = "include/*", "tests/*", "CMakeLists.txt", "LICENSE"
3838
generators = "CMakeToolchain", "CMakeDeps"
3939
no_copy_source = True
4040

@@ -60,25 +60,20 @@ def validate(self):
6060

6161
def build_requirements(self):
6262
self.tool_requires("cmake/3.27.1")
63+
self.tool_requires("libhal-cmake-util/1.2.0")
64+
self.test_requires("boost-ext-ut/1.1.9")
6365

6466
def requirements(self):
6567
self.requires("tl-function-ref/1.0.0")
6668
self.requires("boost-leaf/1.81.0")
67-
self.test_requires("boost-ext-ut/1.1.9")
6869

6970
def layout(self):
7071
cmake_layout(self)
7172

7273
def build(self):
73-
if not self.conf.get("tools.build:skip_test", default=False):
74-
cmake = CMake(self)
75-
if self.settings.os == "Windows":
76-
cmake.configure(build_script_folder="tests")
77-
else:
78-
cmake.configure(build_script_folder="tests",
79-
variables={"ENABLE_ASAN": True})
80-
cmake.build()
81-
self.run(os.path.join(self.cpp.build.bindir, "unit_test"))
74+
cmake = CMake(self)
75+
cmake.configure()
76+
cmake.build()
8277

8378
def package(self):
8479
copy(self, "LICENSE", dst=os.path.join(

tests/CMakeLists.txt

Lines changed: 0 additions & 96 deletions
This file was deleted.

tests/adc.test.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ class test_adc : public hal::adc
2626
constexpr static float m_returned_position{ 0.5f };
2727
bool m_return_error_status{ false };
2828

29-
~test_adc()
30-
{
31-
}
29+
~test_adc() override = default;
3230

3331
private:
3432
result<read_t> driver_read() override

tests/can.test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class test_can : public hal::can
3636
hal::callback<handler> m_handler = [](const message_t&) {};
3737
bool m_return_error_status{ false };
3838
bool m_bus_on_called{ false };
39+
~test_can() override = default;
3940

4041
private:
4142
status driver_configure(const settings& p_settings) override

tests/dac.test.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ class test_dac : public hal::dac
2525
float m_passed_value{};
2626
bool m_return_error_status{ false };
2727

28-
~test_dac()
29-
{
30-
}
28+
~test_dac() override = default;
3129

3230
private:
3331
result<write_t> driver_write(float p_value) override

tests/error.test.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,16 @@ void error_test()
4343

4444
"[success] hal::attempt calls handler"_test = []() {
4545
// Setup
46-
constexpr int expected = 123456789;
46+
static const int expected = 123456789;
4747
int value_to_be_change = 0;
4848

4949
// Exercise
5050
// Should call the `on_error_callback` defined in the tweaks file
51-
auto result =
52-
attempt([expected]() -> status { return new_error(expected); },
53-
[&value_to_be_change](int p_handler_value) -> status {
54-
value_to_be_change = p_handler_value;
55-
return {};
56-
});
51+
auto result = attempt([]() -> status { return new_error(expected); },
52+
[&value_to_be_change](int p_handler_value) -> status {
53+
value_to_be_change = p_handler_value;
54+
return {};
55+
});
5756

5857
// Verify
5958
expect(that % value_to_be_change == expected);

tests/helpers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
bool compare_floats(float p_first, float p_second, float p_error_margin)
44
{
5-
float difference = abs(p_first - p_second);
5+
float difference = std::abs(p_first - p_second);
66
return difference < p_error_margin;
77
}

tests/i2c.test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ class test_i2c : public hal::i2c
3535
hal::byte m_address{};
3636
std::span<const hal::byte> m_data_out{};
3737
std::span<hal::byte> m_data_in{};
38+
bool m_return_error_status{ false };
3839
std::function<hal::timeout_function> m_timeout = []() -> hal::status {
3940
return hal::success();
4041
};
41-
bool m_return_error_status{ false };
42+
43+
~test_i2c() override = default;
4244

4345
private:
4446
status driver_configure(const settings& p_settings) override

tests/input_pin.test.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ class test_input_pin : public hal::input_pin
2727
settings m_settings{};
2828
bool m_return_error_status{ false };
2929

30-
~test_input_pin()
31-
{
32-
}
30+
~test_input_pin() override = default;
3331

3432
private:
3533
status driver_configure(const settings& p_settings) override

0 commit comments

Comments
 (0)