match.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_MATCH_HPP
  10. #define BOOST_BEAST_UNIT_TEST_MATCH_HPP
  11. #include <boost/beast/_experimental/unit_test/suite_info.hpp>
  12. #include <string>
  13. namespace boost {
  14. namespace beast {
  15. namespace unit_test {
  16. // Predicate for implementing matches
  17. class selector
  18. {
  19. public:
  20. enum mode_t
  21. {
  22. // Run all tests except manual ones
  23. all,
  24. // Run tests that match in any field
  25. automatch,
  26. // Match on suite
  27. suite,
  28. // Match on library
  29. library,
  30. // Match on module (used internally)
  31. module,
  32. // Match nothing (used internally)
  33. none
  34. };
  35. private:
  36. mode_t mode_;
  37. std::string pat_;
  38. std::string library_;
  39. public:
  40. template<class = void>
  41. explicit
  42. selector(mode_t mode, std::string const& pattern = "");
  43. template<class = void>
  44. bool
  45. operator()(suite_info const& s);
  46. };
  47. //------------------------------------------------------------------------------
  48. template<class>
  49. selector::selector(mode_t mode, std::string const& pattern)
  50. : mode_(mode)
  51. , pat_(pattern)
  52. {
  53. if(mode_ == automatch && pattern.empty())
  54. mode_ = all;
  55. }
  56. template<class>
  57. bool
  58. selector::operator()(suite_info const& s)
  59. {
  60. switch(mode_)
  61. {
  62. case automatch:
  63. // suite or full name
  64. if(s.name() == pat_ || s.full_name() == pat_)
  65. {
  66. mode_ = none;
  67. return true;
  68. }
  69. // check module
  70. if(pat_ == s.module())
  71. {
  72. mode_ = module;
  73. library_ = s.library();
  74. return ! s.manual();
  75. }
  76. // check library
  77. if(pat_ == s.library())
  78. {
  79. mode_ = library;
  80. return ! s.manual();
  81. }
  82. return false;
  83. case suite:
  84. return pat_ == s.name();
  85. case module:
  86. return pat_ == s.module() && ! s.manual();
  87. case library:
  88. return pat_ == s.library() && ! s.manual();
  89. case none:
  90. return false;
  91. case all:
  92. default:
  93. // fall through
  94. break;
  95. };
  96. return ! s.manual();
  97. }
  98. //------------------------------------------------------------------------------
  99. // Utility functions for producing predicates to select suites.
  100. /** Returns a predicate that implements a smart matching rule.
  101. The predicate checks the suite, module, and library fields of the
  102. suite_info in that order. When it finds a match, it changes modes
  103. depending on what was found:
  104. If a suite is matched first, then only the suite is selected. The
  105. suite may be marked manual.
  106. If a module is matched first, then only suites from that module
  107. and library not marked manual are selected from then on.
  108. If a library is matched first, then only suites from that library
  109. not marked manual are selected from then on.
  110. */
  111. inline
  112. selector
  113. match_auto(std::string const& name)
  114. {
  115. return selector(selector::automatch, name);
  116. }
  117. /** Return a predicate that matches all suites not marked manual. */
  118. inline
  119. selector
  120. match_all()
  121. {
  122. return selector(selector::all);
  123. }
  124. /** Returns a predicate that matches a specific suite. */
  125. inline
  126. selector
  127. match_suite(std::string const& name)
  128. {
  129. return selector(selector::suite, name);
  130. }
  131. /** Returns a predicate that matches all suites in a library. */
  132. inline
  133. selector
  134. match_library(std::string const& name)
  135. {
  136. return selector(selector::library, name);
  137. }
  138. } // unit_test
  139. } // beast
  140. } // boost
  141. #endif