gtest-extra.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Formatting library for C++ - custom Google Test assertions
  2. //
  3. // Copyright (c) 2012 - present, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. #ifndef FMT_GTEST_EXTRA_H_
  8. #define FMT_GTEST_EXTRA_H_
  9. #include <stdlib.h> // _invalid_parameter_handler
  10. #include <string>
  11. #ifdef FMT_MODULE_TEST
  12. import fmt;
  13. #else
  14. # include "fmt/os.h"
  15. #endif // FMG_MODULE_TEST
  16. #include "gmock/gmock.h"
  17. #define FMT_TEST_THROW_(statement, expected_exception, expected_message, fail) \
  18. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  19. if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \
  20. std::string gtest_expected_message = expected_message; \
  21. bool gtest_caught_expected = false; \
  22. try { \
  23. GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
  24. } catch (expected_exception const& e) { \
  25. if (gtest_expected_message != e.what()) { \
  26. gtest_ar << #statement \
  27. " throws an exception with a different message.\n" \
  28. << "Expected: " << gtest_expected_message << "\n" \
  29. << " Actual: " << e.what(); \
  30. goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
  31. } \
  32. gtest_caught_expected = true; \
  33. } catch (...) { \
  34. gtest_ar << "Expected: " #statement \
  35. " throws an exception of type " #expected_exception \
  36. ".\n Actual: it throws a different type."; \
  37. goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
  38. } \
  39. if (!gtest_caught_expected) { \
  40. gtest_ar << "Expected: " #statement \
  41. " throws an exception of type " #expected_exception \
  42. ".\n Actual: it throws nothing."; \
  43. goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
  44. } \
  45. } else \
  46. GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__) \
  47. : fail(gtest_ar.failure_message())
  48. // Tests that the statement throws the expected exception and the exception's
  49. // what() method returns expected message.
  50. #define EXPECT_THROW_MSG(statement, expected_exception, expected_message) \
  51. FMT_TEST_THROW_(statement, expected_exception, expected_message, \
  52. GTEST_NONFATAL_FAILURE_)
  53. inline std::string system_error_message(int error_code,
  54. const std::string& message) {
  55. auto ec = std::error_code(error_code, std::generic_category());
  56. return std::system_error(ec, message).what();
  57. }
  58. #define EXPECT_SYSTEM_ERROR(statement, error_code, message) \
  59. EXPECT_THROW_MSG(statement, std::system_error, \
  60. system_error_message(error_code, message))
  61. #if FMT_USE_FCNTL
  62. // Captures file output by redirecting it to a pipe.
  63. // The output it can handle is limited by the pipe capacity.
  64. class output_redirect {
  65. private:
  66. FILE* file_;
  67. fmt::file original_; // Original file passed to redirector.
  68. fmt::file read_end_; // Read end of the pipe where the output is redirected.
  69. void flush();
  70. void restore();
  71. public:
  72. explicit output_redirect(FILE* file);
  73. ~output_redirect() FMT_NOEXCEPT;
  74. output_redirect(const output_redirect&) = delete;
  75. void operator=(const output_redirect&) = delete;
  76. // Restores the original file, reads output from the pipe into a string
  77. // and returns it.
  78. std::string restore_and_read();
  79. };
  80. # define FMT_TEST_WRITE_(statement, expected_output, file, fail) \
  81. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  82. if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \
  83. std::string gtest_expected_output = expected_output; \
  84. output_redirect gtest_redir(file); \
  85. GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
  86. std::string gtest_output = gtest_redir.restore_and_read(); \
  87. if (gtest_output != gtest_expected_output) { \
  88. gtest_ar << #statement " produces different output.\n" \
  89. << "Expected: " << gtest_expected_output << "\n" \
  90. << " Actual: " << gtest_output; \
  91. goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
  92. } \
  93. } else \
  94. GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__) \
  95. : fail(gtest_ar.failure_message())
  96. // Tests that the statement writes the expected output to file.
  97. # define EXPECT_WRITE(file, statement, expected_output) \
  98. FMT_TEST_WRITE_(statement, expected_output, file, GTEST_NONFATAL_FAILURE_)
  99. # ifdef _MSC_VER
  100. #include <crtdbg.h>
  101. // Suppresses Windows assertions on invalid file descriptors, making
  102. // POSIX functions return proper error codes instead of crashing on Windows.
  103. class suppress_assert {
  104. private:
  105. _invalid_parameter_handler original_handler_;
  106. int original_report_mode_;
  107. static void handle_invalid_parameter(const wchar_t*, const wchar_t*,
  108. const wchar_t*, unsigned, uintptr_t) {}
  109. public:
  110. suppress_assert()
  111. : original_handler_(
  112. _set_invalid_parameter_handler(handle_invalid_parameter)),
  113. original_report_mode_(_CrtSetReportMode(_CRT_ASSERT, 0)) {}
  114. ~suppress_assert() {
  115. _set_invalid_parameter_handler(original_handler_);
  116. _CrtSetReportMode(_CRT_ASSERT, original_report_mode_);
  117. (void)original_report_mode_;
  118. }
  119. };
  120. # define SUPPRESS_ASSERT(statement) \
  121. { \
  122. suppress_assert sa; \
  123. statement; \
  124. }
  125. # else
  126. # define SUPPRESS_ASSERT(statement) statement
  127. # endif // _MSC_VER
  128. # define EXPECT_SYSTEM_ERROR_NOASSERT(statement, error_code, message) \
  129. EXPECT_SYSTEM_ERROR(SUPPRESS_ASSERT(statement), error_code, message)
  130. // Attempts to read count characters from a file.
  131. std::string read(fmt::file& f, size_t count);
  132. # define EXPECT_READ(file, expected_content) \
  133. EXPECT_EQ(expected_content, \
  134. read(file, fmt::string_view(expected_content).size()))
  135. #else
  136. # define EXPECT_WRITE(file, statement, expected_output) \
  137. do { \
  138. (void)(file); \
  139. (void)(statement); \
  140. (void)(expected_output); \
  141. SUCCEED(); \
  142. } while (false)
  143. #endif // FMT_USE_FCNTL
  144. #endif // FMT_GTEST_EXTRA_H_