Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/BDD.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Name> = true;
```

To write a project type, in these reports and in `info()`, `fail()` and composed section names, specialize
`webfront::bdd::Printer`:

Expand Down
6 changes: 6 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 27 additions & 5 deletions include/webfront/testing/bdd/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,29 @@ concept Matcher = requires(const M& matcher, std::string_view text) {
template <typename T>
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<Name> = true;
/// @endcode
template <typename T>
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 <typename Allocator>
void standardString(const std::basic_string<char, std::char_traits<char>, Allocator>&);

/// Whether a T is a C string, std::string or std::string_view, or a type isString<T> opts in.
template <typename T>
concept String = std::is_convertible_v<const T&, std::string_view>
&& (isString<std::remove_cv_t<T>> || std::is_same_v<std::decay_t<T>, char*> || std::is_same_v<std::decay_t<T>, const char*>
|| std::is_base_of_v<std::string_view, T> || 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;
Expand All @@ -103,7 +124,7 @@ concept Customized = requires(std::string& text, const T& value) { Printer<T>::a

/// A range whose elements are not characters, iterated without <ranges>.
template <typename T>
concept Sequence = !std::is_convertible_v<const T&, std::string_view> && requires(const T& range) {
concept Sequence = !String<T> && requires(const T& range) {
range.begin() != range.end();
*range.begin();
};
Expand All @@ -112,7 +133,7 @@ concept Sequence = !std::is_convertible_v<const T&, std::string_view> && require
/// template; a range whose elements are of its own type (std::filesystem::path) is not written as a range.
template <typename T>
inline constexpr bool printable = [] {
if constexpr (Customized<T> || std::is_convertible_v<const T&, std::string_view> || std::is_arithmetic_v<T> || std::is_enum_v<T>)
if constexpr (Customized<T> || String<T> || std::is_arithmetic_v<T> || std::is_enum_v<T>)
return true;
else if constexpr (Sequence<T>) {
using Element = std::remove_cvref_t<decltype(*std::declval<const T&>().begin())>;
Expand Down Expand Up @@ -142,7 +163,7 @@ template <typename T>
void append(std::string& text, const T& value) {
if constexpr (Customized<T>)
Printer<T>::append(text, value);
else if constexpr (std::is_convertible_v<const T&, std::string_view>)
else if constexpr (String<T>)
text += nullString(value) ? std::string_view{"nullptr"} : std::string_view{value};
else if constexpr (std::is_same_v<T, bool>)
text += value ? "true" : "false";
Expand Down Expand Up @@ -514,12 +535,13 @@ concept PlainInteger = std::is_integral_v<T> && !std::is_same_v<std::remove_cv_t

/// Whether two values are equal: two integers are compared by value whatever their signedness, so that
/// checkEqual(items.size(), 3) neither warns nor converts -1 to a large unsigned number, and two strings by their
/// characters, so that two `const char*` are not compared as pointers. A null C string equals only another one.
/// characters, so that two `const char*` are not compared as pointers. A null C string equals only another one. A type that
/// merely converts to std::string_view is not a string (isString): it is compared with `==`.
template <typename L, typename R>
bool equal(const L& left, const R& right) {
if constexpr (PlainInteger<L> && PlainInteger<R>)
return std::cmp_equal(left, right);
else if constexpr (std::is_convertible_v<const L&, std::string_view> && std::is_convertible_v<const R&, std::string_view>) {
else if constexpr (String<L> && String<R>) {
if (nullString(left) || nullString(right))
return nullString(left) && nullString(right);
return std::string_view{left} == std::string_view{right};
Expand Down
48 changes: 48 additions & 0 deletions test/unit/BddSelfTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <thread>
#include <type_traits>
#include <vector>

using namespace webfront::bdd;
Expand Down Expand Up @@ -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 <typename T>
requires std::is_same_v<T, std::string_view>
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<const Json&, std::string_view>);
static_assert(!webfront::bdd::impl::String<Json>);
static_assert(webfront::bdd::impl::String<const char*> && webfront::bdd::impl::String<char[4]> && webfront::bdd::impl::String<std::string>
&& webfront::bdd::impl::String<std::string_view>);
// A project's string type opts in.
struct Name {
std::string value;
operator std::string_view() const {
return value;
}
};

} // namespace

Expand All @@ -306,6 +335,16 @@ struct webfront::bdd::Printer<Color> {
}
};

template <>
struct webfront::bdd::Printer<Json> {
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<Name> = true;

namespace {

// approx() refuses the numbers a double cannot always hold: beyond 2^53 a different integer would compare equal.
Expand Down Expand Up @@ -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<const char*>(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");
}};

Expand Down Expand Up @@ -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<const char*>(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);
Expand Down Expand Up @@ -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) {
Expand Down
Loading