Skip to content

Commit

Permalink
feat: MIMICPP_DETAIL_STRIP_PARENS and related helper macros
Browse files Browse the repository at this point in the history
  • Loading branch information
DNKpp committed Sep 24, 2024
1 parent 848060b commit e4c7a1a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
19 changes: 18 additions & 1 deletion include/mimic++/InterfaceMock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
47 changes: 43 additions & 4 deletions test/unit-tests/InterfaceMock.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -16,4 +16,43 @@
#include <catch2/matchers/catch_matchers_string.hpp>
#include <catch2/matchers/catch_matchers_templated.hpp>

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(),)"));
}

0 comments on commit e4c7a1a

Please sign in to comment.