M5UnitUnified 0.0.5 git rev:3b942a7
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#if defined(CONFIG_IDF_TARGET_ESP32C6)
44 TwoWire* w[1] = {&Wire};
45#else
46 TwoWire* w[2] = {&Wire, &Wire1};
47#endif
48 if (WNUM < m5::stl::size(w) && i2cIsInit(WNUM)) {
49 M5_LOGW("Already inititlized Wire %d. Terminate and restart FREQ %u", WNUM, FREQ);
50 w[WNUM]->end();
51 }
52 w[WNUM]->begin(pin_num_sda, pin_num_scl, FREQ);
53 }
54};
55
62template <typename U, typename TP>
63class ComponentTestBase : public ::testing::TestWithParam<TP> {
64 static_assert(std::is_base_of<m5::unit::Component, U>::value, "U must be derived from Component");
65
66protected:
67 virtual void SetUp() override
68 {
69 unit.reset(get_instance());
70 if (!unit) {
71 FAIL() << "Failed to get_instance";
72 GTEST_SKIP();
73 return;
74 }
75 ustr = m5::utility::formatString("%s:%s", unit->deviceName(), is_using_hal() ? "HAL" : "Wire");
76 if (!begin()) {
77 FAIL() << "Failed to begin " << ustr;
78 GTEST_SKIP();
79 }
80 }
81
82 virtual void TearDown() override
83 {
84 }
85
86 virtual bool begin()
87 {
88 if (is_using_hal()) {
89 // Using M5HAL
90 auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
91 auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
92 m5::hal::bus::I2CBusConfig i2c_cfg;
93 i2c_cfg.pin_sda = m5::hal::gpio::getPin(pin_num_sda);
94 i2c_cfg.pin_scl = m5::hal::gpio::getPin(pin_num_scl);
95 auto i2c_bus = m5::hal::bus::i2c::getBus(i2c_cfg);
96 return Units.add(*unit, i2c_bus ? i2c_bus.value() : nullptr) && Units.begin();
97 }
98 // Using TwoWire
99 return Units.add(*unit, Wire) && Units.begin();
100 }
101
103 virtual bool is_using_hal() const = 0;
105 virtual U* get_instance() = 0;
106
107 std::string ustr{};
108 std::unique_ptr<U> unit{};
110};
111
112} // namespace googletest
113} // namespace unit
114} // namespace m5
115#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:63
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.