forked from ros2/rclcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rclcpp_gtest_macros.cpp
More file actions
204 lines (176 loc) · 6.76 KB
/
test_rclcpp_gtest_macros.cpp
File metadata and controls
204 lines (176 loc) · 6.76 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2020 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <gtest/gtest.h>
#include <cstring>
#include <exception>
#include <string>
#include "./rclcpp_gtest_macros.hpp"
#include "rcl/rcl.h"
#include "rclcpp/rclcpp.hpp"
struct NonStandardThrowable
{
bool operator==(const NonStandardThrowable &) const
{
return true;
}
};
std::ostream & operator<<(std::ostream & os, const NonStandardThrowable &)
{
os << "NonStandardThrowable";
return os;
}
TEST(TestGtestMacros, standard_exceptions) {
RCLCPP_EXPECT_THROW_EQ(
throw std::runtime_error("some runtime error"),
std::runtime_error("some runtime error"));
RCLCPP_EXPECT_THROW_EQ(
throw std::invalid_argument("some invalid argument error"),
std::invalid_argument("some invalid argument error"));
RCLCPP_ASSERT_THROW_EQ(
throw std::runtime_error("some runtime error"),
std::runtime_error("some runtime error"));
RCLCPP_ASSERT_THROW_EQ(
throw std::invalid_argument("some invalid argument error"),
std::invalid_argument("some invalid argument error"));
}
TEST(TestGtestMacros, standard_exceptions_not_equals) {
::testing::AssertionResult result = ::testing::AssertionSuccess();
CHECK_THROW_EQ_IMPL(
throw std::runtime_error("some runtime error"),
std::range_error("some runtime error"),
result);
EXPECT_FALSE(result);
CHECK_THROW_EQ_IMPL(
throw std::invalid_argument("some invalid argument error"),
std::invalid_argument("some different invalid argument error"),
result);
EXPECT_FALSE(result);
}
TEST(TestGTestMacros, non_standard_types) {
RCLCPP_EXPECT_THROW_EQ(throw 0, 0);
RCLCPP_EXPECT_THROW_EQ(throw 42, 42);
RCLCPP_EXPECT_THROW_EQ(throw std::string("some string"), std::string("some string"));
RCLCPP_EXPECT_THROW_EQ(throw NonStandardThrowable(), NonStandardThrowable());
RCLCPP_ASSERT_THROW_EQ(throw 0, 0);
RCLCPP_ASSERT_THROW_EQ(throw 42, 42);
RCLCPP_ASSERT_THROW_EQ(throw std::string("some string"), std::string("some string"));
RCLCPP_ASSERT_THROW_EQ(throw NonStandardThrowable(), NonStandardThrowable());
}
TEST(TestGTestMacros, non_standard_types_not_equals) {
::testing::AssertionResult result = ::testing::AssertionSuccess();
CHECK_THROW_EQ_IMPL(throw 0, 1, result);
EXPECT_FALSE(result);
result = ::testing::AssertionSuccess();
CHECK_THROW_EQ_IMPL(throw -42, 42, result);
EXPECT_FALSE(result);
result = ::testing::AssertionSuccess();
CHECK_THROW_EQ_IMPL(throw std::string("some string"), std::string("some other string"), result);
EXPECT_FALSE(result);
}
TEST(TestGTestMacros, rclcpp_exceptions) {
rcutils_error_state_t rcl_error_state = {"this is some error message", __FILE__, __LINE__};
{
auto expected =
rclcpp::exceptions::RCLError(RCL_RET_ERROR, &rcl_error_state, "exception_prefix");
auto actual =
rclcpp::exceptions::RCLError(RCL_RET_ERROR, &rcl_error_state, "exception_prefix");
RCLCPP_EXPECT_THROW_EQ(throw actual, expected);
RCLCPP_ASSERT_THROW_EQ(throw actual, expected);
}
{
auto expected =
rclcpp::exceptions::RCLBadAlloc(RCL_RET_BAD_ALLOC, &rcl_error_state);
auto actual =
rclcpp::exceptions::RCLBadAlloc(RCL_RET_BAD_ALLOC, &rcl_error_state);
RCLCPP_EXPECT_THROW_EQ(throw actual, expected);
}
{
// Prefixes are not checked
auto expected =
rclcpp::exceptions::RCLError(
RCL_RET_ERROR, &rcl_error_state, "exception_prefix");
auto actual =
rclcpp::exceptions::RCLError(
RCL_RET_ERROR, &rcl_error_state, "different_prefix");
RCLCPP_EXPECT_THROW_EQ(throw actual, expected);
RCLCPP_ASSERT_THROW_EQ(throw actual, expected);
}
{
// File names are not checked
rcutils_error_state_t different_error_state = rcl_error_state;
std::snprintf(
different_error_state.file, RCUTILS_ERROR_STATE_FILE_MAX_LENGTH, "different_file.cpp");
auto expected =
rclcpp::exceptions::RCLError(
RCL_RET_ERROR, &rcl_error_state, "exception_prefix");
auto actual =
rclcpp::exceptions::RCLError(
RCL_RET_ERROR, &different_error_state, "exception_prefix");
RCLCPP_EXPECT_THROW_EQ(throw actual, expected);
RCLCPP_ASSERT_THROW_EQ(throw actual, expected);
}
{
// Line numbers are not checked
rcutils_error_state_t different_error_state = rcl_error_state;
different_error_state.line_number += 42;
auto expected =
rclcpp::exceptions::RCLError(
RCL_RET_ERROR, &rcl_error_state, "exception_prefix");
auto actual =
rclcpp::exceptions::RCLError(
RCL_RET_ERROR, &different_error_state, "exception_prefix");
RCLCPP_EXPECT_THROW_EQ(throw actual, expected);
RCLCPP_ASSERT_THROW_EQ(throw actual, expected);
}
}
TEST(TestGTestMacros, rclcpp_exceptions_not_equal) {
rcutils_error_state_t rcl_error_state = {"this is some error message", __FILE__, __LINE__};
{
// Check different return errors
::testing::AssertionResult result = ::testing::AssertionSuccess();
auto expected =
rclcpp::exceptions::RCLError(RCL_RET_ERROR, &rcl_error_state, "exception_prefix");
auto actual =
rclcpp::exceptions::RCLError(RCL_RET_BAD_ALLOC, &rcl_error_state, "exception_prefix");
CHECK_THROW_EQ_IMPL(throw actual, expected, result);
EXPECT_FALSE(result);
}
{
// Check different error messages
rcutils_error_state_t different_error_state = rcl_error_state;
std::snprintf(
different_error_state.message,
RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH,
"this is a different error message");
::testing::AssertionResult result = ::testing::AssertionSuccess();
auto expected =
rclcpp::exceptions::RCLError(
RCL_RET_ERROR, &rcl_error_state, "exception_prefix");
auto actual =
rclcpp::exceptions::RCLError(
RCL_RET_ERROR, &different_error_state, "exception_prefix");
CHECK_THROW_EQ_IMPL(throw actual, expected, result);
EXPECT_FALSE(result);
}
{
// Check different exception types
::testing::AssertionResult result = ::testing::AssertionSuccess();
auto expected =
rclcpp::exceptions::RCLError(RCL_RET_ERROR, &rcl_error_state, "exception_prefix");
auto actual =
rclcpp::exceptions::RCLInvalidArgument(RCL_RET_ERROR, &rcl_error_state, "exception_prefix");
CHECK_THROW_EQ_IMPL(throw actual, expected, result);
EXPECT_FALSE(result);
}
}