-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathtest_gtest.h
121 lines (105 loc) · 3.95 KB
/
test_gtest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// This file is part of KWIVER, and is distributed under the
// OSI-approved BSD 3-Clause License. See top-level LICENSE file or
// https://github.com/Kitware/kwiver/blob/master/LICENSE for details.
/**
* \file
*
* \brief Supplemental macro definitions for test cases
*/
#ifndef KWIVER_TEST_TEST_GTEST_H_
#define KWIVER_TEST_TEST_GTEST_H_
#include <gtest/gtest.h>
#define TEST_LOAD_PLUGINS() \
class _test_plugin_helper : public ::testing::EmptyTestEventListener \
{ \
public: \
virtual void OnTestProgramStart(::testing::UnitTest const&) override \
{ kwiver::vital::plugin_manager::instance().load_all_plugins(); } \
}; \
::testing::UnitTest::GetInstance()->listeners().Append( \
new _test_plugin_helper )
// ----------------------------------------------------------------------------
/** @brief Consume a required command line argument.
*
* @param idx Index of the required argument.
* @param var Name of global variable into which the argument will be stored.
*
* @note The variable must be copy-assignable, and it must be possible to
* construct an instance of the variable's type from a \c char*. Use
* GET_ARG_EX if the latter condition does not hold.
*/
#define GET_ARG(idx, var) \
GET_ARG_EX(idx, var, decltype(var))
// ----------------------------------------------------------------------------
/** @brief Consume a required command line argument.
*
* @param idx Index of the required argument.
* @param var Name of global variable into which the argument will be stored.
* @param conv A functor which accepts a \c char* and returns an object that is
* copy-assignable to \p var.
*/
#define GET_ARG_EX(idx, var, conv) \
do \
{ \
using namespace testing; \
if (!GTEST_FLAG(list_tests)) \
{ \
if (argc <= (idx)) \
{ \
EXPECT_GT(argc, (idx)) \
<< "Required argument " \
<< (idx) << " missing"; \
return EXIT_FAILURE; \
} \
var = conv(argv[idx]); \
} \
} while (false)
// ----------------------------------------------------------------------------
/** @brief Declare a used command line argument.
*
* @param var Name of variable by which the command line argument will be
* accessed.
*
* @note The global variable must be named <code>g_ ## var</code>.
*/
#define TEST_ARG(var) \
public: decltype(g_ ## var) const& var = g_ ## var
// ----------------------------------------------------------------------------
/// Call a test function with the given arguments and print a traceback
/// on failure.
///
/// \param func Test function to call.
#define CALL_TEST( func, ... ) \
do { SCOPED_TRACE( #func ); func( __VA_ARGS__ ); } while( 0 )
namespace kwiver {
namespace testing {
// ----------------------------------------------------------------------------
template <typename V, typename L, typename U>
::testing::AssertionResult
is_in_inclusive_range(
char const* expr_value, char const* expr_lower, char const* expr_upper,
V value, L lower, U upper )
{
using namespace ::testing;
if ( value >= lower && value <= upper )
{
return AssertionSuccess();
}
return AssertionFailure()
<< "Expected: ("
<< expr_lower << ") ≤ ("
<< expr_value << ") ≤ ("
<< expr_upper << "), where\n"
<< expr_lower << " evaluates to " << PrintToString(lower) << ",\n"
<< expr_upper << " evaluates to " << PrintToString(upper) << ", and\n"
<< expr_value << " evaluates to " << PrintToString(value) << ".";
}
#define EXPECT_WITHIN( lower, value, upper ) \
EXPECT_PRED_FORMAT3( ::kwiver::testing::is_in_inclusive_range, \
value, lower, upper )
#define ASSERT_WITHIN( lower, value, upper ) \
ASSERT_PRED_FORMAT3( ::kwiver::testing::is_in_inclusive_range, \
value, lower, upper )
}
}
#endif