Skip to content

checkEqual compares only real strings by their characters [#739] - #744

Open
ambroise-leclerc wants to merge 1 commit into
developfrom
739-bdd-real-strings
Open

ambroise-leclerc wants to merge 1 commit into
developfrom
739-bdd-real-strings

Conversation

@ambroise-leclerc

Copy link
Copy Markdown
Collaborator

Closes #739.

Problem

impl::equal and impl::append (bdd/Core.hpp) took the string path for any type with std::is_convertible_v<const T&, std::string_view>. With MSVC, nlohmann::json has an implicit, templated conversion to std::string_view, so checkEqual(json, "text"), checkEqual(json, json) and checkEqual(json, std::string) converted the JSON value and threw type_error.302 instead of comparing with ==.

Changes

  • A string is now a C string (char*, const char*, a character array), a std::string or a std::string_view, or a class derived from them (impl::String). A type that merely converts to std::string_view is compared with == and written by its Printer (or {?} in the operands of a failure).
  • A project's own string type opts in with template <> inline constexpr bool webfront::bdd::isString<Name> = true;.
  • docs/BDD.md documents the rule and the opt-in; changelog: a Breaking entry (such a type is no longer written as text by info(), fail() and checkEqual unless it opts in or has a Printer) and a Fixed entry.

Tests

  • BddSelfTest gains a Json type that mimics nlohmann::json with MSVC (a templated implicit conversion to std::string_view that throws unless it holds a string): checkEqual(object, object), checkEqual(object, "text"), checkEqual(object, std::string) compare with ==; a failed checkEqual(Json{...}, "text") is written by its Printer; a type opting in with isString is compared by its characters; static checks of impl::String.
  • Without the fix, the self-test fails (the conversion throws); with it, it passes.
  • CEF-off build and full CTest suite: 768/768 passed (the CUDA case skipped).

A type that merely converts to std::string_view, such as nlohmann::json with
MSVC, took the string path of impl::equal and impl::append: checkEqual(json,
"text") converted the JSON value and threw instead of comparing with ==.

The string path is now taken for C strings, character arrays, std::string,
std::string_view and the classes derived from them, or a type that opts in
with webfront::bdd::isString. Other types are compared with == and written by
their Printer.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

webfront::bdd: checkEqual takes a nlohmann::json for a string with MSVC

1 participant