Enumerator generator

未关闭
#3,000 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
42/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
cpp
领域
testing

调研方向

从 Catch2 的 GENERATE 和 GENERATE_REF 宏以及 range generator 开始,然后将它们的行为与遍历 expected_results 的循环进行比较。跟踪生成的值如何暴露给 CAPTURE,并确定现有的 generator API 是否能够在不手动索引的情况下同时提供一个元素及其索引;完成的标准是记录或实现适当的方法。

由索引模型根据 Issue 内容生成。

描述

I'm using fff to mock a call and I have a std::vector of expected arguments for the mocked function. The way fff works, I need a generator that works like the Python enumerate statement: on each iteration it returns the iteration number and an element of my vector, so my test goes something like this:

REQUIRE(my_function_fake.arg0_history[i] == val_a);
REQUIRE(my_function_fake.arg1_history[i] == val_b);

I need both the vector element and the index. I'm trying to understand how to best go about it. My first try was to simply iterate over the vector and use CAPTURE:

uint32_t factorial(uint32_t number)
{
    return number <= 1 ? number : factorial(number - 1) * number;
}

TEST_CASE("Factorials are computed", "[factorial]")
{
    std::vector<std::tuple<uint32_t, uint32_t>> expected_results = {
        {1, 1},
        {2, 2},
        {3, 6},
        {10, 3'628'800+1},  // Bad value
    };

    for( int i = -1; const auto& v : expected_results ) {
        ++i;
        auto& [input, expected] = v;
        CAPTURE(i, input, expected);
        REQUIRE(factorial(input) == expected);
    }

This works great, I get a nice error message with the particular values that failed:

test.cc:50: FAILED:
  REQUIRE( factorial(input) == expected )
with expansion:
  3628800 (0x375f00) == 3628801 (0x375f01)
with messages:
  i := 3
  input := 10
  expected := 3628801 (0x375f01)

Using the range generator seems a bit overly complicated:

    size_t i = GENERATE_REF(Catch::Generators::range(static_cast<size_t>(0), expected_results.size()));
    auto [input, expected] = expected_results[i];
    CAPTURE(i, input, expected);
    REQUIRE(factorial(input) == expected);

Is there a way to use one of the GENERATE macros for this?

Thanks!

主要语言
C++
星标
21.6k
派生
3.4k
平均合并
3 天 12 小时
30 天内合并 PR
5

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

catchorg/Catch2 的其他 Issue

查看 catchorg/Catch2 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。