Con*_*man 38 c++ unit-testing private-members template-meta-programming
我正在为本科生编写单元测试,并希望将某些成员强制为公共或私有成员。我知道实际测试私有成员的方法,例如,#define private public或使用朋友类,但没有看到任何可以让我准确检查成员是否为私有的方法。
蛮力方法将尝试编译并解析输出错误,例如,寻找类似的东西error: 'foo' is a private member of 'Bar',但我希望有人会有更好的技巧!
cig*_*ien 34
如果要断言某个类型Bar没有名为 的公共成员foo,则可以编写以下测试:
template<typename T>
constexpr auto has_public_foo(T const &t) -> decltype(t.foo, true) 
{
    return true;
}
constexpr auto has_public_foo(...) 
{
    return false;
}
static_assert(not has_public_foo(Bar{}), "Public members are bad practice");
这是一个演示。
Sla*_*ica 16
您可以使用Clang 编译器的AST输出来验证某个成员是私有的还是公共的。例如,对于以下代码:
class test {
public:
    int pub;
private:
    int prv;
};
运行此命令: clang -Xclang -ast-dump -fsyntax-only t.cpp
给出 AST 转储:
TranslationUnitDecl 0x55f6f550e3f8 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x55f6f550e9b0 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
| `-BuiltinType 0x55f6f550e690 '__int128'
|-TypedefDecl 0x55f6f550ea20 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
| `-BuiltinType 0x55f6f550e6b0 'unsigned __int128'
|-TypedefDecl 0x55f6f550ed68 <<invalid sloc>> <invalid sloc> implicit __NSConstantString '__NSConstantString_tag'
| `-RecordType 0x55f6f550eb10 '__NSConstantString_tag'
|   `-CXXRecord 0x55f6f550ea78 '__NSConstantString_tag'
|-TypedefDecl 0x55f6f550ee00 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'
| `-PointerType 0x55f6f550edc0 'char *'
|   `-BuiltinType 0x55f6f550e490 'char'
|-TypedefDecl 0x55f6f5545bf8 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list '__va_list_tag [1]'
| `-ConstantArrayType 0x55f6f5545ba0 '__va_list_tag [1]' 1
|   `-RecordType 0x55f6f550eef0 '__va_list_tag'
|     `-CXXRecord 0x55f6f550ee58 '__va_list_tag'
`-CXXRecordDecl 0x55f6f5545c50 <t.cpp:1:1, line:6:1> line:1:7 class test definition
  |-DefinitionData pass_in_registers trivially_copyable trivial literal
  | |-DefaultConstructor exists trivial needs_implicit
  | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
  | |-MoveConstructor exists simple trivial needs_implicit
  | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param
  | |-MoveAssignment exists simple trivial needs_implicit
  | `-Destructor simple irrelevant trivial needs_implicit
  |-CXXRecordDecl 0x55f6f5545d78 <col:1, col:7> col:7 implicit class test
  |-AccessSpecDecl 0x55f6f5545e10 <line:2:1, col:7> col:1 public
  |-FieldDecl 0x55f6f5545e50 <line:3:5, col:9> col:9 pub 'int'
  |-AccessSpecDecl 0x55f6f5545e98 <line:4:1, col:8> col:1 private
  `-FieldDecl 0x55f6f5545ed8 <line:5:5, col:9> col:9 prv 'int'
这很容易通过脚本解析。或者,您可以使用 Clang AST 库创建LibASTMatcher以使用文档中所述的数据本身进行验证。
在我测试非常量对象时附加cigien 的正确答案,即T在对象的构造过程中进行了更改,因此将在运行时而不是编译时进行测试。这仅涉及删除const关键字:
// Check that color is a public member
template<typename T>
auto has_public_color(T &t) -> decltype(t.color, true)
{
    // Returns true if T has a public member named color
    return true;
}
auto has_public_color(...)
{
    return false;
}
然后我只是插入到我的单元测试框架(Boost 单元测试)中:
BOOST_AUTO_TEST_CASE(test_cell_no_public_color)
{
    BOOST_TEST_MESSAGE("Testing that Cell has no public member of color");
    // Check that Cell has no public color
    BOOST_CHECK(not has_public_color(Cell{}));
}