| 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | /* SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]> */ |
| 3 | |
| 4 | #ifndef __GPIOD_CXX_TEST_HELPERS_HPP__ |
| 5 | #define __GPIOD_CXX_TEST_HELPERS_HPP__ |
| 6 | |
| 7 | #include <catch2/catch_all.hpp> |
| 8 | #include <regex> |
| 9 | #include <string> |
| 10 | #include <sstream> |
| 11 | #include <system_error> |
| 12 | |
| 13 | class system_error_matcher : public Catch::Matchers::MatcherBase<::std::system_error> |
| 14 | { |
| 15 | public: |
| 16 | explicit system_error_matcher(int expected_errno); |
| 17 | ::std::string describe() const override; |
| 18 | bool match(const ::std::system_error& error) const override; |
| 19 | |
| 20 | private: |
| 21 | ::std::error_condition _m_cond; |
| 22 | }; |
| 23 | |
| 24 | class regex_matcher : public Catch::Matchers::MatcherBase<::std::string> |
| 25 | { |
| 26 | public: |
| 27 | explicit regex_matcher(const ::std::string& pattern); |
| 28 | ::std::string describe() const override; |
| 29 | bool match(const ::std::string& str) const override; |
| 30 | |
| 31 | private: |
| 32 | ::std::regex _m_pattern; |
| 33 | ::std::string _m_repr; |
| 34 | }; |
| 35 | |
| 36 | template<class T> class stringify_matcher : public Catch::Matchers::MatcherBase<T> |
| 37 | { |
| 38 | public: |
| 39 | explicit stringify_matcher(const ::std::string& expected) : _m_expected(expected) |
| 40 | { |
| 41 | |
| 42 | } |
| 43 | |
| 44 | ::std::string describe() const override |
| 45 | { |
| 46 | return "equals " + this->_m_expected; |
| 47 | } |
| 48 | |
| 49 | bool match(const T& obj) const override |
| 50 | { |
| 51 | ::std::stringstream buf; |
| 52 | |
| 53 | buf << obj; |
| 54 | |
| 55 | return buf.str() == this->_m_expected; |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | ::std::string _m_expected; |
| 60 | }; |
| 61 | |
| 62 | #endif /* __GPIOD_CXX_TEST_HELPERS_HPP__ */ |