我有一个用C ++编写的程序,为了确保我们在进行更改时不会破坏任何内容,我想添加单元测试。
在程序中,我们使用宏来创建经常使用的某些对象。这看起来像是:
#define PROPERTY_SWITCHPOINT(var) \
private: \
comp::SwitchPoint* m##var; \
public: \
void set##var(bool val, unsigned short switchIdx = 0) \
{\
if(m##var) m##var->setValue(val,switchIdx); \
}\
bool get##var() \
{\
return (NULL == m##var ? false : m##var->getValue()); \
}\
comp::SwitchPoint* get##var##Ptr() \
{\
return m##var; \
}
在包含switchpoints的类的标题中,我们称之为宏
class Classname
{
private:
PROPERTY_SWITCHPOINT(SwitchpointObject)
}
在包含switchpoints的类的构造函数中,我们会这样做:
Classname::Classname()
{
mSwitchpointObject = CreateSwitchpoint("Switchpoint name", 2);
}
comp::SwitchPoint* Classname::CreateSwitchpoint(const std::string& name, unsigned short numberOfSwitches = 1)
{
comp::SwitchPoint* sp = new comp::SwitchPoint(name, true, numberOfSwitches);
return sp;
}
现在我们可以使用mSwitchpointObject->getValue()
来获取此对象的值。所有这些都有效,但我无法为它创建单元测试,我正在使用unittest ++框架。
我试过这个测试:
#include "UnitTest++.h"
#include "Classname.h"
namespace
{
TEST(SwitchpointTest1)
{
PROPERTY_SWITCHPOINT(SwitchpointObject)
mSwitchpointTestVariabele = CreateSwitchpoint("test switchpoint", 2);
CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), true);
//mSwitchpointTestVariabele->setValue(false, 0);
//CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false);
//mSwitchpointTestVariabele->setValue(false, 1);
//CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false);
//mSwitchpointTestVariabele->setValue(true, 0);
//CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false);
//mSwitchpointTestVariabele->setValue(true, 1);
//CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), true);
}
}
但这给了我编译器错误:
| |In member function 'virtual void<unnamed>::TestSwitchpointTest1::RunImpl() const':|
| 9|error: expected primary-expression before 'private'|
| 9|error: expected ';' before 'private'|
| 9|error: expected primary-expression before 'public'|
| 9|error: expected ';' before 'public'|
| 9|error: a function-definition is not allowed here before '{' token|
| 9|error: a function-definition is not allowed here before '{' token|
|10|error: 'mSwitchpointTestVariabele' was not declared in this scope|
|10|error: 'CreateSwitchpoint' was not declared in this scope|
| |=== Build finished: 8 errors, 0 warnings ===|
我猜测问题是宏创建了需要测试的代码的一部分,并且在创建此部分之前执行了单元测试,但是从Aleguna的回复中我知道这不是问题。
我应该如何为这样的代码编写测试?
答案 0 :(得分:0)
我建议看一下实际生成的代码,因为你有编译问题,听起来好像宏没有生成你期望它的代码。如果你使用的是windows / VS,你可以使用“cl.exe / E”或g ++ / gcc,你可以使用“gcc -E source.cpp”运行预处理器并生成将要编译的c ++文件。
单元测试只能在代码成功编译后运行(并且可以像对单元测试任何其他代码一样进行测试)。
您的单元测试代码正在扩展到:
#include "UnitTest++.h"
#include "Classname.h"
namespace
{
TEST(SwitchpointTest1)
{
private:
comp::SwitchPoint* mSwitchpointObject;
public:
void setSwitchpointObject(bool val, unsigned short switchIdx = 0)
{
if(mSwitchpointObject) mSwitchpointObject->setValue(val,switchIdx);
}
bool getSwitchpointObject()
{
return (NULL == mSwitchpointObject ? false : mSwitchpointObject->getValue());
}
comp::SwitchPoint* getSwitchpointObject##Ptr()
{
return mSwitchpointObject;
}
mSwitchpointTestVariabele = CreateSwitchpoint("test switchpoint", 2);
CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), true);
//mSwitchpointTestVariabele->setValue(false, 0);
//CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false);
//mSwitchpointTestVariabele->setValue(false, 1);
//CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false);
//mSwitchpointTestVariabele->setValue(true, 0);
//CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false);
//mSwitchpointTestVariabele->setValue(true, 1);
//CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), true);
}
}
答案 1 :(得分:0)
错误很明显
In member function 'virtual void<unnamed>::TestSwitchpointTest1::RunImpl() const':
您无法在函数正文中使用private:
和public:
(包含在您的宏中)关键字。