suite.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. virtual ~suite() = default;
  166. suite(suite const&) = delete;
  167. suite& operator=(suite const&) = delete;
  168. /** Invokes the test using the specified runner.
  169. Data members are set up here instead of the constructor as a
  170. convenience to writing the derived class to avoid repetition of
  171. forwarded constructor arguments to the base.
  172. Normally this is called by the framework for you.
  173. */
  174. template<class = void>
  175. void
  176. operator()(runner& r);
  177. /** Record a successful test condition. */
  178. template<class = void>
  179. void
  180. pass();
  181. /** Record a failure.
  182. @param reason Optional text added to the output on a failure.
  183. @param file The source code file where the test failed.
  184. @param line The source code line number where the test failed.
  185. */
  186. /** @{ */
  187. template<class String>
  188. void
  189. fail(String const& reason, char const* file, int line);
  190. template<class = void>
  191. void
  192. fail(std::string const& reason = "");
  193. /** @} */
  194. /** Evaluate a test condition.
  195. This function provides improved logging by incorporating the
  196. file name and line number into the reported output on failure,
  197. as well as additional text specified by the caller.
  198. @param shouldBeTrue The condition to test. The condition
  199. is evaluated in a boolean context.
  200. @param reason Optional added text to output on a failure.
  201. @param file The source code file where the test failed.
  202. @param line The source code line number where the test failed.
  203. @return `true` if the test condition indicates success.
  204. */
  205. /** @{ */
  206. template<class Condition>
  207. bool
  208. expect(Condition const& shouldBeTrue)
  209. {
  210. return expect(shouldBeTrue, "");
  211. }
  212. template<class Condition, class String>
  213. bool
  214. expect(Condition const& shouldBeTrue, String const& reason);
  215. template<class Condition>
  216. bool
  217. expect(Condition const& shouldBeTrue,
  218. char const* file, int line)
  219. {
  220. return expect(shouldBeTrue, "", file, line);
  221. }
  222. template<class Condition, class String>
  223. bool
  224. expect(Condition const& shouldBeTrue,
  225. String const& reason, char const* file, int line);
  226. /** @} */
  227. //
  228. // DEPRECATED
  229. //
  230. // Expect an exception from f()
  231. template<class F, class String>
  232. bool
  233. except(F&& f, String const& reason);
  234. template<class F>
  235. bool
  236. except(F&& f)
  237. {
  238. return except(f, "");
  239. }
  240. template<class E, class F, class String>
  241. bool
  242. except(F&& f, String const& reason);
  243. template<class E, class F>
  244. bool
  245. except(F&& f)
  246. {
  247. return except<E>(f, "");
  248. }
  249. template<class F, class String>
  250. bool
  251. unexcept(F&& f, String const& reason);
  252. template<class F>
  253. bool
  254. unexcept(F&& f)
  255. {
  256. return unexcept(f, "");
  257. }
  258. /** Return the argument associated with the runner. */
  259. std::string const&
  260. arg() const
  261. {
  262. return runner_->arg();
  263. }
  264. // DEPRECATED
  265. // @return `true` if the test condition indicates success(a false value)
  266. template<class Condition, class String>
  267. bool
  268. unexpected(Condition shouldBeFalse,
  269. String const& reason);
  270. template<class Condition>
  271. bool
  272. unexpected(Condition shouldBeFalse)
  273. {
  274. return unexpected(shouldBeFalse, "");
  275. }
  276. private:
  277. friend class thread;
  278. static
  279. suite**
  280. p_this_suite()
  281. {
  282. static suite* pts = nullptr;
  283. return &pts;
  284. }
  285. /** Runs the suite. */
  286. virtual
  287. void
  288. run() = 0;
  289. void
  290. propagate_abort();
  291. template<class = void>
  292. void
  293. run(runner& r);
  294. };
  295. //------------------------------------------------------------------------------
  296. // Helper for streaming testcase names
  297. class suite::scoped_testcase
  298. {
  299. private:
  300. suite& suite_;
  301. std::stringstream& ss_;
  302. public:
  303. scoped_testcase& operator=(scoped_testcase const&) = delete;
  304. ~scoped_testcase()
  305. {
  306. auto const& name = ss_.str();
  307. if(! name.empty())
  308. suite_.runner_->testcase(name);
  309. }
  310. scoped_testcase(suite& self, std::stringstream& ss)
  311. : suite_(self)
  312. , ss_(ss)
  313. {
  314. ss_.clear();
  315. ss_.str({});
  316. }
  317. template<class T>
  318. scoped_testcase(suite& self,
  319. std::stringstream& ss, T const& t)
  320. : suite_(self)
  321. , ss_(ss)
  322. {
  323. ss_.clear();
  324. ss_.str({});
  325. ss_ << t;
  326. }
  327. template<class T>
  328. scoped_testcase&
  329. operator<<(T const& t)
  330. {
  331. ss_ << t;
  332. return *this;
  333. }
  334. };
  335. //------------------------------------------------------------------------------
  336. inline
  337. void
  338. suite::testcase_t::operator()(
  339. std::string const& name, abort_t abort)
  340. {
  341. suite_.abort_ = abort == abort_on_fail;
  342. suite_.runner_->testcase(name);
  343. }
  344. inline
  345. suite::scoped_testcase
  346. suite::testcase_t::operator()(abort_t abort)
  347. {
  348. suite_.abort_ = abort == abort_on_fail;
  349. return { suite_, ss_ };
  350. }
  351. template<class T>
  352. inline
  353. suite::scoped_testcase
  354. suite::testcase_t::operator<<(T const& t)
  355. {
  356. return { suite_, ss_, t };
  357. }
  358. //------------------------------------------------------------------------------
  359. template<class>
  360. void
  361. suite::
  362. operator()(runner& r)
  363. {
  364. *p_this_suite() = this;
  365. try
  366. {
  367. run(r);
  368. *p_this_suite() = nullptr;
  369. }
  370. catch(...)
  371. {
  372. *p_this_suite() = nullptr;
  373. throw;
  374. }
  375. }
  376. template<class Condition, class String>
  377. bool
  378. suite::
  379. expect(
  380. Condition const& shouldBeTrue, String const& reason)
  381. {
  382. if(shouldBeTrue)
  383. {
  384. pass();
  385. return true;
  386. }
  387. fail(reason);
  388. return false;
  389. }
  390. template<class Condition, class String>
  391. bool
  392. suite::
  393. expect(Condition const& shouldBeTrue,
  394. String const& reason, char const* file, int line)
  395. {
  396. if(shouldBeTrue)
  397. {
  398. pass();
  399. return true;
  400. }
  401. fail(detail::make_reason(reason, file, line));
  402. return false;
  403. }
  404. // DEPRECATED
  405. template<class F, class String>
  406. bool
  407. suite::
  408. except(F&& f, String const& reason)
  409. {
  410. try
  411. {
  412. f();
  413. fail(reason);
  414. return false;
  415. }
  416. catch(...)
  417. {
  418. pass();
  419. }
  420. return true;
  421. }
  422. template<class E, class F, class String>
  423. bool
  424. suite::
  425. except(F&& f, String const& reason)
  426. {
  427. try
  428. {
  429. f();
  430. fail(reason);
  431. return false;
  432. }
  433. catch(E const&)
  434. {
  435. pass();
  436. }
  437. return true;
  438. }
  439. template<class F, class String>
  440. bool
  441. suite::
  442. unexcept(F&& f, String const& reason)
  443. {
  444. try
  445. {
  446. f();
  447. pass();
  448. return true;
  449. }
  450. catch(...)
  451. {
  452. fail(reason);
  453. }
  454. return false;
  455. }
  456. template<class Condition, class String>
  457. bool
  458. suite::
  459. unexpected(
  460. Condition shouldBeFalse, String const& reason)
  461. {
  462. bool const b =
  463. static_cast<bool>(shouldBeFalse);
  464. if(! b)
  465. pass();
  466. else
  467. fail(reason);
  468. return ! b;
  469. }
  470. template<class>
  471. void
  472. suite::
  473. pass()
  474. {
  475. propagate_abort();
  476. runner_->pass();
  477. }
  478. // ::fail
  479. template<class>
  480. void
  481. suite::
  482. fail(std::string const& reason)
  483. {
  484. propagate_abort();
  485. runner_->fail(reason);
  486. if(abort_)
  487. {
  488. aborted_ = true;
  489. BOOST_THROW_EXCEPTION(abort_exception());
  490. }
  491. }
  492. template<class String>
  493. void
  494. suite::
  495. fail(String const& reason, char const* file, int line)
  496. {
  497. fail(detail::make_reason(reason, file, line));
  498. }
  499. inline
  500. void
  501. suite::
  502. propagate_abort()
  503. {
  504. if(abort_ && aborted_)
  505. BOOST_THROW_EXCEPTION(abort_exception());
  506. }
  507. template<class>
  508. void
  509. suite::
  510. run(runner& r)
  511. {
  512. runner_ = &r;
  513. try
  514. {
  515. run();
  516. }
  517. catch(abort_exception const&)
  518. {
  519. // ends the suite
  520. }
  521. catch(std::exception const& e)
  522. {
  523. runner_->fail("unhandled exception: " +
  524. std::string(e.what()));
  525. }
  526. catch(...)
  527. {
  528. runner_->fail("unhandled exception");
  529. }
  530. }
  531. #ifndef BEAST_PASS
  532. #define BEAST_PASS() ::boost::beast::unit_test::suite::this_suite()->pass()
  533. #endif
  534. #ifndef BEAST_FAIL
  535. #define BEAST_FAIL() ::boost::beast::unit_test::suite::this_suite()->fail("", __FILE__, __LINE__)
  536. #endif
  537. #ifndef BEAST_EXPECT
  538. /** Check a precondition.
  539. If the condition is false, the file and line number are reported.
  540. */
  541. #define BEAST_EXPECT(cond) ::boost::beast::unit_test::suite::this_suite()->expect(cond, __FILE__, __LINE__)
  542. #endif
  543. #ifndef BEAST_EXPECTS
  544. /** Check a precondition.
  545. If the condition is false, the file and line number are reported.
  546. */
  547. #define BEAST_EXPECTS(cond, reason) ((cond) ? \
  548. (::boost::beast::unit_test::suite::this_suite()->pass(), true) : \
  549. (::boost::beast::unit_test::suite::this_suite()->fail((reason), __FILE__, __LINE__), false))
  550. #endif
  551. /** Ensure an exception is thrown
  552. */
  553. #define BEAST_THROWS( EXPR, EXCEP ) \
  554. try { \
  555. EXPR; \
  556. BEAST_FAIL(); \
  557. } \
  558. catch(EXCEP const&) { \
  559. BEAST_PASS(); \
  560. } \
  561. catch(...) { \
  562. BEAST_FAIL(); \
  563. }
  564. } // unit_test
  565. } // beast
  566. } // boost
  567. //------------------------------------------------------------------------------
  568. // detail:
  569. // This inserts the suite with the given manual flag
  570. #define BEAST_DEFINE_TESTSUITE_INSERT(Library,Module,Class,manual) \
  571. static ::boost::beast::unit_test::detail::insert_suite <Class##_test> \
  572. Library ## Module ## Class ## _test_instance( \
  573. #Class, #Module, #Library, manual)
  574. //------------------------------------------------------------------------------
  575. // Preprocessor directives for controlling unit test definitions.
  576. // If this is already defined, don't redefine it. This allows
  577. // programs to provide custom behavior for testsuite definitions
  578. //
  579. #ifndef BEAST_DEFINE_TESTSUITE
  580. /** Enables insertion of test suites into the global container.
  581. The default is to insert all test suite definitions into the global
  582. container. If BEAST_DEFINE_TESTSUITE is user defined, this macro
  583. has no effect.
  584. */
  585. #ifndef BEAST_NO_UNIT_TEST_INLINE
  586. #define BEAST_NO_UNIT_TEST_INLINE 0
  587. #endif
  588. /** Define a unit test suite.
  589. Library Identifies the library.
  590. Module Identifies the module.
  591. Class The type representing the class being tested.
  592. The declaration for the class implementing the test should be the same
  593. as Class ## _test. For example, if Class is aged_ordered_container, the
  594. test class must be declared as:
  595. @code
  596. struct aged_ordered_container_test : beast::unit_test::suite
  597. {
  598. //...
  599. };
  600. @endcode
  601. The macro invocation must appear in the same namespace as the test class.
  602. */
  603. #if BEAST_NO_UNIT_TEST_INLINE
  604. #define BEAST_DEFINE_TESTSUITE(Class,Module,Library)
  605. #else
  606. #include <boost/beast/_experimental/unit_test/global_suites.hpp>
  607. #define BEAST_DEFINE_TESTSUITE(Library,Module,Class) \
  608. BEAST_DEFINE_TESTSUITE_INSERT(Library,Module,Class,false)
  609. #define BEAST_DEFINE_TESTSUITE_MANUAL(Library,Module,Class) \
  610. BEAST_DEFINE_TESTSUITE_INSERT(Library,Module,Class,true)
  611. #endif
  612. #endif
  613. //------------------------------------------------------------------------------
  614. #endif