From e4c7a1a8d2f93aab9f1f1c478174d0b78d9b6115 Mon Sep 17 00:00:00 2001 From: DNKpp Date: Tue, 24 Sep 2024 22:33:32 +0200 Subject: [PATCH] feat: MIMICPP_DETAIL_STRIP_PARENS and related helper macros --- include/mimic++/InterfaceMock.hpp | 19 ++++++++++++- test/unit-tests/InterfaceMock.cpp | 47 ++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/include/mimic++/InterfaceMock.hpp b/include/mimic++/InterfaceMock.hpp index ff635b36c..94f1a4e30 100644 --- a/include/mimic++/InterfaceMock.hpp +++ b/include/mimic++/InterfaceMock.hpp @@ -16,8 +16,25 @@ namespace mimicpp { /** - * \defgroup INTERFACES interfaces + * \defgroup MOCK_INTERFACES interfaces * \ingroup MOCK * \brief Contains utility to simplify interface mocking. */ + + /** + * \defgroup MOCK_INTERFACES_DETAIL detail + * \ingroup MOCK_INTERFACES + */ } + +/** + * \brief Removes an enclosing pair of (), if present. + * \ingroup MOCK_INTERFACES_DETAIL + * \param x token + * \see Inspired by https://stackoverflow.com/a/62984543 + */ +#define MIMICPP_DETAIL_STRIP_PARENS(x) MIMICPP_DETAIL_STRIP_PARENS_OUTER(MIMICPP_DETAIL_STRIP_PARENS_INNER x) +#define MIMICPP_DETAIL_STRIP_PARENS_INNER(...) MIMICPP_DETAIL_STRIP_PARENS_INNER __VA_ARGS__ +#define MIMICPP_DETAIL_STRIP_PARENS_OUTER(...) MIMICPP_DETAIL_STRIP_PARENS_OUTER_(__VA_ARGS__) +#define MIMICPP_DETAIL_STRIP_PARENS_OUTER_(...) MIMICPP_DETAIL_STRIP_PARENS_STRIPPED_ ## __VA_ARGS__ +#define MIMICPP_DETAIL_STRIP_PARENS_STRIPPED_MIMICPP_DETAIL_STRIP_PARENS_INNER diff --git a/test/unit-tests/InterfaceMock.cpp b/test/unit-tests/InterfaceMock.cpp index 77826e311..21788f996 100644 --- a/test/unit-tests/InterfaceMock.cpp +++ b/test/unit-tests/InterfaceMock.cpp @@ -1,7 +1,7 @@ - // Copyright Dominic (DNKpp) Koepke 2024 - 2024. - // Distributed under the Boost Software License, Version 1.0. - // (See accompanying file LICENSE_1_0.txt or copy at - // https://www.boost.org/LICENSE_1_0.txt) +// // Copyright Dominic (DNKpp) Koepke 2024 - 2024. +// // Distributed under the Boost Software License, Version 1.0. +// // (See accompanying file LICENSE_1_0.txt or copy at +// // https://www.boost.org/LICENSE_1_0.txt) #include "TestReporter.hpp" #include "TestTypes.hpp" @@ -16,4 +16,43 @@ #include #include +using namespace mimicpp; +#define TO_STRING_IMPL(...) #__VA_ARGS__ +#define TO_STRING(...) TO_STRING_IMPL(__VA_ARGS__) + +TEST_CASE( + "MIMICPP_DETAIL_STRIP_PARENS removes outer parens, if present.", + "[mock][mock::interface]" +) +{ + namespace Matches = Catch::Matchers; + + REQUIRE_THAT( + TO_STRING(MIMICPP_DETAIL_STRIP_PARENS()), + Matches::Equals("")); + + REQUIRE_THAT( + TO_STRING(MIMICPP_DETAIL_STRIP_PARENS(())), + Matches::Equals("")); + + REQUIRE_THAT( + TO_STRING(MIMICPP_DETAIL_STRIP_PARENS((()))), + Matches::Equals("()")); + + REQUIRE_THAT( + TO_STRING(MIMICPP_DETAIL_STRIP_PARENS(Test())), + Matches::Equals("Test()")); + + REQUIRE_THAT( + TO_STRING(MIMICPP_DETAIL_STRIP_PARENS((Test()))), + Matches::Equals("Test()")); + + REQUIRE_THAT( + TO_STRING(MIMICPP_DETAIL_STRIP_PARENS(((Test())))), + Matches::Equals("(Test())")); + + REQUIRE_THAT( + TO_STRING(MIMICPP_DETAIL_STRIP_PARENS(((,Test(),)))), + Matches::Equals("(,Test(),)")); +}