我有一个 boost::variant 包含命令。我正在编写一个PathValidator
访问者,它对Create
命令采取一种方式,对所有其他命令采取一种方式。这是我正在测试的方式。随着我添加更多测试,我将需要编写更多 HelperVisitors。关于如何使这更具可读性和更少冗长的任何提示?我正在使用 C++98。或者,也许你认为这没问题。
struct TestOneNodeInTreeHelper
{
explicit TestOneNodeInTreeHelper(TestPathValidator& test): _test(test)
{
}
void operator()(Create&) const
{
SyntaxTree::Create create;
_test.addNodeToSyntaxTree(create.nodes,"1");
PathValidator pathValidator(_test._accessorMock);
pathValidator(create);
EXPECT_EQ((int)_test._accessorMock->path.size(), 0);
}
template<class T> void operator()(const T&) const
{
SyntaxTree::Create create;
_test.addNodeToSyntaxTree(create.nodes,"1");
PathValidator pathValidator(_test._accessorMock);
pathValidator(create);
EXPECT_EQ((int)_test._accessorMock->path.size(), 0);
EXPECT_EQ(_test._accessorMock->path[0], "path1");
}
TestPathValidator& _test;
};
TEST_F (TestPathValidator, oneNodeInTree)
{
TestOneNodeInTreeHelper helper(*this);
boost::mpl::for_each<Command::types>(helper);
}