Apache Mesos
gtest.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_GTEST_HPP__
14 #define __STOUT_GTEST_HPP__
15 
16 #include <string>
17 
18 #include <gtest/gtest.h>
19 
20 #include <stout/option.hpp>
21 #include <stout/result.hpp>
22 #include <stout/try.hpp>
23 
24 #ifdef __FreeBSD__
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #endif
28 
29 #ifdef __WINDOWS__
30 #include <stout/windows.hpp>
31 #endif
32 
33 template <typename T>
34 ::testing::AssertionResult AssertSome(
35  const char* expr,
36  const Option<T>& actual)
37 {
38  if (actual.isNone()) {
39  return ::testing::AssertionFailure()
40  << expr << " is NONE";
41  }
42 
43  return ::testing::AssertionSuccess();
44 }
45 
46 
47 template <typename T>
48 ::testing::AssertionResult AssertSome(
49  const char* expr,
50  const Try<T>& actual)
51 {
52  if (actual.isError()) {
53  return ::testing::AssertionFailure()
54  << expr << ": " << actual.error();
55  }
56 
57  return ::testing::AssertionSuccess();
58 }
59 
60 
61 template <typename T>
62 ::testing::AssertionResult AssertSome(
63  const char* expr,
64  const Result<T>& actual)
65 {
66  if (actual.isNone()) {
67  return ::testing::AssertionFailure()
68  << expr << " is NONE";
69  } else if (actual.isError()) {
70  return ::testing::AssertionFailure()
71  << expr << ": " << actual.error();
72  }
73 
74  return ::testing::AssertionSuccess();
75 }
76 
77 
78 template <typename T1, typename T2>
79 ::testing::AssertionResult AssertSomeEq(
80  const char* expectedExpr,
81  const char* actualExpr,
82  const T1& expected,
83  const T2& actual) // Duck typing!
84 {
85  const ::testing::AssertionResult result = AssertSome(actualExpr, actual);
86 
87  if (result) {
88  if (expected == actual.get()) {
89  return ::testing::AssertionSuccess();
90  } else {
91  return ::testing::AssertionFailure()
92  << "Value of: (" << actualExpr << ").get()\n"
93  << " Actual: " << ::testing::PrintToString(actual.get()) << "\n"
94  << "Expected: " << expectedExpr << "\n"
95  << "Which is: " << ::testing::PrintToString(expected);
96  }
97  }
98 
99  return result;
100 }
101 
102 
103 template <typename T1, typename T2>
104 ::testing::AssertionResult AssertSomeNe(
105  const char* notExpectedExpr,
106  const char* actualExpr,
107  const T1& notExpected,
108  const T2& actual) // Duck typing!
109 {
110  const ::testing::AssertionResult result = AssertSome(actualExpr, actual);
111 
112  if (result) {
113  if (notExpected == actual.get()) {
114  return ::testing::AssertionFailure()
115  << " Value of: (" << actualExpr << ").get()\n"
116  << " Actual: " << ::testing::PrintToString(actual.get()) << "\n"
117  << "Not expected: " << notExpectedExpr << "\n"
118  << " Which is: " << ::testing::PrintToString(notExpected);
119  } else {
120  return ::testing::AssertionSuccess();
121  }
122  }
123 
124  return result;
125 }
126 
127 
128 #define ASSERT_SOME(actual) \
129  ASSERT_PRED_FORMAT1(AssertSome, actual)
130 
131 
132 #define EXPECT_SOME(actual) \
133  EXPECT_PRED_FORMAT1(AssertSome, actual)
134 
135 
136 #define ASSERT_SOME_EQ(expected, actual) \
137  ASSERT_PRED_FORMAT2(AssertSomeEq, expected, actual)
138 
139 
140 #define EXPECT_SOME_EQ(expected, actual) \
141  EXPECT_PRED_FORMAT2(AssertSomeEq, expected, actual)
142 
143 
144 #define ASSERT_SOME_NE(notExpected, actual) \
145  ASSERT_PRED_FORMAT2(AssertSomeNe, notExpected, actual)
146 
147 
148 #define EXPECT_SOME_NE(notExpected, actual) \
149  EXPECT_PRED_FORMAT2(AssertSomeNe, notExpected, actual)
150 
151 
152 #define ASSERT_SOME_TRUE(actual) \
153  ASSERT_PRED_FORMAT2(AssertSomeEq, true, actual)
154 
155 
156 #define EXPECT_SOME_TRUE(actual) \
157  EXPECT_PRED_FORMAT2(AssertSomeEq, true, actual)
158 
159 
160 #define ASSERT_SOME_FALSE(actual) \
161  ASSERT_PRED_FORMAT2(AssertSomeEq, false, actual)
162 
163 
164 #define EXPECT_SOME_FALSE(actual) \
165  EXPECT_PRED_FORMAT2(AssertSomeEq, false, actual)
166 
167 
168 #define ASSERT_ERROR(actual) \
169  ASSERT_TRUE(actual.isError())
170 
171 
172 #define EXPECT_ERROR(actual) \
173  EXPECT_TRUE(actual.isError())
174 
175 
176 #define ASSERT_NONE(actual) \
177  ASSERT_TRUE(actual.isNone())
178 
179 
180 #define EXPECT_NONE(actual) \
181  EXPECT_TRUE(actual.isNone())
182 
183 
184 // Creates a gtest `TEST` that is disabled on Windows.
185 // TODO(hausdorff): Remove after temporarily-disabled tests are fixed on
186 // Windows. See MESOS-6392.
187 #ifndef __WINDOWS__
188 #define TEST_TEMP_DISABLED_ON_WINDOWS(test_case_name, test_name) \
189  TEST(test_case_name, test_name)
190 #else
191 #define TEST_TEMP_DISABLED_ON_WINDOWS(test_case_name, test_name) \
192  TEST(test_case_name, DISABLED_##test_name)
193 #endif // __WINDOWS__
194 
195 
196 // Creates a gtest `TEST_F` that is disabled on Windows.
197 // TODO(hausdorff): Remove after temporarily-disabled tests are fixed on
198 // Windows. See MESOS-6392.
199 #ifndef __WINDOWS__
200 #define TEST_F_TEMP_DISABLED_ON_WINDOWS(test_case_name, test_name) \
201  TEST_F(test_case_name, test_name)
202 #else
203 #define TEST_F_TEMP_DISABLED_ON_WINDOWS(test_case_name, test_name) \
204  TEST_F(test_case_name, DISABLED_##test_name)
205 #endif // __WINDOWS__
206 
207 
208 // Creates a gtest `TEST_P` that is disabled on Windows.
209 // TODO(greggomann): Remove after temporarily-disabled tests are fixed on
210 // Windows. See MESOS-6392.
211 #ifndef __WINDOWS__
212 #define TEST_P_TEMP_DISABLED_ON_WINDOWS(test_case_name, test_name) \
213  TEST_P(test_case_name, test_name)
214 #else
215 #define TEST_P_TEMP_DISABLED_ON_WINDOWS(test_case_name, test_name) \
216  TEST_P(test_case_name, DISABLED_##test_name)
217 #endif // __WINDOWS__
218 
219 
220 // NOTE: On Windows, the closest equivalent to `sleep` is `timeout`.
221 // Unfortunately, `timeout` requires an interactive terminal, otherwise
222 // it errors out immediately. Instead, we use `ping` against localhost
223 // with a count. On Windows, `ping` waits one second between pings.
224 // Additionally, because `ping` requires a count greater than 0,
225 // we simply `exit 0` if the sleep is too short.
226 #ifndef __WINDOWS__
227 #define SLEEP_COMMAND(x) "sleep " #x
228 #else
229 #define SLEEP_COMMAND(x) \
230  "powershell -NoProfile -Command Start-Sleep -Seconds " #x
231 #endif // __WINDOWS__
232 
233 
234 inline ::testing::AssertionResult AssertExited(
235  const char* actualExpr,
236  const int actual)
237 {
238  if (WIFEXITED(actual)) {
239  return ::testing::AssertionSuccess();
240  } else if (WIFSIGNALED(actual)) {
241  return ::testing::AssertionFailure()
242  << "Expecting WIFEXITED(" << actualExpr << ") but "
243  << " WIFSIGNALED(" << actualExpr << ") is true and "
244  << "WTERMSIG(" << actualExpr << ") is " << strsignal(WTERMSIG(actual));
245  } else if (WIFSTOPPED(actual)) {
246  return ::testing::AssertionFailure()
247  << "Expecting WIFEXITED(" << actualExpr << ") but"
248  << " WIFSTOPPED(" << actualExpr << ") is true and "
249  << "WSTOPSIG(" << actualExpr << ") is " << strsignal(WSTOPSIG(actual));
250  }
251 
252  return ::testing::AssertionFailure()
253  << "Expecting WIFEXITED(" << actualExpr << ") but got"
254  << " unknown value: " << ::testing::PrintToString(actual);
255 }
256 
257 
258 #define ASSERT_EXITED(expected, actual) \
259  ASSERT_PRED_FORMAT2(AssertExited, expected, actual)
260 
261 
262 #define EXPECT_EXITED(expected, actual) \
263  EXPECT_PRED_FORMAT2(AssertExited, expected, actual)
264 
265 
266 inline ::testing::AssertionResult AssertExitStatusEq(
267  const char* expectedExpr,
268  const char* actualExpr,
269  const int expected,
270  const int actual)
271 {
272  const ::testing::AssertionResult result = AssertExited(actualExpr, actual);
273 
274  if (result) {
275  if (WEXITSTATUS(actual) == expected) {
276  return ::testing::AssertionSuccess();
277  } else {
278  return ::testing::AssertionFailure()
279  << "Value of: WEXITSTATUS(" << actualExpr << ")\n"
280  << " Actual: " << ::testing::PrintToString(WEXITSTATUS(actual)) << "\n"
281  << "Expected: " << expectedExpr << "\n"
282  << "Which is: " << ::testing::PrintToString(expected);
283  }
284  }
285 
286  return result;
287 }
288 
289 
290 #define ASSERT_WEXITSTATUS_EQ(expected, actual) \
291  ASSERT_PRED_FORMAT2(AssertExitStatusEq, expected, actual)
292 
293 
294 #define EXPECT_WEXITSTATUS_EQ(expected, actual) \
295  EXPECT_PRED_FORMAT2(AssertExitStatusEq, expected, actual)
296 
297 
298 
299 inline ::testing::AssertionResult AssertExitStatusNe(
300  const char* expectedExpr,
301  const char* actualExpr,
302  const int expected,
303  const int actual)
304 {
305  const ::testing::AssertionResult result = AssertExited(actualExpr, actual);
306 
307  if (result) {
308  if (WEXITSTATUS(actual) != expected) {
309  return ::testing::AssertionSuccess();
310  } else {
311  return ::testing::AssertionFailure()
312  << "Value of: WEXITSTATUS(" << actualExpr << ")\n"
313  << " Actual: " << ::testing::PrintToString(WEXITSTATUS(actual)) << "\n"
314  << "Expected: " << expectedExpr << "\n"
315  << "Which is: " << ::testing::PrintToString(expected);
316  }
317  }
318 
319  return result;
320 }
321 
322 
323 #define ASSERT_WEXITSTATUS_NE(expected, actual) \
324  ASSERT_PRED_FORMAT2(AssertExitStatusNe, expected, actual)
325 
326 
327 #define EXPECT_WEXITSTATUS_NE(expected, actual) \
328  EXPECT_PRED_FORMAT2(AssertExitStatusNe, expected, actual)
329 
330 
331 inline ::testing::AssertionResult AssertSignaled(
332  const char* actualExpr,
333  const int actual)
334 {
335  if (WIFEXITED(actual)) {
336  return ::testing::AssertionFailure()
337  << "Expecting WIFSIGNALED(" << actualExpr << ") but "
338  << " WIFEXITED(" << actualExpr << ") is true and "
339  << "WEXITSTATUS(" << actualExpr << ") is " << WEXITSTATUS(actual);
340  } else if (WIFSIGNALED(actual)) {
341  return ::testing::AssertionSuccess();
342  } else if (WIFSTOPPED(actual)) {
343  return ::testing::AssertionFailure()
344  << "Expecting WIFSIGNALED(" << actualExpr << ") but"
345  << " WIFSTOPPED(" << actualExpr << ") is true and "
346  << "WSTOPSIG(" << actualExpr << ") is " << strsignal(WSTOPSIG(actual));
347  }
348 
349  return ::testing::AssertionFailure()
350  << "Expecting WIFSIGNALED(" << actualExpr << ") but got"
351  << " unknown value: " << ::testing::PrintToString(actual);
352 }
353 
354 
355 #define ASSERT_SIGNALED(expected, actual) \
356  ASSERT_PRED_FORMAT2(AssertSignaled, expected, actual)
357 
358 
359 #define EXPECT_SIGNALED(expected, actual) \
360  EXPECT_PRED_FORMAT2(AssertSignaled, expected, actual)
361 
362 
363 inline ::testing::AssertionResult AssertTermSigEq(
364  const char* expectedExpr,
365  const char* actualExpr,
366  const int expected,
367  const int actual)
368 {
369  const ::testing::AssertionResult result = AssertSignaled(actualExpr, actual);
370 
371  if (result) {
372  if (WTERMSIG(actual) == expected) {
373  return ::testing::AssertionSuccess();
374  } else {
375  return ::testing::AssertionFailure()
376  << "Value of: WTERMSIG(" << actualExpr << ")\n"
377  << " Actual: " << strsignal(WTERMSIG(actual)) << "\n"
378  << "Expected: " << expectedExpr << "\n"
379  << "Which is: " << strsignal(expected);
380  }
381  }
382 
383  return result;
384 }
385 
386 
387 #define ASSERT_WTERMSIG_EQ(expected, actual) \
388  ASSERT_PRED_FORMAT2(AssertTermSigEq, expected, actual)
389 
390 
391 #define EXPECT_WTERMSIG_EQ(expected, actual) \
392  EXPECT_PRED_FORMAT2(AssertTermSigEq, expected, actual)
393 
394 
395 inline ::testing::AssertionResult AssertTermSigNe(
396  const char* expectedExpr,
397  const char* actualExpr,
398  const int expected,
399  const int actual)
400 {
401  const ::testing::AssertionResult result = AssertSignaled(actualExpr, actual);
402 
403  if (result) {
404  if (WTERMSIG(actual) != expected) {
405  return ::testing::AssertionSuccess();
406  } else {
407  return ::testing::AssertionFailure()
408  << "Value of: WTERMSIG(" << actualExpr << ")\n"
409  << " Actual: " << strsignal(WTERMSIG(actual)) << "\n"
410  << "Expected: " << expectedExpr << "\n"
411  << "Which is: " << strsignal(expected);
412  }
413  }
414 
415  return result;
416 }
417 
418 
419 #define ASSERT_WTERMSIG_NE(expected, actual) \
420  ASSERT_PRED_FORMAT2(AssertTermSigNe, expected, actual)
421 
422 
423 #define EXPECT_WTERMSIG_NE(expected, actual) \
424  EXPECT_PRED_FORMAT2(AssertTermSigNe, expected, actual)
425 
426 #endif // __STOUT_GTEST_HPP__
inline::testing::AssertionResult AssertExitStatusNe(const char *expectedExpr, const char *actualExpr, const int expected, const int actual)
Definition: gtest.hpp:299
bool isNone() const
Definition: result.hpp:112
Definition: option.hpp:28
::testing::AssertionResult AssertSomeEq(const char *expectedExpr, const char *actualExpr, const T1 &expected, const T2 &actual)
Definition: gtest.hpp:79
Definition: try.hpp:34
static Result< T > error(const std::string &message)
Definition: result.hpp:53
const char * strsignal(int signum)
Definition: windows.hpp:358
Definition: result.hpp:40
::testing::AssertionResult AssertSome(const char *expr, const Option< T > &actual)
Definition: gtest.hpp:34
#define WSTOPSIG(x)
Definition: windows.hpp:401
inline::testing::AssertionResult AssertExitStatusEq(const char *expectedExpr, const char *actualExpr, const int expected, const int actual)
Definition: gtest.hpp:266
#define WIFSIGNALED(x)
Definition: windows.hpp:380
#define WIFSTOPPED(x)
Definition: windows.hpp:396
#define WEXITSTATUS(x)
Definition: windows.hpp:376
inline::testing::AssertionResult AssertTermSigNe(const char *expectedExpr, const char *actualExpr, const int expected, const int actual)
Definition: gtest.hpp:395
static Try error(const E &e)
Definition: try.hpp:42
#define WIFEXITED(x)
Definition: windows.hpp:370
bool isError() const
Definition: try.hpp:71
inline::testing::AssertionResult AssertTermSigEq(const char *expectedExpr, const char *actualExpr, const int expected, const int actual)
Definition: gtest.hpp:363
inline::testing::AssertionResult AssertSignaled(const char *actualExpr, const int actual)
Definition: gtest.hpp:331
bool isNone() const
Definition: option.hpp:116
#define WTERMSIG(x)
Definition: windows.hpp:386
bool isError() const
Definition: result.hpp:113
inline::testing::AssertionResult AssertExited(const char *actualExpr, const int actual)
Definition: gtest.hpp:234
::testing::AssertionResult AssertSomeNe(const char *notExpectedExpr, const char *actualExpr, const T1 &notExpected, const T2 &actual)
Definition: gtest.hpp:104