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, typename E>
48 ::testing::AssertionResult AssertSome(
49  const char* expr,
50  const Try<T, E>& 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 // Creates a gtest `TYPED_TEST` that is disabled on Windows.
221 // TODO(andschwa): Remove after temporarily-disabled tests are fixed on
222 // Windows. See MESOS-6392.
223 #ifndef __WINDOWS__
224 #define TYPED_TEST_TEMP_DISABLED_ON_WINDOWS(test_case_name, test_name) \
225  TYPED_TEST(test_case_name, test_name)
226 #else
227 #define TYPED_TEST_TEMP_DISABLED_ON_WINDOWS(test_case_name, test_name) \
228  TYPED_TEST(test_case_name, DISABLED_##test_name)
229 #endif // __WINDOWS__
230 
231 
232 // NOTE: On Windows, the closest equivalent to `sleep` is `timeout`.
233 // Unfortunately, `timeout` requires an interactive terminal, otherwise
234 // it errors out immediately. Instead, we use `ping` against localhost
235 // with a count. On Windows, `ping` waits one second between pings.
236 // Additionally, because `ping` requires a count greater than 0,
237 // we simply `exit 0` if the sleep is too short.
238 //
239 // This must not be replaced with `powershell -c Start-Sleep`, because
240 // that causes flaky tests due to PowerShell crashing when a test
241 // machine is overloaded. See MESOS-8308.
242 #ifndef __WINDOWS__
243 #define SLEEP_COMMAND(x) "sleep " #x
244 #else
245 #define SLEEP_COMMAND(x) \
246  ((x) > 0 ? "ping 127.0.0.1 -n " #x " > NUL" : "cmd /C exit 0")
247 #endif // __WINDOWS__
248 
249 
250 inline ::testing::AssertionResult AssertExited(
251  const char* actualExpr,
252  const int actual)
253 {
254  if (WIFEXITED(actual)) {
255  return ::testing::AssertionSuccess();
256 #ifndef __WINDOWS__
257  } else if (WIFSIGNALED(actual)) {
258  return ::testing::AssertionFailure()
259  << "Expecting WIFEXITED(" << actualExpr << ") but "
260  << " WIFSIGNALED(" << actualExpr << ") is true and "
261  << "WTERMSIG(" << actualExpr << ") is " << strsignal(WTERMSIG(actual));
262  } else if (WIFSTOPPED(actual)) {
263  return ::testing::AssertionFailure()
264  << "Expecting WIFEXITED(" << actualExpr << ") but"
265  << " WIFSTOPPED(" << actualExpr << ") is true and "
266  << "WSTOPSIG(" << actualExpr << ") is " << strsignal(WSTOPSIG(actual));
267 #endif // __WINDOWS__
268  }
269 
270  return ::testing::AssertionFailure()
271  << "Expecting WIFEXITED(" << actualExpr << ") but got"
272  << " unknown value: " << ::testing::PrintToString(actual);
273 }
274 
275 
276 #define ASSERT_EXITED(expected, actual) \
277  ASSERT_PRED_FORMAT2(AssertExited, expected, actual)
278 
279 
280 #define EXPECT_EXITED(expected, actual) \
281  EXPECT_PRED_FORMAT2(AssertExited, expected, actual)
282 
283 
284 inline ::testing::AssertionResult AssertExitStatusEq(
285  const char* expectedExpr,
286  const char* actualExpr,
287  const int expected,
288  const int actual)
289 {
290  const ::testing::AssertionResult result = AssertExited(actualExpr, actual);
291 
292  if (result) {
293  if (WEXITSTATUS(actual) == expected) {
294  return ::testing::AssertionSuccess();
295  } else {
296  return ::testing::AssertionFailure()
297  << "Value of: WEXITSTATUS(" << actualExpr << ")\n"
298  << " Actual: " << ::testing::PrintToString(WEXITSTATUS(actual)) << "\n"
299  << "Expected: " << expectedExpr << "\n"
300  << "Which is: " << ::testing::PrintToString(expected);
301  }
302  }
303 
304  return result;
305 }
306 
307 
308 #define ASSERT_WEXITSTATUS_EQ(expected, actual) \
309  ASSERT_PRED_FORMAT2(AssertExitStatusEq, expected, actual)
310 
311 
312 #define EXPECT_WEXITSTATUS_EQ(expected, actual) \
313  EXPECT_PRED_FORMAT2(AssertExitStatusEq, expected, actual)
314 
315 
316 
317 inline ::testing::AssertionResult AssertExitStatusNe(
318  const char* expectedExpr,
319  const char* actualExpr,
320  const int expected,
321  const int actual)
322 {
323  const ::testing::AssertionResult result = AssertExited(actualExpr, actual);
324 
325  if (result) {
326  if (WEXITSTATUS(actual) != expected) {
327  return ::testing::AssertionSuccess();
328  } else {
329  return ::testing::AssertionFailure()
330  << "Value of: WEXITSTATUS(" << actualExpr << ")\n"
331  << " Actual: " << ::testing::PrintToString(WEXITSTATUS(actual)) << "\n"
332  << "Expected: " << expectedExpr << "\n"
333  << "Which is: " << ::testing::PrintToString(expected);
334  }
335  }
336 
337  return result;
338 }
339 
340 
341 #define ASSERT_WEXITSTATUS_NE(expected, actual) \
342  ASSERT_PRED_FORMAT2(AssertExitStatusNe, expected, actual)
343 
344 
345 #define EXPECT_WEXITSTATUS_NE(expected, actual) \
346  EXPECT_PRED_FORMAT2(AssertExitStatusNe, expected, actual)
347 
348 
349 // Signals aren't used in Windows, so #ifdef these out.
350 #ifndef __WINDOWS__
351 inline ::testing::AssertionResult AssertSignaled(
352  const char* actualExpr,
353  const int actual)
354 {
355  if (WIFEXITED(actual)) {
356  return ::testing::AssertionFailure()
357  << "Expecting WIFSIGNALED(" << actualExpr << ") but "
358  << " WIFEXITED(" << actualExpr << ") is true and "
359  << "WEXITSTATUS(" << actualExpr << ") is " << WEXITSTATUS(actual);
360  } else if (WIFSIGNALED(actual)) {
361  return ::testing::AssertionSuccess();
362  } else if (WIFSTOPPED(actual)) {
363  return ::testing::AssertionFailure()
364  << "Expecting WIFSIGNALED(" << actualExpr << ") but"
365  << " WIFSTOPPED(" << actualExpr << ") is true and "
366  << "WSTOPSIG(" << actualExpr << ") is " << strsignal(WSTOPSIG(actual));
367  }
368 
369  return ::testing::AssertionFailure()
370  << "Expecting WIFSIGNALED(" << actualExpr << ") but got"
371  << " unknown value: " << ::testing::PrintToString(actual);
372 }
373 
374 
375 #define ASSERT_SIGNALED(expected, actual) \
376  ASSERT_PRED_FORMAT2(AssertSignaled, expected, actual)
377 
378 
379 #define EXPECT_SIGNALED(expected, actual) \
380  EXPECT_PRED_FORMAT2(AssertSignaled, expected, actual)
381 
382 
383 inline ::testing::AssertionResult AssertTermSigEq(
384  const char* expectedExpr,
385  const char* actualExpr,
386  const int expected,
387  const int actual)
388 {
389  const ::testing::AssertionResult result = AssertSignaled(actualExpr, actual);
390 
391  if (result) {
392  if (WTERMSIG(actual) == expected) {
393  return ::testing::AssertionSuccess();
394  } else {
395  return ::testing::AssertionFailure()
396  << "Value of: WTERMSIG(" << actualExpr << ")\n"
397  << " Actual: " << strsignal(WTERMSIG(actual)) << "\n"
398  << "Expected: " << expectedExpr << "\n"
399  << "Which is: " << strsignal(expected);
400  }
401  }
402 
403  return result;
404 }
405 
406 
407 #define ASSERT_WTERMSIG_EQ(expected, actual) \
408  ASSERT_PRED_FORMAT2(AssertTermSigEq, expected, actual)
409 
410 
411 #define EXPECT_WTERMSIG_EQ(expected, actual) \
412  EXPECT_PRED_FORMAT2(AssertTermSigEq, expected, actual)
413 
414 
415 inline ::testing::AssertionResult AssertTermSigNe(
416  const char* expectedExpr,
417  const char* actualExpr,
418  const int expected,
419  const int actual)
420 {
421  const ::testing::AssertionResult result = AssertSignaled(actualExpr, actual);
422 
423  if (result) {
424  if (WTERMSIG(actual) != expected) {
425  return ::testing::AssertionSuccess();
426  } else {
427  return ::testing::AssertionFailure()
428  << "Value of: WTERMSIG(" << actualExpr << ")\n"
429  << " Actual: " << strsignal(WTERMSIG(actual)) << "\n"
430  << "Expected: " << expectedExpr << "\n"
431  << "Which is: " << strsignal(expected);
432  }
433  }
434 
435  return result;
436 }
437 
438 
439 #define ASSERT_WTERMSIG_NE(expected, actual) \
440  ASSERT_PRED_FORMAT2(AssertTermSigNe, expected, actual)
441 
442 
443 #define EXPECT_WTERMSIG_NE(expected, actual) \
444  EXPECT_PRED_FORMAT2(AssertTermSigNe, expected, actual)
445 #endif // __WINDOWS__
446 
447 #endif // __STOUT_GTEST_HPP__
inline::testing::AssertionResult AssertExitStatusNe(const char *expectedExpr, const char *actualExpr, const int expected, const int actual)
Definition: gtest.hpp:317
bool isNone() const
Definition: result.hpp:113
Definition: option.hpp:29
::testing::AssertionResult AssertSomeEq(const char *expectedExpr, const char *actualExpr, const T1 &expected, const T2 &actual)
Definition: gtest.hpp:79
Definition: check.hpp:33
static Result< T > error(const std::string &message)
Definition: result.hpp:54
const char * strsignal(int signum)
Definition: windows.hpp:347
Definition: check.hpp:30
::testing::AssertionResult AssertSome(const char *expr, const Option< T > &actual)
Definition: gtest.hpp:34
inline::testing::AssertionResult AssertExitStatusEq(const char *expectedExpr, const char *actualExpr, const int expected, const int actual)
Definition: gtest.hpp:284
#define WIFSIGNALED(x)
Definition: windows.hpp:376
#define WEXITSTATUS(x)
Definition: windows.hpp:368
inline::testing::AssertionResult AssertTermSigNe(const char *expectedExpr, const char *actualExpr, const int expected, const int actual)
Definition: gtest.hpp:415
static Try error(const E &e)
Definition: try.hpp:43
#define WIFEXITED(x)
Definition: windows.hpp:362
bool isError() const
Definition: try.hpp:78
inline::testing::AssertionResult AssertTermSigEq(const char *expectedExpr, const char *actualExpr, const int expected, const int actual)
Definition: gtest.hpp:383
inline::testing::AssertionResult AssertSignaled(const char *actualExpr, const int actual)
Definition: gtest.hpp:351
bool isNone() const
Definition: option.hpp:117
bool isError() const
Definition: result.hpp:114
inline::testing::AssertionResult AssertExited(const char *actualExpr, const int actual)
Definition: gtest.hpp:250
::testing::AssertionResult AssertSomeNe(const char *notExpectedExpr, const char *actualExpr, const T1 &notExpected, const T2 &actual)
Definition: gtest.hpp:104