diff --git a/docs/BDD.md b/docs/BDD.md index 846f5955..badff9c4 100644 --- a/docs/BDD.md +++ b/docs/BDD.md @@ -102,6 +102,16 @@ signedness, so `checkEqual(items.size(), 3)` neither warns nor equates `-1` with characters (two `const char*` are not compared as pointers; a null one equals only another null one and is written `nullptr`); any other pair uses `left == right`. +A string is a `const char*`, a character array, a `std::string` or a `std::string_view` (or a class derived from them). A type +that only converts to `std::string_view`, such as a `nlohmann::json` with MSVC, is not one: it is compared with `==` and written +by its `Printer`, so that `checkEqual(json, "text")` never converts a JSON object to a string. A project's own string type +opts in: + +```cpp +template <> +inline constexpr bool webfront::bdd::isString = true; +``` + To write a project type, in these reports and in `info()`, `fail()` and composed section names, specialize `webfront::bdd::Printer`: diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index b9af4e69..5f85702b 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,12 @@ these entries stays in the GitHub releases and the Git history. ## Unreleased +### Breaking +- A type that converts to `std::string_view` without being a string is no longer written as text by `info()`, `fail()` and `checkEqual`: specialize `webfront::bdd::isString` (or `Printer`) for it (#739, `docs/BDD.md`). + +### Fixed +- Compare a type that merely converts to `std::string_view`, such as `nlohmann::json` with MSVC, with `==` in `checkEqual` and `requireEqual`, and write it by its `Printer`, instead of converting it and throwing (#739, `docs/BDD.md`). + ## [1.3.0] - 2026-11-28 WebFront 1.3 ships with developer environment 1.3.0. The developer environment gains inspectors of the running application diff --git a/include/webfront/testing/bdd/Core.hpp b/include/webfront/testing/bdd/Core.hpp index 47e8dfe5..cf1cc504 100644 --- a/include/webfront/testing/bdd/Core.hpp +++ b/include/webfront/testing/bdd/Core.hpp @@ -81,8 +81,29 @@ concept Matcher = requires(const M& matcher, std::string_view text) { template struct Printer; +/// Whether checkEqual() compares and writes a T as a string. Strings are `const char*`, character arrays, std::string and +/// std::string_view (and the classes derived from them); a project's own string type, convertible to std::string_view, +/// opts in by specializing this variable to true. Another type convertible to std::string_view, such as a JSON value +/// that converts to a string only when it holds one, is compared with `==` and written by its Printer. +/// @code +/// template <> inline constexpr bool webfront::bdd::isString = true; +/// @endcode +template +inline constexpr bool isString = false; + namespace impl { +/// Accepts a std::string or a class derived from it, never a type that only converts to one: a template parameter is +/// deduced without conversions. +template +void standardString(const std::basic_string, Allocator>&); + +/// Whether a T is a C string, std::string or std::string_view, or a type isString opts in. +template +concept String = std::is_convertible_v + && (isString> || std::is_same_v, char*> || std::is_same_v, const char*> + || std::is_base_of_v || requires(const T& value) { impl::standardString(value); }); + /// A number written by snprintf, with '.' as its decimal separator whatever the C locale a test may have changed. inline std::string withPoint(std::string number) { std::string_view point = std::localeconv()->decimal_point; @@ -103,7 +124,7 @@ concept Customized = requires(std::string& text, const T& value) { Printer::a /// A range whose elements are not characters, iterated without . template -concept Sequence = !std::is_convertible_v && requires(const T& range) { +concept Sequence = !String && requires(const T& range) { range.begin() != range.end(); *range.begin(); }; @@ -112,7 +133,7 @@ concept Sequence = !std::is_convertible_v && require /// template; a range whose elements are of its own type (std::filesystem::path) is not written as a range. template inline constexpr bool printable = [] { - if constexpr (Customized || std::is_convertible_v || std::is_arithmetic_v || std::is_enum_v) + if constexpr (Customized || String || std::is_arithmetic_v || std::is_enum_v) return true; else if constexpr (Sequence) { using Element = std::remove_cvref_t().begin())>; @@ -142,7 +163,7 @@ template void append(std::string& text, const T& value) { if constexpr (Customized) Printer::append(text, value); - else if constexpr (std::is_convertible_v) + else if constexpr (String) text += nullString(value) ? std::string_view{"nullptr"} : std::string_view{value}; else if constexpr (std::is_same_v) text += value ? "true" : "false"; @@ -514,12 +535,13 @@ concept PlainInteger = std::is_integral_v && !std::is_same_v bool equal(const L& left, const R& right) { if constexpr (PlainInteger && PlainInteger) return std::cmp_equal(left, right); - else if constexpr (std::is_convertible_v && std::is_convertible_v) { + else if constexpr (String && String) { if (nullString(left) || nullString(right)) return nullString(left) && nullString(right); return std::string_view{left} == std::string_view{right}; diff --git a/test/unit/BddSelfTest.cpp b/test/unit/BddSelfTest.cpp index 2e6638c2..d046ea4a 100644 --- a/test/unit/BddSelfTest.cpp +++ b/test/unit/BddSelfTest.cpp @@ -26,7 +26,9 @@ #include #include #include +#include #include +#include #include using namespace webfront::bdd; @@ -296,6 +298,33 @@ struct Opaque { int value; bool operator==(const Opaque&) const = default; }; +// A value that converts to any string type but throws unless it holds a string, as nlohmann::json does with MSVC (#739). +struct Json { + bool holdsString; + std::string text; + template + requires std::is_same_v + operator T() const { + if (!holdsString) + throw std::runtime_error{"type must be string, but is object"}; + return text; + } + bool operator==(const Json&) const = default; + bool operator==(std::string_view other) const { + return holdsString && text == other; + } +}; +static_assert(std::is_convertible_v); +static_assert(!webfront::bdd::impl::String); +static_assert(webfront::bdd::impl::String && webfront::bdd::impl::String && webfront::bdd::impl::String + && webfront::bdd::impl::String); +// A project's string type opts in. +struct Name { + std::string value; + operator std::string_view() const { + return value; + } +}; } // namespace @@ -306,6 +335,16 @@ struct webfront::bdd::Printer { } }; +template <> +struct webfront::bdd::Printer { + static void append(std::string& text, const Json& json) { + text += json.holdsString ? json.text : std::string{"a JSON object"}; + } +}; + +template <> +inline constexpr bool webfront::bdd::isString = true; + namespace { // approx() refuses the numbers a double cannot always hold: beyond 2^53 a different integer would compare equal. @@ -379,6 +418,13 @@ const Scenario equalHolds{"checkEqual holds", [] { require(!webfront::bdd::impl::equal(none, "x")); require(!webfront::bdd::impl::equal(std::string{"x"}, none)); requireEqual(none, static_cast(nullptr)); + // A value that only converts to a string is compared with ==, not converted (#739). + Json object{false, {}}; + requireEqual(object, object); + require(!webfront::bdd::impl::equal(object, "text")); + require(!webfront::bdd::impl::equal(object, std::string{"text"})); + requireEqual(Json{true, "text"}, "text"); + requireEqual(Name{"abc"}, "abc"); trace.push_back("equal"); }}; @@ -420,6 +466,7 @@ const Scenario equalFails{"checkEqual reports its operands", [] { checkEqual(Opaque{1}, Opaque{2}); checkEqual(std::string{"left text"}, "right text"); checkEqual(static_cast(nullptr), "x"); + checkEqual(Json{false, {}}, "text"); // written by its Printer, not converted (#739) checkEqual(1.0L, std::nextafter(1.0L, 2.0L)); // written with the precision of a long double trace.push_back("after check"); requireEqual(2, 3); @@ -650,6 +697,7 @@ int main() { "left: {?}\n right: {?}", "left: left text\n right: right text", "left: nullptr\n right: x", + "left: a JSON object\n right: text", "left: 1\n right: 1.0000000000000", "left: 2\n right: 3"}) if (xml.find(part) == std::string::npos) {