suite.hpp 15 KB

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