使用CPPUnit打印样式断言
Printf-Style Assertions using CPPUnit
CPPUnit是否具有允许我进行printf
风格断言的任何功能?例如:
CPPUNIT_ASSERT("Actual size: %d", p->GetSize(), p->GetSize() == 0);
我知道这不是一个有效的CPPUNIT_SSERT——我只是用它作为一个例子。
我找到了CPPUNIT_ASSERT_MESSAGE(message,condition)
,它需要一个字符串,然后是要计算的条件,但没有幸运地将值输入到断言中。
您应该能够执行以下操作:
#define CPPUNIT_ASSERT_STREAM(MSG, CONDITION)
do {
std::ostringstream oss;
CPPUNIT_ASSERT_MESSAGE(
static_cast<std::ostringstream &>(oss << MSG).str(),
CONDITION);
} while (0)
CPPUNIT_ASSERT_STREAM("Actual size: " << p->GetSize(), p->GetSize() == 0);
上述宏也可以与Boost.format:组合使用
CPPUNIT_ASSERT_STREAM(boost::format("Actual size: %d") % p->GetSize(),
p->GetSize() == 0);
我使用自定义断言打印所有需要的信息:
#ifdef NDEBUG
#define ASSERT(v, msg)
#else
#define ASSERT(v, msg)
if (v) {} else {
std::cerr << __FILE__ << ":" << __LINE__ << " assertion failed: "
<< #v << " = " << (v) << "n" << msg << std::endl;
abort();
}
#endif
使用:
#include <iostream>
...
ASSERT( p->GetSize() == 0, p->GetSize() );
或
ASSERT( p->GetSize() == 0, "Actual size: " << p->GetSize() << " Actual size * 2: " << p->GetSize()*2 );
请使用CPPUNIT_ASSERT_EQUAL
,它采用实际值和期望值。这几乎消除了格式化字符串的需要,因为实际值和预期值将打印在失败消息中。
您还可以复制此宏的实现及其调用的函数,并为其他类型的比较添加其他宏。我尝试过的另一个想法是使用运算符重载来捕获表达式中的值。This(ab)使用运算符重载来捕获表达式,但它看起来可能有点太粗糙了。我把它包括在内,让你知道什么是可能的,但不建议使用:
#include <iostream>
#include <sstream>
class ExpressionPrinter
{
public:
std::ostringstream result;
template<typename T> ExpressionPrinter& operator<<(const T& other)
{
result << other;
return *this;
}
template<typename T> ExpressionPrinter& operator<(const T& other)
{
result << " < ";
result << other;
return *this;
}
template<typename T> ExpressionPrinter& operator<=(const T& other)
{
result << " <= ";
result << other;
return *this;
}
template<typename T> ExpressionPrinter& operator>(const T& other)
{
result << " > ";
result << other;
return *this;
}
template<typename T> ExpressionPrinter& operator>=(const T& other)
{
result << " >= ";
result << other;
return *this;
}
template<typename T> ExpressionPrinter& operator==(const T& other)
{
result << " == ";
result << other;
return *this;
}
template<typename T> ExpressionPrinter& operator!=(const T& other)
{
result << " != ";
result << other;
return *this;
}
};
#define ASSERT(X) doAssert((X), (ExpressionPrinter() << X));
void doAssert(bool result, const ExpressionPrinter& message)
{
std::cout << "Result: " << result << ", Expression: " << message.result.str() << std::endl;
}
int main()
{
int i = 1, j = 2;
ASSERT(i < j);
return 0;
}
#include <iostream>
...
ASSERT( p->GetSize() == 0, p->GetSize() );