CPPUnit是否有任何允许我进行printf
样式断言的功能?例如:
CPPUNIT_ASSERT("Actual size: %d", p->GetSize(), p->GetSize() == 0);
我知道这不是一个有效的CPPUNIT_ASSERT - 我只是以此为例。
我发现CPPUNIT_ASSERT_MESSAGE(message,condition)
接受一个字符串,然后条件进行评估,但没有运气将值传入断言。
答案 0 :(得分:6)
你应该可以这样做:
#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);
答案 1 :(得分:0)
请使用CPPUNIT_ASSERT_EQUAL
,它取实际值和预期值。这几乎消除了格式化字符串的需要,因为实际值和期望值将打印在失败消息中。
您还可以复制此宏的实现及其调用的函数,并为其他类型的比较添加其他宏。我尝试过的另一个想法是使用运算符重载来捕获表达式中的值。这个(ab)使用运算符重载来捕获表达式,但似乎有点过于hackish。我已经把它包括在内,让你知道什么是可能的,但不建议使用它:
#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;
}
答案 2 :(得分:0)
我使用selfdefined assert打印所有需要的信息:
#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 );
答案 3 :(得分:0)
#include <iostream>
...
ASSERT( p->GetSize() == 0, p->GetSize() );