diff --git a/CHANGELOG.md b/CHANGELOG.md index 01066b809e..f3980aac43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ 1.0.0-b18 * Clean up message docs +* unit_test::suite fixes: + - New overload of fail() specifies file and line + - BEAST_EXPECTS only evaluates the reason string on a failure -------------------------------------------------------------------------------- diff --git a/extras/beast/unit_test/suite.hpp b/extras/beast/unit_test/suite.hpp index ab12f1c6e1..54aab4ad9e 100644 --- a/extras/beast/unit_test/suite.hpp +++ b/extras/beast/unit_test/suite.hpp @@ -17,6 +17,27 @@ namespace beast { namespace unit_test { +namespace detail { + +template +static +std::string +make_reason(String const& reason, + char const* file, int line) +{ + std::string s(reason); + if(! s.empty()) + s.append(": "); + namespace fs = boost::filesystem; + s.append(fs::path{file}.filename().string()); + s.append("("); + s.append(std::to_string(line)); + s.append(")"); + return s; +} + +} // detail + class thread; enum abort_t @@ -178,11 +199,21 @@ class suite /** Record a failure. - @param reason + @param reason Optional text added to the output on a failure. + + @param file The source code file where the test failed. + + @param line The source code line number where the test failed. */ + /** @{ */ + template + void + fail(String const& reason, char const* file, int line); + template void fail(std::string const& reason = ""); + /** @} */ /** Evaluate a test condition. @@ -206,25 +237,25 @@ class suite bool expect(Condition const& shouldBeTrue) { - return expect(shouldBeTrue, {}); + return expect(shouldBeTrue, ""); } - template + template bool - expect(Condition const& shouldBeTrue, std::string const& reason); + expect(Condition const& shouldBeTrue, String const& reason); template bool expect(Condition const& shouldBeTrue, char const* file, int line) { - return expect(shouldBeTrue, {}, file, line); + return expect(shouldBeTrue, "", file, line); } - template + template bool expect(Condition const& shouldBeTrue, - std::string const& reason, char const* file, int line); + String const& reason, char const* file, int line); /** @} */ // @@ -402,11 +433,11 @@ operator()(runner& r) } } -template +template bool suite:: expect( - Condition const& shouldBeTrue, std::string const& reason) + Condition const& shouldBeTrue, String const& reason) { if(shouldBeTrue) { @@ -417,26 +448,18 @@ expect( return false; } -template +template bool suite:: expect(Condition const& shouldBeTrue, - std::string const& reason, char const* file, int line) + String const& reason, char const* file, int line) { if(shouldBeTrue) { pass(); return true; } - std::string s; - if(! reason.empty()) - { - s += reason; - s += " "; - } - s += boost::filesystem::path{file}.filename().string() + - "(" + std::to_string(line) + ")"; - fail(s); + fail(detail::make_reason(reason, file, line)); return false; } @@ -535,6 +558,14 @@ fail(std::string const& reason) } } +template +void +suite:: +fail(String const& reason, char const* file, int line) +{ + fail(detail::make_reason(reason, file, line)); +} + inline void suite:: @@ -583,7 +614,8 @@ run(runner& r) If the condition is false, the file and line number are reported. */ -#define BEAST_EXPECTS(cond, reason) expect(cond, reason, __FILE__, __LINE__) +#define BEAST_EXPECTS(cond, reason) ((cond) ? (pass(), true) : \ + (fail((reason), __FILE__, __LINE__), false)) #endif } // unit_test