M5UnitUnified 0.0.2 git rev:a353e8c
Loading...
Searching...
No Matches
test_template.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
11#ifndef M5_UNIT_COMPONENT_GOOGLETEST_TEMPLATE_HPP
12#define M5_UNIT_COMPONENT_GOOGLETEST_TEMPLATE_HPP
13
15#include <type_traits>
16#include <Wire.h>
17#include <esp32-hal-i2c.h>
18
19namespace m5 {
20namespace unit {
25namespace googletest {
26
33template <uint32_t FREQ, uint32_t WNUM = 0>
34class GlobalFixture : public ::testing::Environment {
35 static_assert(WNUM < 2, "Wire number must be lesser than 2");
36
37public:
38 void SetUp() override
39 {
40 auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
41 auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
42
43 TwoWire* w[2] = {&Wire, &Wire1};
44 if (i2cIsInit(WNUM)) {
45 M5_LOGW("Already inititlized Wire. Terminate and restart");
46 w[WNUM]->end();
47 }
48 w[WNUM]->begin(pin_num_sda, pin_num_scl, FREQ);
49 }
50};
51
58template <typename U, typename TP>
59class ComponentTestBase : public ::testing::TestWithParam<TP> {
60 static_assert(std::is_base_of<m5::unit::Component, U>::value, "U must be derived from Component");
61
62protected:
63 virtual void SetUp() override
64 {
65 unit.reset(get_instance());
66 if (!unit) {
67 FAIL() << "Failed to get_instance";
68 GTEST_SKIP();
69 return;
70 }
71 ustr = m5::utility::formatString("%s:%s", unit->deviceName(), is_using_hal() ? "HAL" : "Wire");
72 if (!begin()) {
73 FAIL() << "Failed to begin " << ustr;
74 GTEST_SKIP();
75 }
76 }
77
78 virtual void TearDown() override
79 {
80 }
81
82 virtual bool begin()
83 {
84 if (is_using_hal()) {
85 // Using M5HAL
86 auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
87 auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
88 m5::hal::bus::I2CBusConfig i2c_cfg;
89 i2c_cfg.pin_sda = m5::hal::gpio::getPin(pin_num_sda);
90 i2c_cfg.pin_scl = m5::hal::gpio::getPin(pin_num_scl);
91 auto i2c_bus = m5::hal::bus::i2c::getBus(i2c_cfg);
92 return Units.add(*unit, i2c_bus ? i2c_bus.value() : nullptr) && Units.begin();
93 }
94 // Using TwoWire
95 return Units.add(*unit, Wire) && Units.begin();
96 }
97
99 virtual bool is_using_hal() const = 0;
101 virtual U* get_instance() = 0;
102
103 std::string ustr{};
104 std::unique_ptr<U> unit{};
106};
107
108} // namespace googletest
109} // namespace unit
110} // namespace m5
111#endif
Main header of M5UnitComponent.
For managing and leading units.
Definition M5UnitUnified.hpp:42
bool begin()
Begin of all units under management.
Definition M5UnitUnified.cpp:102
UnitComponent Derived class for testing.
Definition test_template.hpp:59
virtual U * get_instance()=0
return m5::unit::Component-derived class instance (decision based on TP)
virtual bool is_using_hal() const =0
Function returning true if M5HAL is used (decision based on TP)
Overall test environment configuration.
Definition test_template.hpp:34
For GoogleTest.
Top level namespace of M5stack.
Definition test_helper.hpp:18
Unit-related namespace.