suite.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_UNIT_TEST_SUITE_HPP
  10. #define BOOST_BEAST_UNIT_TEST_SUITE_HPP
  11. #include <boost/beast/_experimental/unit_test/runner.hpp>
  12. #include <boost/throw_exception.hpp>
  13. #include <ostream>
  14. #include <sstream>
  15. #include <string>
  16. namespace boost {
  17. namespace beast {
  18. namespace unit_test {
  19. namespace detail {
  20. template<class String>
  21. std::string
  22. make_reason(String const& reason,
  23. char const* file, int line)
  24. {
  25. std::string s(reason);
  26. if(! s.empty())
  27. s.append(": ");
  28. char const* path = file + strlen(file);
  29. while(path != file)
  30. {
  31. #ifdef _MSC_VER
  32. if(path[-1] == '\\')
  33. #else
  34. if(path[-1] == '/')
  35. #endif
  36. break;
  37. --path;
  38. }
  39. s.append(path);
  40. s.append("(");
  41. s.append(std::to_string(line));
  42. s.append(")");
  43. return s;
  44. }
  45. } // detail
  46. class thread;
  47. enum abort_t
  48. {
  49. no_abort_on_fail,
  50. abort_on_fail
  51. };
  52. /** A testsuite class.
  53. Derived classes execute a series of testcases, where each testcase is
  54. a series of pass/fail tests. To provide a unit test using this class,
  55. derive from it and use the BOOST_BEAST_DEFINE_UNIT_TEST macro in a
  56. translation unit.
  57. */
  58. class suite
  59. {
  60. private:
  61. bool abort_ = false;
  62. bool aborted_ = false;
  63. runner* runner_ = nullptr;
  64. // This exception is thrown internally to stop the current suite
  65. // in the event of a failure, if the option to stop is set.
  66. struct abort_exception : public std::exception
  67. {
  68. char const*
  69. what() const noexcept override
  70. {
  71. return "test suite aborted";
  72. }
  73. };
  74. template<class CharT, class Traits, class Allocator>
  75. class log_buf
  76. : public std::basic_stringbuf<CharT, Traits, Allocator>
  77. {
  78. suite& suite_;
  79. public:
  80. explicit
  81. log_buf(suite& self)
  82. : suite_(self)
  83. {
  84. }
  85. ~log_buf()
  86. {
  87. sync();
  88. }
  89. int
  90. sync() override
  91. {
  92. auto const& s = this->str();
  93. if(s.size() > 0)
  94. suite_.runner_->log(s);
  95. this->str("");
  96. return 0;
  97. }
  98. };
  99. template<
  100. class CharT,
  101. class Traits = std::char_traits<CharT>,
  102. class Allocator = std::allocator<CharT>
  103. >
  104. class log_os : public std::basic_ostream<CharT, Traits>
  105. {
  106. log_buf<CharT, Traits, Allocator> buf_;
  107. public:
  108. explicit
  109. log_os(suite& self)
  110. : std::basic_ostream<CharT, Traits>(&buf_)
  111. , buf_(self)
  112. {
  113. }
  114. };
  115. class scoped_testcase;
  116. class testcase_t
  117. {
  118. suite& suite_;
  119. std::stringstream ss_;
  120. public:
  121. explicit
  122. testcase_t(suite& self)
  123. : suite_(self)
  124. {
  125. }
  126. /** Open a new testcase.
  127. A testcase is a series of evaluated test conditions. A test
  128. suite may have multiple test cases. A test is associated with
  129. the last opened testcase. When the test first runs, a default
  130. unnamed case is opened. Tests with only one case may omit the
  131. call to testcase.
  132. @param abort Determines if suite continues running after a failure.
  133. */
  134. void
  135. operator()(std::string const& name,
  136. abort_t abort = no_abort_on_fail);
  137. scoped_testcase
  138. operator()(abort_t abort);
  139. template<class T>
  140. scoped_testcase
  141. operator<<(T const& t);
  142. };
  143. public:
  144. /** Logging output stream.
  145. Text sent to the log output stream will be forwarded to
  146. the output stream associated with the runner.
  147. */
  148. log_os<char> log;
  149. /** Memberspace for declaring test cases. */
  150. testcase_t testcase;
  151. /** Returns the "current" running suite.
  152. If no suite is running, nullptr is returned.
  153. */
  154. static
  155. suite*
  156. this_suite()
  157. {
  158. return *p_this_suite();
  159. }
  160. suite()
  161. : log(*this)
  162. , testcase(*this)
  163. {
  164. }
  165. /** Invokes the test using the specified runner.
  166. Data members are set up here instead of the constructor as a
  167. convenience to writing the derived class to avoid repetition of
  168. forwarded constructor arguments to the base.
  169. Normally this is called by the framework for you.
  170. */
  171. template<class = void>
  172. void
  173. operator()(runner& r);
  174. /** Record a successful test condition. */
  175. template<class = void>
  176. void
  177. pass();
  178. /** Record a failure.
  179. @param reason Optional text added to the output on a failure.
  180. @param file The source code file where the test failed.
  181. @param line The source code line number where the test failed.
  182. */
  183. /** @{ */
  184. template<class String>
  185. void
  186. fail(String const& reason, char const* file, int line);
  187. template<class = void>
  188. void
  189. fail(std::string const& reason = "");
  190. /** @} */
  191. /** Evaluate a test condition.
  192. This function provides improved logging by incorporating the
  193. file name and line number into the reported output on failure,
  194. as well as additional text specified by the caller.
  195. @param shouldBeTrue The condition to test. The condition
  196. is evaluated in a boolean context.
  197. @param reason Optional added text to output on a failure.
  198. @param file The source code file where the test failed.
  199. @param line The source code line number where the test failed.
  200. @return `true` if the test condition indicates success.
  201. */
  202. /** @{ */
  203. template<class Condition>
  204. bool
  205. expect(Condition const& shouldBeTrue)
  206. {
  207. return expect(shouldBeTrue, "");
  208. }
  209. template<class Condition, class String>
  210. bool
  211. expect(Condition const& shouldBeTrue, String const& reason);
  212. template<class Condition>
  213. bool
  214. expect(Condition const& shouldBeTrue,
  215. char const* file, int line)
  216. {
  217. return expect(shouldBeTrue, "", file, line);
  218. }
  219. template<class Condition, class String>
  220. bool
  221. expect(Condition const& shouldBeTrue,
  222. String const& reason, char const* file, int line);
  223. /** @} */
  224. //
  225. // DEPRECATED
  226. //
  227. // Expect an exception from f()
  228. template<class F, class String>
  229. bool
  230. except(F&& f, String const& reason);
  231. template<class F>
  232. bool
  233. except(F&& f)
  234. {
  235. return except(f, "");
  236. }
  237. template<class E, class F, class String>
  238. bool
  239. except(F&& f, String const& reason);
  240. template<class E, class F>
  241. bool
  242. except(F&& f)
  243. {
  244. return except<E>(f, "");
  245. }
  246. template<class F, class String>
  247. bool
  248. unexcept(F&& f, String const& reason);
  249. template<class F>
  250. bool
  251. unexcept(F&& f)
  252. {
  253. return unexcept(f, "");
  254. }
  255. /** Return the argument associated with the runner. */
  256. std::string const&
  257. arg() const
  258. {
  259. return runner_->arg();
  260. }
  261. // DEPRECATED
  262. // @return `true` if the test condition indicates success(a false value)
  263. template<class Condition, class String>
  264. bool
  265. unexpected(Condition shouldBeFalse,
  266. String const& reason);
  267. template<class Condition>
  268. bool
  269. unexpected(Condition shouldBeFalse)
  270. {
  271. return unexpected(shouldBeFalse, "");
  272. }
  273. private:
  274. friend class thread;
  275. static
  276. suite**
  277. p_this_suite()
  278. {
  279. static suite* pts = nullptr;
  280. return &pts;
  281. }
  282. /** Runs the suite. */
  283. virtual
  284. void
  285. run() = 0;
  286. void
  287. propagate_abort();
  288. template<class = void>
  289. void
  290. run(runner& r);
  291. };
  292. //------------------------------------------------------------------------------
  293. // Helper for streaming testcase names
  294. class suite::scoped_testcase
  295. {
  296. private:
  297. suite& suite_;
  298. std::stringstream& ss_;
  299. public:
  300. scoped_testcase& operator=(scoped_testcase const&) = delete;
  301. ~scoped_testcase()
  302. {
  303. auto const& name = ss_.str();
  304. if(! name.empty())
  305. suite_.runner_->testcase(name);
  306. }
  307. scoped_testcase(suite& self, std::stringstream& ss)
  308. : suite_(self)
  309. , ss_(ss)
  310. {
  311. ss_.clear();
  312. ss_.str({});
  313. }
  314. template<class T>
  315. scoped_testcase(suite& self,
  316. std::stringstream& ss, T const& t)
  317. : suite_(self)
  318. , ss_(ss)
  319. {
  320. ss_.clear();
  321. ss_.str({});
  322. ss_ << t;
  323. }
  324. template<class T>
  325. scoped_testcase&
  326. operator<<(T const& t)
  327. {
  328. ss_ << t;
  329. return *this;
  330. }
  331. };
  332. //------------------------------------------------------------------------------
  333. inline
  334. void
  335. suite::testcase_t::operator()(
  336. std::string const& name, abort_t abort)
  337. {
  338. suite_.abort_ = abort == abort_on_fail;
  339. suite_.runner_->testcase(name);
  340. }
  341. inline
  342. suite::scoped_testcase
  343. suite::testcase_t::operator()(abort_t abort)
  344. {
  345. suite_.abort_ = abort == abort_on_fail;
  346. return { suite_, ss_ };
  347. }
  348. template<class T>
  349. inline
  350. suite::scoped_testcase
  351. suite::testcase_t::operator<<(T const& t)
  352. {
  353. return { suite_, ss_, t };
  354. }
  355. //------------------------------------------------------------------------------
  356. template<class>
  357. void
  358. suite::
  359. operator()(runner& r)
  360. {
  361. *p_this_suite() = this;
  362. try
  363. {
  364. run(r);
  365. *p_this_suite() = nullptr;
  366. }
  367. catch(...)
  368. {
  369. *p_this_suite() = nullptr;
  370. throw;
  371. }
  372. }
  373. template<class Condition, class String>
  374. bool
  375. suite::
  376. expect(
  377. Condition const& shouldBeTrue, String const& reason)
  378. {
  379. if(shouldBeTrue)
  380. {
  381. pass();
  382. return true;
  383. }
  384. fail(reason);
  385. return false;
  386. }
  387. template<class Condition, class String>
  388. bool
  389. suite::
  390. expect(Condition const& shouldBeTrue,
  391. String const& reason, char const* file, int line)
  392. {
  393. if(shouldBeTrue)
  394. {
  395. pass();
  396. return true;
  397. }
  398. fail(detail::make_reason(reason, file, line));
  399. return false;
  400. }
  401. // DEPRECATED
  402. template<class F, class String>
  403. bool
  404. suite::
  405. except(F&& f, String const& reason)
  406. {
  407. try
  408. {
  409. f();
  410. fail(reason);
  411. return false;
  412. }
  413. catch(...)
  414. {
  415. pass();
  416. }
  417. return true;
  418. }
  419. template<class E, class F, class String>
  420. bool
  421. suite::
  422. except(F&& f, String const& reason)
  423. {
  424. try
  425. {
  426. f();
  427. fail(reason);
  428. return false;
  429. }
  430. catch(E const&)
  431. {
  432. pass();
  433. }
  434. return true;
  435. }
  436. template<class F, class String>
  437. bool
  438. suite::
  439. unexcept(F&& f, String const& reason)
  440. {
  441. try
  442. {
  443. f();
  444. pass();
  445. return true;
  446. }
  447. catch(...)
  448. {
  449. fail(reason);
  450. }
  451. return false;
  452. }
  453. template<class Condition, class String>
  454. bool
  455. suite::
  456. unexpected(
  457. Condition shouldBeFalse, String const& reason)
  458. {
  459. bool const b =
  460. static_cast<bool>(shouldBeFalse);
  461. if(! b)
  462. pass();
  463. else
  464. fail(reason);
  465. return ! b;
  466. }
  467. template<class>
  468. void
  469. suite::
  470. pass()
  471. {
  472. propagate_abort();
  473. runner_->pass();
  474. }
  475. // ::fail
  476. template<class>
  477. void
  478. suite::
  479. fail(std::string const& reason)
  480. {
  481. propagate_abort();
  482. runner_->fail(reason);
  483. if(abort_)
  484. {
  485. aborted_ = true;
  486. BOOST_THROW_EXCEPTION(abort_exception());
  487. }
  488. }
  489. template<class String>
  490. void
  491. suite::
  492. fail(String const& reason, char const* file, int line)
  493. {
  494. fail(detail::make_reason(reason, file, line));
  495. }
  496. inline
  497. void
  498. suite::
  499. propagate_abort()
  500. {
  501. if(abort_ && aborted_)
  502. BOOST_THROW_EXCEPTION(abort_exception());
  503. }
  504. template<class>
  505. void
  506. suite::
  507. run(runner& r)
  508. {
  509. runner_ = &r;
  510. try
  511. {
  512. run();
  513. }
  514. catch(abort_exception const&)
  515. {
  516. // ends the suite
  517. }
  518. catch(std::exception const& e)
  519. {
  520. runner_->fail("unhandled exception: " +
  521. std::string(e.what()));
  522. }
  523. catch(...)
  524. {
  525. runner_->fail("unhandled exception");
  526. }
  527. }
  528. #ifndef BEAST_PASS
  529. #define BEAST_PASS() ::boost::beast::unit_test::suite::this_suite()->pass()
  530. #endif
  531. #ifndef BEAST_FAIL
  532. #define BEAST_FAIL() ::boost::beast::unit_test::suite::this_suite()->fail("", __FILE__, __LINE__)
  533. #endif
  534. #ifndef BEAST_EXPECT
  535. /** Check a precondition.
  536. If the condition is false, the file and line number are reported.
  537. */
  538. #define BEAST_EXPECT(cond) ::boost::beast::unit_test::suite::this_suite()->expect(cond, __FILE__, __LINE__)
  539. #endif
  540. #ifndef BEAST_EXPECTS
  541. /** Check a precondition.
  542. If the condition is false, the file and line number are reported.
  543. */
  544. #define BEAST_EXPECTS(cond, reason) ((cond) ? \
  545. (::boost::beast::unit_test::suite::this_suite()->pass(), true) : \
  546. (::boost::beast::unit_test::suite::this_suite()->fail((reason), __FILE__, __LINE__), false))
  547. #endif
  548. } // unit_test
  549. } // beast
  550. } // boost
  551. //------------------------------------------------------------------------------
  552. // detail:
  553. // This inserts the suite with the given manual flag
  554. #define BEAST_DEFINE_TESTSUITE_INSERT(Library,Module,Class,manual) \
  555. static ::boost::beast::unit_test::detail::insert_suite <Class##_test> \
  556. Library ## Module ## Class ## _test_instance( \
  557. #Class, #Module, #Library, manual)
  558. //------------------------------------------------------------------------------
  559. // Preprocessor directives for controlling unit test definitions.
  560. // If this is already defined, don't redefine it. This allows
  561. // programs to provide custom behavior for testsuite definitions
  562. //
  563. #ifndef BEAST_DEFINE_TESTSUITE
  564. /** Enables insertion of test suites into the global container.
  565. The default is to insert all test suite definitions into the global
  566. container. If BEAST_DEFINE_TESTSUITE is user defined, this macro
  567. has no effect.
  568. */
  569. #ifndef BEAST_NO_UNIT_TEST_INLINE
  570. #define BEAST_NO_UNIT_TEST_INLINE 0
  571. #endif
  572. /** Define a unit test suite.
  573. Library Identifies the library.
  574. Module Identifies the module.
  575. Class The type representing the class being tested.
  576. The declaration for the class implementing the test should be the same
  577. as Class ## _test. For example, if Class is aged_ordered_container, the
  578. test class must be declared as:
  579. @code
  580. struct aged_ordered_container_test : beast::unit_test::suite
  581. {
  582. //...
  583. };
  584. @endcode
  585. The macro invocation must appear in the same namespace as the test class.
  586. */
  587. #if BEAST_NO_UNIT_TEST_INLINE
  588. #define BEAST_DEFINE_TESTSUITE(Class,Module,Library)
  589. #else
  590. #include <boost/beast/_experimental/unit_test/global_suites.hpp>
  591. #define BEAST_DEFINE_TESTSUITE(Library,Module,Class) \
  592. BEAST_DEFINE_TESTSUITE_INSERT(Library,Module,Class,false)
  593. #define BEAST_DEFINE_TESTSUITE_MANUAL(Library,Module,Class) \
  594. BEAST_DEFINE_TESTSUITE_INSERT(Library,Module,Class,true)
  595. #endif
  596. #endif
  597. //------------------------------------------------------------------------------
  598. #endif