Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Improve error messages in "robot-simulator"

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

还没有人认领这个 Issue。

评估

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

调研方向

从 robot_simulator_test.cpp 和现有的 Catch2 测试设置开始。查阅 Catch2 的 StringMaker 和 CATCH_REGISTER_ENUM 文档,然后添加建议的诊断支持并运行 robot-simulator 测试;当失败的比较显示成对的值和 Bearing 名称,而不是占位符或整数时,就算完成。

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

描述

The tests in the exercise "robot-simulator" compare the result of the member functions get_position() and get_bearing() with the operator== like this:

TEST_CASE("A_robots_is_created_with_a_position_and_a_direction")
{
    const Robot r;

    const std::pair<int, int> expected_robot_position{0, 0};
    REQUIRE(expected_robot_position == r.get_position());
    REQUIRE(Bearing::NORTH == r.get_bearing());
}

But Catch2 does not know how to print a std::pair, and it prints an enum or enum class like an integer:

-------------------------------------------------------------------------------
A_robots_is_created_with_a_position_and_a_direction
-------------------------------------------------------------------------------
/home/user/exercism/cpp/robot-simulator/robot_simulator_test.cpp:13
...............................................................................

/home/user/exercism/cpp/robot-simulator/robot_simulator_test.cpp:18: FAILED:
  REQUIRE( expected_robot_position == r.get_position() )
with expansion:
  {?} == {?}


-------------------------------------------------------------------------------
A_robots_is_created_with_a_position_and_a_direction
-------------------------------------------------------------------------------
/home/user/exercism/cpp/robot-simulator/robot_simulator_test.cpp:13
...............................................................................

/home/user/exercism/cpp/robot-simulator/robot_simulator_test.cpp:19: FAILED:
  REQUIRE( Bearing::NORTH == r.get_bearing() )
with expansion:
  0 == 2

That's not really helpful.


Catch2 has the a StringMaker for printing custom classes (see the documentation), and it has the macro CATCH_REGISTER_ENUM() for better error messages when working with enums (see the documentation).

By adding a few lines somewhere at the beginning of robot_simulator_test.cpp

// for better error messages
namespace Catch
{
    template <typename T1, typename T2>
    struct StringMaker<std::pair<T1, T2>>
    {
        static std::string convert(const std::pair<T1, T2>& value)
        {
            std::string result = "std::pair{";
            result += StringMaker<T1>::convert(value.first);
            result += ", ";
            result += StringMaker<T2>::convert(value.second);
            result += '}';
            return result;
        }
    };
}
CATCH_REGISTER_ENUM(robot_simulator::Bearing,
        robot_simulator::Bearing::NORTH,
        robot_simulator::Bearing::WEST,
        robot_simulator::Bearing::SOUTH,
        robot_simulator::Bearing::EAST)

we would get better error messages:

-------------------------------------------------------------------------------
A_robots_is_created_with_a_position_and_a_direction
-------------------------------------------------------------------------------
/home/user/exercism/cpp/robot-simulator/robot_simulator_test.cpp:36
...............................................................................

/home/user/exercism/cpp/robot-simulator/robot_simulator_test.cpp:41: FAILED:
  REQUIRE( expected_robot_position == r.get_position() )
with expansion:
  std::pair{0, 0} == std::pair{0, 1}


-------------------------------------------------------------------------------
A_robots_is_created_with_a_position_and_a_direction
-------------------------------------------------------------------------------
/home/user/exercism/cpp/robot-simulator/robot_simulator_test.cpp:36
...............................................................................

/home/user/exercism/cpp/robot-simulator/robot_simulator_test.cpp:42: FAILED:
  REQUIRE( Bearing::NORTH == r.get_bearing() )
with expansion:
  NORTH == SOUTH

AFAIK there are only two possible problems:

  1. robot_simulator_test.cpp becomes more complex. But IMHO those 22 lines can be ignored.

  2. That would effectively enforce the use of enum or enum class for Bearing. IMHO that's not a problem for us because we want idiomatic solutions, and that's enum or better enum class.

IMHO the benefits outweigh these problems.
What do you folks think?

主要语言
C++
星标
291
派生
244
平均合并
17 分钟
30 天内合并 PR
1

贡献指南

这个仓库没有索引到贡献指南

从这里开始

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

exercism/cpp 的其他 Issue

查看 exercism/cpp 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

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