gtest-extra.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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() 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. // Suppresses Windows assertions on invalid file descriptors, making
  101. // POSIX functions return proper error codes instead of crashing on Windows.
  102. class suppress_assert {
  103. private:
  104. _invalid_parameter_handler original_handler_;
  105. int original_report_mode_;
  106. static void handle_invalid_parameter(const wchar_t*, const wchar_t*,
  107. const wchar_t*, unsigned, uintptr_t) {}
  108. public:
  109. suppress_assert()
  110. : original_handler_(
  111. _set_invalid_parameter_handler(handle_invalid_parameter)),
  112. original_report_mode_(_CrtSetReportMode(_CRT_ASSERT, 0)) {}
  113. ~suppress_assert() {
  114. _set_invalid_parameter_handler(original_handler_);
  115. _CrtSetReportMode(_CRT_ASSERT, original_report_mode_);
  116. (void)original_report_mode_;
  117. }
  118. };
  119. # define SUPPRESS_ASSERT(statement) \
  120. { \
  121. suppress_assert sa; \
  122. statement; \
  123. }
  124. # else
  125. # define SUPPRESS_ASSERT(statement) statement
  126. # endif // _MSC_VER
  127. # define EXPECT_SYSTEM_ERROR_NOASSERT(statement, error_code, message) \
  128. EXPECT_SYSTEM_ERROR(SUPPRESS_ASSERT(statement), error_code, message)
  129. // Attempts to read count characters from a file.
  130. std::string read(fmt::file& f, size_t count);
  131. # define EXPECT_READ(file, expected_content) \
  132. EXPECT_EQ(expected_content, \
  133. read(file, fmt::string_view(expected_content).size()))
  134. #else
  135. # define EXPECT_WRITE(file, statement, expected_output) \
  136. do { \
  137. (void)(file); \
  138. (void)(statement); \
  139. (void)(expected_output); \
  140. SUCCEED(); \
  141. } while (false)
  142. #endif // FMT_USE_FCNTL
  143. #endif // FMT_GTEST_EXTRA_H_