My Project
ut.hpp
Go to the documentation of this file.
1 
28 #ifndef BSL_UT_HPP
29 #define BSL_UT_HPP
30 
31 #include "color.hpp"
32 #include "debug.hpp"
33 #include "discard.hpp"
34 #include "exit_code.hpp"
35 #include "main.hpp"
36 #include "source_location.hpp"
37 #include "string_view.hpp"
38 
39 #include <stdlib.h> // NOLINT
40 
41 #pragma clang diagnostic ignored "-Wunused-member-function"
42 #pragma clang diagnostic ignored "-Wunneeded-member-function"
43 #pragma clang diagnostic ignored "-Wunneeded-internal-declaration"
44 #pragma clang diagnostic ignored "-Wmissing-braces"
45 #pragma clang diagnostic ignored "-Wglobal-constructors"
46 #pragma clang diagnostic ignored "-Wexit-time-destructors"
47 
48 namespace bsl
49 {
50  namespace details
51  {
58  inline void
59  ut_print_here(sloc_type const &sloc) noexcept
60  {
61  bsl::print() << sloc;
62  }
63  }
64 
73  class ut_scenario final
74  {
75  public:
82  explicit constexpr ut_scenario(string_view const &name) noexcept
83  {
84  bsl::discard(name);
85  }
86 
96  template<typename FUNC>
97  [[maybe_unused]] constexpr ut_scenario &
98  operator=(FUNC &&func) noexcept
99  {
100  func();
101  return *this;
102  }
103  };
104 
112  class ut_given final
113  {
114  public:
124  template<typename FUNC>
125  [[maybe_unused]] constexpr ut_given &
126  operator=(FUNC &&func) noexcept
127  {
128  func();
129  return *this;
130  }
131  };
132 
138  class ut_when final
139  {
140  public:
150  template<typename FUNC>
151  [[maybe_unused]] constexpr ut_when &
152  operator=(FUNC &&func) noexcept
153  {
154  func();
155  return *this;
156  }
157  };
158 
164  class ut_then final
165  {
166  public:
176  template<typename FUNC>
177  [[maybe_unused]] constexpr ut_then &
178  operator=(FUNC &&func) noexcept
179  {
180  func();
181  return *this;
182  }
183  };
184 
191  constexpr bsl::exit_code
192  ut_success() noexcept
193  {
194  bsl::print() << bsl::green << "All tests passed" << bsl::reset_color << bsl::endl;
195  return bsl::exit_success;
196  }
197 
204  inline void
205  ut_check_failed() noexcept
206  {}
207 
219  [[maybe_unused]] constexpr bool
220  ut_check(bool const test, sloc_type const &sloc = here()) noexcept
221  {
222  if (!test) {
223  bsl::error() << bsl::magenta << "[CHECK FAILED]" << bsl::reset_color << bsl::endl;
224  bsl::error() << sloc;
225 
226  ut_check_failed();
227  exit(1);
228  }
229 
230  return test;
231  }
232 }
233 
234 #endif
constexpr cstr_type reset_color
resets the color output of debug statements
Definition: color.hpp:36
constexpr ut_when & operator=(FUNC &&func) noexcept
Executes a lambda function as the body of the "when" portion of the scenario.
Definition: ut.hpp:152
constexpr out< details::out_type_print > print() noexcept
Returns and instance of bsl::out<T>. This version of bsl::out<T> does not print a label and does not ...
Definition: debug.hpp:72
constexpr ut_scenario & operator=(FUNC &&func) noexcept
Executes a lambda function as the body of the scenario.
Definition: ut.hpp:98
void ut_check_failed() noexcept
This is a non-constexpr function that can be used to detect when a unit test check fails....
Definition: ut.hpp:205
constexpr bsl::exit_code ut_success() noexcept
Outputs a message and returns bsl::exit_success.
Definition: ut.hpp:192
constexpr void discard(ARGS &&... args) noexcept
This function discards a parameter that it is given. This is the same as executing a static cast....
Definition: discard.hpp:58
constexpr cstr_type magenta
changes the foreground color to normal magenta
Definition: color.hpp:49
constexpr bsl::char_type endl
newline constant
Definition: debug.hpp:53
void ut_print_here(sloc_type const &sloc) noexcept
Prints the current source location to the console.
Definition: ut.hpp:59
constexpr bool ut_check(bool const test, sloc_type const &sloc=here()) noexcept
Checks to see if "test" is true. If test is false, this function will exit fast with a failure code.
Definition: ut.hpp:220
Defines a unit test scenario. A scenario defines a user story, describing the "scenario" being tested...
Definition: ut.hpp:73
Defines the initial state of a unit test scenario including the creation of any objects that might pa...
Definition: ut.hpp:112
This class implements the source_location specification that will eventually be included in C++20....
Definition: source_location.hpp:47
constexpr source_location here(source_location const &sloc=source_location::current()) noexcept
This provides a less verbose version of bsl::source_location::current() to help reduce how large this...
Definition: source_location.hpp:185
Defines the expected "result" of a unit test scenario.
Definition: ut.hpp:164
Defines the "action" of a unit test scenario.
Definition: ut.hpp:138
constexpr ut_scenario(string_view const &name) noexcept
Constructs a scenario.
Definition: ut.hpp:82
bsl::int32 exit_code
defines the exit code type
Definition: exit_code.hpp:36
constexpr cstr_type green
changes the foreground color to normal green
Definition: color.hpp:43
constexpr exit_code exit_success
represents a successful exit
Definition: exit_code.hpp:39
constexpr ut_given & operator=(FUNC &&func) noexcept
Executes a lambda function as the body of the "given" portion of the scenario.
Definition: ut.hpp:126
constexpr out< details::out_type_error > error() noexcept
Returns and instance of bsl::out<T>. This version of bsl::out<T> prints "ERROR: " when created and do...
Definition: debug.hpp:128
constexpr ut_then & operator=(FUNC &&func) noexcept
Executes a lambda function as the body of the "then" portion of the scenario.
Definition: ut.hpp:178