GTest 的 C++11 问题

C++11 issue with GTest

我正在为应用程序编写单元测试。我在构造函数中有一些异常,所以我这样写:

TEST(Tablier, ConstructeurParamInvalide2)
{
    ASSERT_THROW(Tablier t_tablier{10, 65} , PreconditionException);
}

写这个的时候好像宏ASSERT_THROW不满足,测试失败。这是宏扩展:

switch (0) case 0: default: \
  if (::testing::internal::ConstCharPtr gtest_msg = "") { \
    bool gtest_caught_expected = false; \
    try { \
      if (::testing::internal::AlwaysTrue()) { Tablier t_tablier{10; }; \
    } \
    catch (65} const&) { \
      gtest_caught_expected = true; \
    } \
    catch (...) { \
      gtest_msg.value = \
          "Expected: " "Tablier t_tablier{10" " throws an exception of type " \
          "65}" ".\n  Actual: it throws a different type."; \
      goto gtest_label_testthrow_76; \
    } \
    if (!gtest_caught_expected) { \
      gtest_msg.value = \
          "Expected: " "Tablier t_tablier{10" " throws an exception of type " \
          "65}" ".\n  Actual: it throws nothing."; \
      goto gtest_label_testthrow_76; \
    } \
  } else \
    gtest_label_testthrow_76: \
      return ::testing::internal::AssertHelper(::testing::TestPartResult::kFatalFailure, "/home/eric/Programming/cpp/Puissance4/pxTestsUnitaires/tests/test_Tablier.cpp", 76, gtest_msg.value) \
    = ::testing::Message()

注意 Tablier t_tablier{10; }; 相反,如果我这样写:

TEST(Tablier, ConstructeurParamInvalide2)
{
    ASSERT_THROW(Tablier t_tablier(10, 65) , PreconditionException);
}

宏运行正常,测试通过。我的项目和编译器针对 C++11 配置,许多其他测试使用 C++11 语法通过。知道可能是什么问题吗?

这应该有效:

ASSERT_THROW(Tablier t_tablier(10, 65) , PreconditionException);
ASSERT_THROW(Tablier (10, 65) , PreconditionException);

由于宏扩展认为10和65之间的逗号是宏参数分隔符。括号是用来告诉编译器哪一个是定界符。

来自cpp.replace

The sequence of preprocessing tokens bounded by the outside-most matching parentheses forms the list of arguments for the function-like macro. The individual arguments within the list are separated by comma preprocessing tokens, but comma preprocessing tokens between matching inner parentheses do not separate arguments.