简体   繁体   English

使用CPPUnit的Printf样式断言

[英]Printf-Style Assertions using CPPUnit

Does CPPUnit have any functionality that would allow me to do printf -style assertions? CPPUnit是否具有允许我执行printf style断言的任何功能? For example: 例如:

CPPUNIT_ASSERT("Actual size: %d", p->GetSize(), p->GetSize() == 0);

I know this is not a valid CPPUNIT_ASSERT - I am just using this as an example. 我知道这不是一个有效的CPPUNIT_ASSERT - 我只是以此为例。

I found CPPUNIT_ASSERT_MESSAGE(message,condition) which takes a string and then the condition to evaluate but no luck getting the value into the assert. 我找到了CPPUNIT_ASSERT_MESSAGE(message,condition) ,它接受一个字符串,然后条件进行评估,但没有运气将值输入断言。

You should be able to do something like this: 你应该可以做这样的事情:

#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);

The above macro can also be combined with Boost.format : 上面的宏也可以与Boost.format结合使用:

CPPUNIT_ASSERT_STREAM(boost::format("Actual size: %d") % p->GetSize(),
                      p->GetSize() == 0);

Please use CPPUNIT_ASSERT_EQUAL , which takes an actual and expected value. 请使用CPPUNIT_ASSERT_EQUAL ,它取实际值和期望值。 This pretty much eliminates the need for formatting a string, as the actual and expected values will be printed in the failure message. 这几乎消除了格式化字符串的需要,因为实际值和期望值将打印在失败消息中。

You could also copy the implementation of this macro and the function it invokes and add additional macros for other types of comparisons. 您还可以复制此宏的实现及其调用的函数,并为其他类型的比较添加其他宏。 Another idea I tried out would be to use operator overloading to capture the value out of an expression. 我尝试过的另一个想法是使用运算符重载来捕获表达式中的值。 This (ab)uses operator overloading to capture the expression, but it seems perhaps a bit too hackish. 这个(ab)使用运算符重载来捕获表达式,但似乎有点过于hackish。 I have included it to give you an idea of what is possible, but would not recommend its use: 我已将其包含在内,以便您了解可能的内容,但不建议使用它:

#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;
}

I use selfdefined assert to print all needed info: 我使用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

To use: 使用:

#include <iostream>
...
ASSERT( p->GetSize() == 0, p->GetSize() );

or 要么

ASSERT( p->GetSize() == 0, "Actual size: " << p->GetSize() << " Actual size * 2: " << p->GetSize()*2 );
#include <iostream>
...
ASSERT( p->GetSize() == 0, p->GetSize() );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM