0

The doctest C++ test framework has a DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING configuration option. This option allows me to write

char * tls_password;

// ...

CHECK(tsl_password == "swordfish");

That is, use == to compare C strings without having to use the more verbose C-style

CHECK(strcmp(tsl_password, "swordfish") == 0);

How can I achieve the effect of DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING in Catch2? That is, have boilerplate-free assertions that compare to C-style string constants.

user7610
  • 25,267
  • 15
  • 124
  • 150
  • 1
    IMO it is better to: `using namespace std::literals` (or more specific version) and then just use suffix int check: `CHECK(tsl_password == "swordfish"sv);`. Changing behavior of build in operator is a code smell. Or Better use matcher: `CHECK_THAT(tsl_password, Equals("swordfish"));` – Marek R Sep 26 '22 at 15:48
  • @MarekR What do I do when I want to compare two `char *` variables? Create std::string from one of them? Btw, if you post your comment as answer, I'd accept it – user7610 Sep 26 '22 at 16:16
  • 1
    `std::string` has `operator==` for handling `const char*` so one side, so it is enough to do conversion for one argument. Also if you use C++17 `std::string_view` is cheaper. – Marek R Sep 26 '22 at 16:22

0 Answers0