RavEngine
Loading...
Searching...
No Matches
gtest_helper.h
1//----------------------------------------------------------------------------//
2// //
3// ozz-animation is hosted at http://github.com/guillaumeblanc/ozz-animation //
4// and distributed under the MIT License (MIT). //
5// //
6// Copyright (c) Guillaume Blanc //
7// //
8// Permission is hereby granted, free of charge, to any person obtaining a //
9// copy of this software and associated documentation files (the "Software"), //
10// to deal in the Software without restriction, including without limitation //
11// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
12// and/or sell copies of the Software, and to permit persons to whom the //
13// Software is furnished to do so, subject to the following conditions: //
14// //
15// The above copyright notice and this permission notice shall be included in //
16// all copies or substantial portions of the Software. //
17// //
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
21// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER //
24// DEALINGS IN THE SOFTWARE. //
25// //
26//----------------------------------------------------------------------------//
27
28#ifndef OZZ_OZZ_BASE_GTEST_HELPER_H_
29#define OZZ_OZZ_BASE_GTEST_HELPER_H_
30
31#include <sstream>
32
33#include "gtest/gtest.h"
34
35// EXPECT_ASSERTION expands to real death test if assertions are enabled.
36// Parameters:
37// statement - A statement that a macro such as EXPECT_DEATH would test
38// for program termination.
39// regex - A regex that a macro such as EXPECT_DEATH would use to test
40// the output of statement.
41#ifdef NDEBUG
42// Expands to nothing if asserts aren't enabled (ie: NDEBUG is defined)
43#define EXPECT_ASSERTION(_statement, _regex) \
44 do { \
45 } while (void(0), false);
46#else // NDEBUG
47#ifdef _WIN32
48#include <crtdbg.h>
49
50#include <cstdlib>
51namespace internal {
52// Provides a hook during abort to ensure EXIT_FAILURE is returned.
53inline int AbortHook(int, char*, int*) { exit(EXIT_FAILURE); }
54} // namespace internal
55#define EXPECT_ASSERTION(_statement, _regex) \
56 do { \
57 /* During death tests executions:*/ \
58 /* Disables popping message boxes during crt and stl assertions*/ \
59 int old_mode = 0; \
60 (void)old_mode; \
61 _CRT_REPORT_HOOK old_hook = nullptr; \
62 (void)old_hook; \
63 if (testing::internal::GTEST_FLAG(internal_run_death_test).length() > 0) { \
64 old_mode = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); \
65 old_hook = _CrtSetReportHook(&internal::AbortHook); \
66 } \
67 EXPECT_DEATH((void)(_statement), _regex); \
68 if (testing::internal::GTEST_FLAG(internal_run_death_test).length() > 0) { \
69 _CrtSetReportMode(_CRT_ASSERT, old_mode); \
70 (void)_CrtSetReportHook(old_hook); \
71 } \
72 \
73 } while (void(0), 0)
74#else // _WIN32
75#define EXPECT_ASSERTION(_statement, _regex) EXPECT_DEATH(_statement, _regex)
76#endif // _WIN32
77#endif // NDEBUG
78
79// EXPECT_EQ_LOG* executes _expression and compares its result with _eq.
80// While executing _expression, EXPECT_EQ_LOG redirects _output (ex:
81// std::clog) and then expects that the output matched the regular expression
82// _re.
83#define EXPECT_EQ_LOG(_expression, _eq, _output, _re) \
84 do { \
85 internal::RedirectOuputTester tester(_output, _re); \
86 EXPECT_EQ(_expression, _eq); \
87 \
88 } while (void(0), 0)
89
90// There are multiple declinations EXPECT_EQ_LOG*, to match with clog, cerr and
91// cout outputs, and verbose level option.
92
93// Specialises EXPECT_EQ_LOG* for verbose clog output type.
94#define EXPECT_EQ_LOG_LOGV(_expression, _eq, _re) \
95 EXPECT_EQ_LOG(_expression, _eq, std::clog, \
96 ozz::log::kVerbose <= ozz::log::GetLevel() ? _re : nullptr)
97
98// Specialises EXPECT_EQ_LOG* for standard clog output type.
99#define EXPECT_EQ_LOG_LOG(_expression, _eq, _re) \
100 EXPECT_EQ_LOG(_expression, _eq, std::clog, \
101 ozz::log::kStandard <= ozz::log::GetLevel() ? _re : nullptr)
102
103// Specialises EXPECT_EQ_LOG* for standard cout output type.
104#define EXPECT_EQ_LOG_OUT(_expression, _eq, _re) \
105 EXPECT_EQ_LOG(_expression, _eq, std::cout, \
106 ozz::log::kStandard <= ozz::log::GetLevel() ? _re : nullptr)
107
108// Specialises EXPECT_EQ_LOG* for standard cerr output type.
109#define EXPECT_EQ_LOG_ERR(_expression, _eq, _re) \
110 EXPECT_EQ_LOG(_expression, _eq, std::cerr, \
111 ozz::log::kStandard <= ozz::log::GetLevel() ? _re : nullptr)
112
113// EXPECT_EQ_LOG* executes _expression while redirecting _output (ex:
114// std::clog) and then expects that the output matched the regular expression
115// _re.
116#define EXPECT_LOG(_expression, _output, _re) \
117 do { \
118 internal::RedirectOuputTester tester(_output, _re); \
119 (_expression); \
120 \
121 } while (void(0), 0)
122
123// There are multiple declinations EXPECT_LOG*, to match with clog, cerr and
124// cout outputs, and verbose level option.
125
126// Specialises EXPECT_LOG* for verbose clog output type.
127#define EXPECT_LOG_LOGV(_expression, _re) \
128 EXPECT_LOG(_expression, std::clog, \
129 ozz::log::kVerbose <= ozz::log::GetLevel() ? _re : nullptr)
130
131// Specialises EXPECT_LOG* for standard clog output type.
132#define EXPECT_LOG_LOG(_expression, _re) \
133 EXPECT_LOG(_expression, std::clog, \
134 ozz::log::kStandard <= ozz::log::GetLevel() ? _re : nullptr)
135
136// Specialises EXPECT_LOG* for standard cout output type.
137#define EXPECT_LOG_OUT(_expression, _re) \
138 EXPECT_LOG(_expression, std::cout, \
139 ozz::log::kStandard <= ozz::log::GetLevel() ? _re : nullptr)
140
141// Specialises EXPECT_LOG* for standard cerr output type.
142#define EXPECT_LOG_ERR(_expression, _re) \
143 EXPECT_LOG(_expression, std::cerr, \
144 ozz::log::kStandard <= ozz::log::GetLevel() ? _re : nullptr)
145
146namespace internal {
148 public:
149 // Specify a nullptr _regex to test an empty output
150 RedirectOuputTester(std::ostream& _ostream, const char* _regex)
151 : ostream_(_ostream), old_(_ostream.rdbuf()), regex_(_regex) {
152 // Redirect ostream_ buffer.
153 ostream_.rdbuf(redirect_.rdbuf());
154 }
156 if (regex_) {
157 EXPECT_TRUE(::testing::internal::RE::PartialMatch(redirect_.str().c_str(),
158 regex_));
159 } else {
160 EXPECT_EQ(redirect_.str().size(), 0u);
161 }
162 // Restore ostream_ buffer.
163 ostream_.rdbuf(old_);
164 // finally outputs everything temporary redirected.
165 ostream_ << redirect_.str();
166 }
167
168 private:
169 std::ostream& ostream_;
170 std::streambuf* old_;
171 const char* regex_;
172 std::stringstream redirect_;
173};
174} // namespace internal
175#endif // OZZ_OZZ_BASE_GTEST_HELPER_H_
Definition gtest_helper.h:147