我正在与CPPUNIT 1.12.1合作。
它定义了以下宏:
#define CPPUNIT_TEST_SUITE_ADD_TEST( test ) \
context.addTest( test )
#define CPPUNIT_TEST( testMethod ) \
CPPUNIT_TEST_SUITE_ADD_TEST( \
( new CPPUNIT_NS::TestCaller<TestFixtureType>( \
context.getTestNameFor( #testMethod), \
&TestFixtureType::testMethod, \
context.makeFixture() ) ) )我希望使用模板将许多测试添加到同一个测试套件中(因为CPPUNIT工作,每个测试都必须是一个void函数,因此使用模板可以使用不同的“参数”调用相同的void函数.)。
这是非常有效的:
class MyTestSuite1 : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(MyTestSuite1);
CPPUNIT_TEST(doTest<false>);
CPPUNIT_TEST(doTest<true>);
CPPUNIT_TEST_SUITE_END();
template<bool param> void doTest() { /* test here */ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(MyTestSuite1);虽然这并不是:
class MyTestSuite2 : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(MyTestSuite2);
CPPUNIT_TEST(doTest<false,false>);
CPPUNIT_TEST(doTest<true,false>);
CPPUNIT_TEST_SUITE_END();
template<bool param1,bool param2> void doTest() { /* test here */ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(MyTestSuite2);编译器(Visual 2015)报告:
1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(20):警告C4002:宏'CPPUNIT_TEST‘1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(20):警告C4002的实际参数太多:宏'CPPUNIT_TEST’1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(20):错误C2059的实际参数太多:语法错误:‘) 1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(21):错误C2059:语法错误:')’1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(22):错误C2143:语法错误:缺失';'}‘之前的1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(22):错误C2065:'namer':未声明的标识符1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(22):错误C2065:’工厂‘:未声明的标识符1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(22):错误C2059:语法错误::“1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(29):error C2143:语法错误:缺失”;'{‘{’1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(30):错误之前的‘C2143:语法错误:缺失';’‘之前'{’
为什么会这样呢?宏如何正确处理1个模板参数,但失败两个?知道我怎么能轻松地编译和工作吗?
编辑:已经尝试过CPPUNIT_TEST((doTest<false,false>));,但没有成功(获得error C2143: syntax error: missing ';' before ')')
发布于 2017-01-10 10:14:08
CPPUNIT_TEST(doTest<false,false>);由于宏认为您正在传递两个宏参数:doTest<false和false>,所以这个宏无法工作。
CPPUNIT_TEST((doTest<false,false>));这不起作用,因为&TestFixtureType::testMethod将扩展为无效的&TestFixtureType::(doTest<false,false>)。
正如Piotr在注释中提到的,您可以使用以下代码:
#define COMMA ,
class MyTestSuite2 : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(MyTestSuite2);
CPPUNIT_TEST(doTest<false COMMA false>);
CPPUNIT_TEST(doTest<true COMMA false>);
CPPUNIT_TEST_SUITE_END();
template<bool param1, bool param2> void doTest() { /* test here */ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(MyTestSuite2);因为预处理器会看到您想要传递一个参数。
发布于 2017-01-10 10:11:46
,在宏中被解析为分隔符(除非被父元素包围)。
工作方式
使用中间宏:
#define COMMA ,
CPPUNIT_TEST(doTest<false COMMA false>);或修复原始宏以处理逗号:
#define CPPUNIT_TEST(testMethod, ...) \
CPPUNIT_TEST_SUITE_ADD_TEST( \
( new CPPUNIT_NS::TestCaller<TestFixtureType>( \
context.getTestNameFor( #testMethod), \
&TestFixtureType::testMethod , ##__VA_ARGS__, \
context.makeFixture() ) ) )发布于 2017-01-10 09:50:21
这个有用吗?
CPPUNIT_TEST((doTest<false,false>));
CPPUNIT_TEST((doTest<true,false>));有时宏在解析逗号时会很棘手.
https://stackoverflow.com/questions/41565773
复制相似问题