【问题标题】:How to get catch2 to print the compared C-style string's contents?如何让 catch2 打印比较的 C 风格字符串的内容?
【发布时间】:2021-07-30 12:46:54
【问题描述】:

我正在使用 catch2(最新版本 - 目前为 2.13.6),在我的测试用例中,我有一堆类似于以下内容的检查:

CHECK(!strcmp(my_str, "some literal string here"));

每个测试用例的文字都不同,my_str 的内容显然也是如此。

当这样的检查失败时,我得到的输出如下:

/path/to/test_source_file.cpp:123: FAILED:
  CHECK( !strcmp(my_str, "some literal string here") )
with expansion:
  false

但我没有打印出my_str 中的内容。让上面的内容也打印(部分)my_str 的内容的最佳方法是什么?

注意事项:

  • 您不能假设 my_str 是空终止的。
  • 代码必须相对简洁。
  • 我不希望将任何内容转换为 std::string,但如果您必须这样做,我不排除它。

【问题讨论】:

    标签: unit-testing string-comparison catch2


    【解决方案1】:

    我自己的 hacky 解决方案如下:

    #define PRINTING_CHECK(expected_, actual_)           \
    do {                                                 \
      INFO( "actual_   = " << '"' << actual_   << '"');  \
      INFO( "expected_ = " << '"' << expected_ << '"');  \
      CHECK(!strcmp(actual_, expected_));                \
    } while (false)                                      \
    

    但我希望可能有更优雅的东西。

    【讨论】:

      猜你喜欢
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多