Use CATCH_TRANSLATE_EXCEPTION for printing failures in *_THROWS_MATCHES
メンテナーはふだん 2 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- cpp
- 領域
- testing-qa
調査の方向性
まず、言及されているヘッダー catch_translate_exception.hpp、catch_matchers_exception.hpp、catch_matchers_templated.hpp を読み、次に CHECK_THROWS_MATCHES が失敗をどのようにフォーマットするかを追跡します。提供された reproducer を使って、その経路を CHECK_THROWS_WITH および CATCH_TRANSLATE_EXCEPTION と比較します。翻訳された例外メッセージと matcher の説明が、報告されている余分な空白も含めて一貫して表示されれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Description
Using CATCH_TRANSLATE_EXCEPTION is convenient, but sometimes we want to test the concrete type and message. Using *_THROWS_MATCHES does not use the function provided by CATCH_TRANSLATE_EXCEPTION and just uses std::exception::what() (as documented). The output of what() prefixes the output of describe() without quotes or other annotation making the error harder to read and understand.
It may be that this is intentional and thus neither a bug nor a wanted feature. If so then I wonder if there is some way to customize the generated error messages that I may have missed in the docs.
Additional context
We're using version 3.6.0 here, but I will test 3.7.0 although I don't see any relevant changes.
The following program produces the output below it. I tried my best to make it as simple but complete as possible. It defines two exceptions, one of which does not inherit from std::exception, along with three matchers: one old matcher for each type and new generic matcher for both.
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_translate_exception.hpp>
#include <catch2/matchers/catch_matchers_exception.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>
#include <catch2/matchers/catch_matchers.hpp>
#include <catch2/matchers/catch_matchers_templated.hpp>
#include <stdexcept>
#include <string>
class Error : public std::runtime_error
{
public:
Error(std::string what, std::string extra)
: std::runtime_error(what)
, extra(extra)
{}
std::string toString() const
{
return std::string(what()) + ": " + extra;
}
private:
std::string extra;
};
CATCH_TRANSLATE_EXCEPTION(Error const& ex)
{
return ex.toString();
}
class CustomError
{
public:
CustomError(std::string msg)
: message(msg)
{}
std::string toString() const
{
return std::string("I don't inherit: ") + message;
}
private:
std::string message;
};
CATCH_TRANSLATE_EXCEPTION(CustomError const& ex)
{
return ex.toString();
}
using namespace Catch::Matchers;
struct OldErrorMatcher : MatcherBase<Error>
{
OldErrorMatcher(std::string m)
: m(m)
{}
bool match(const Error &ex) const override
{
auto s = ex.toString();
auto pos = s.find(": ");
return s.substr(0, pos) == m;
}
std::string describe() const override
{
return "describe()";
}
std::string m;
};
struct OldCustomErrorMatcher : MatcherBase<CustomError>
{
OldCustomErrorMatcher(std::string m)
: m(m)
{}
bool match(const CustomError &ex) const override
{
auto s = ex.toString();
auto pos = s.find(": ");
return s.substr(0, pos) == m;
}
std::string describe() const override
{
return "describe()";
}
std::string m;
};
struct NewErrorMatcher : MatcherGenericBase
{
NewErrorMatcher(std::string m)
: m(m)
{}
template<typename Ex>
bool match(const Ex &ex) const
{
auto s = ex.toString();
auto pos = s.find(": ");
return s.substr(0, pos) == m;
}
std::string describe() const override
{
return "describe()";
}
std::string m;
};
void throwError()
{
throw Error("Oh no", "Bad luck");
}
void throwCustomError()
{
throw CustomError("Oh no");
}
TEST_CASE("Error") {
CHECK_THROWS_WITH(throwError(), StartsWith("Ahh yes"));
CHECK_THROWS_MATCHES(throwError(), Error, MessageMatches(StartsWith("Ahh yes")));
CHECK_THROWS_MATCHES(throwError(), Error, OldErrorMatcher("Ahh yes"));
CHECK_THROWS_MATCHES(throwError(), Error, NewErrorMatcher("Ahh yes"));
}
TEST_CASE("CustomError") {
CHECK_THROWS_WITH(throwCustomError(), StartsWith("Ahh yes"));
CHECK_THROWS_MATCHES(throwCustomError(), CustomError, OldCustomErrorMatcher("Ahh yes"));
CHECK_THROWS_MATCHES(throwCustomError(), CustomError, NewErrorMatcher("Ahh yes"));
}
The following is the output of the above program. Note the double space when using MessageMatcher (bug?).
Randomness seeded to: 1082738197
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tests is a Catch2 v3.6.0 host application.
Run with -? for options
-------------------------------------------------------------------------------
Error
-------------------------------------------------------------------------------
/tmp/catch2experiment/test.cpp:128
...............................................................................
/tmp/catch2experiment/test.cpp:129: FAILED:
CHECK_THROWS_WITH( throwError(), StartsWith("Ahh yes") )
with expansion:
"Oh no: Bad luck" starts with: "Ahh yes"
/tmp/catch2experiment/test.cpp:130: FAILED:
CHECK_THROWS_MATCHES( throwError(), Error, MessageMatches(StartsWith("Ahh yes")) )
with expansion:
Oh no matches "starts with: "Ahh yes""
/tmp/catch2experiment/test.cpp:131: FAILED:
CHECK_THROWS_MATCHES( throwError(), Error, OldErrorMatcher("Ahh yes") )
with expansion:
Oh no describe()
/tmp/catch2experiment/test.cpp:132: FAILED:
CHECK_THROWS_MATCHES( throwError(), Error, NewErrorMatcher("Ahh yes") )
with expansion:
Oh no describe()
-------------------------------------------------------------------------------
CustomError
-------------------------------------------------------------------------------
/tmp/catch2experiment/test.cpp:135
...............................................................................
/tmp/catch2experiment/test.cpp:136: FAILED:
CHECK_THROWS_WITH( throwCustomError(), StartsWith("Ahh yes") )
with expansion:
"I don't inherit: Oh no" starts with: "Ahh yes"
/tmp/catch2experiment/test.cpp:137: FAILED:
CHECK_THROWS_MATCHES( throwCustomError(), CustomError, OldCustomErrorMatcher("Ahh yes") )
with expansion:
{?} describe()
/tmp/catch2experiment/test.cpp:138: FAILED:
CHECK_THROWS_MATCHES( throwCustomError(), CustomError, NewErrorMatcher("Ahh yes") )
with expansion:
{?} describe()
===============================================================================
test cases: 2 | 2 failed
assertions: 7 | 7 failed
- 主要言語
- C++
- スター
- 21.6k
- フォーク
- 3.4k
- 平均マージ
- 3日 12時間
- マージ済み PR(30日)
- 5
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
catchorg/Catch2 のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
メンテナーはふだん 2 日以内に返信
-
catch_discover_tests passes all EXTRA_ARGS as one argument対応中かも @CyberKun が 11 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
catchorg/Catch2#3239 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
Not seeing some error messages from fakeit in jetbrains rider対応中かも @Amanpal9 が 21 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
catchorg/Catch2#3228 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
catch_discover_tests fails for Emscripten/WASM tests since v3.16.0対応中かも @nafiurahman00 が 8 日前に担当しました。 オープンExtras
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
catchorg/Catch2#3241 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
メンテナーはふだん 2 日以内に返信
catchorg/Catch2 の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
cp-algorithms/cp-algorithms#1774 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
telefonicaid/fiware-orion#4836 ·
メンテナーはふだん 1 日以内に返信
-
nixd exits at startup when its parent process belongs to another user対応中かも @conao3 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
nix-community/nixd#930 ·
メンテナーはふだん 2 日以内に返信
-
refactor topic-ui topic-weather
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
Command-Install Needs-Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
microsoft/winget-cli#6718 ·
メンテナーはふだん 2 日以内に返信