suite_info.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_INFO_HPP
  10. #define BOOST_BEAST_UNIT_TEST_SUITE_INFO_HPP
  11. #include <cstring>
  12. #include <functional>
  13. #include <string>
  14. #include <utility>
  15. namespace boost {
  16. namespace beast {
  17. namespace unit_test {
  18. class runner;
  19. /** Associates a unit test type with metadata. */
  20. class suite_info
  21. {
  22. using run_type = std::function<void(runner&)>;
  23. std::string name_;
  24. std::string module_;
  25. std::string library_;
  26. bool manual_;
  27. run_type run_;
  28. public:
  29. suite_info(
  30. std::string name,
  31. std::string module,
  32. std::string library,
  33. bool manual,
  34. run_type run)
  35. : name_(std::move(name))
  36. , module_(std::move(module))
  37. , library_(std::move(library))
  38. , manual_(manual)
  39. , run_(std::move(run))
  40. {
  41. }
  42. std::string const&
  43. name() const
  44. {
  45. return name_;
  46. }
  47. std::string const&
  48. module() const
  49. {
  50. return module_;
  51. }
  52. std::string const&
  53. library() const
  54. {
  55. return library_;
  56. }
  57. /// Returns `true` if this suite only runs manually.
  58. bool
  59. manual() const
  60. {
  61. return manual_;
  62. }
  63. /// Return the canonical suite name as a string.
  64. std::string
  65. full_name() const
  66. {
  67. return library_ + "." + module_ + "." + name_;
  68. }
  69. /// Run a new instance of the associated test suite.
  70. void
  71. run(runner& r) const
  72. {
  73. run_(r);
  74. }
  75. friend
  76. bool
  77. operator<(suite_info const& lhs, suite_info const& rhs)
  78. {
  79. return
  80. std::tie(lhs.library_, lhs.module_, lhs.name_) <
  81. std::tie(rhs.library_, rhs.module_, rhs.name_);
  82. }
  83. };
  84. //------------------------------------------------------------------------------
  85. /// Convenience for producing suite_info for a given test type.
  86. template<class Suite>
  87. suite_info
  88. make_suite_info(
  89. std::string name,
  90. std::string module,
  91. std::string library,
  92. bool manual)
  93. {
  94. return suite_info(
  95. std::move(name),
  96. std::move(module),
  97. std::move(library),
  98. manual,
  99. [](runner& r)
  100. {
  101. Suite{}(r);
  102. }
  103. );
  104. }
  105. } // unit_test
  106. } // beast
  107. } // boost
  108. #endif