Skip to content

Commit

Permalink
unit_test improvements:
Browse files Browse the repository at this point in the history
* New overload of fail() specifies file and line
* BEAST_EXPECTS only evaluates the reason string on a failure
  - This speeds up tests that call BEAST_EXPECTS
  • Loading branch information
vinniefalco committed Oct 24, 2016
1 parent d7c4c80 commit 3bb4b5f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

--------------------------------------------------------------------------------

Expand Down
74 changes: 53 additions & 21 deletions extras/beast/unit_test/suite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@
namespace beast {
namespace unit_test {

namespace detail {

template<class String>
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
Expand Down Expand Up @@ -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<class String>
void
fail(String const& reason, char const* file, int line);

template<class = void>
void
fail(std::string const& reason = "");
/** @} */

/** Evaluate a test condition.
Expand All @@ -206,25 +237,25 @@ class suite
bool
expect(Condition const& shouldBeTrue)
{
return expect(shouldBeTrue, {});
return expect(shouldBeTrue, "");
}

template<class Condition>
template<class Condition, class String>
bool
expect(Condition const& shouldBeTrue, std::string const& reason);
expect(Condition const& shouldBeTrue, String const& reason);

template<class Condition>
bool
expect(Condition const& shouldBeTrue,
char const* file, int line)
{
return expect(shouldBeTrue, {}, file, line);
return expect(shouldBeTrue, "", file, line);
}

template<class Condition>
template<class Condition, class String>
bool
expect(Condition const& shouldBeTrue,
std::string const& reason, char const* file, int line);
String const& reason, char const* file, int line);
/** @} */

//
Expand Down Expand Up @@ -402,11 +433,11 @@ operator()(runner& r)
}
}

template<class Condition>
template<class Condition, class String>
bool
suite::
expect(
Condition const& shouldBeTrue, std::string const& reason)
Condition const& shouldBeTrue, String const& reason)
{
if(shouldBeTrue)
{
Expand All @@ -417,26 +448,18 @@ expect(
return false;
}

template<class Condition>
template<class Condition, class String>
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;
}

Expand Down Expand Up @@ -535,6 +558,14 @@ fail(std::string const& reason)
}
}

template<class String>
void
suite::
fail(String const& reason, char const* file, int line)
{
fail(detail::make_reason(reason, file, line));
}

inline
void
suite::
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3bb4b5f

Please sign in to comment.