gtest-extra-test.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // Formatting library for C++ - tests of 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. #include "gtest-extra.h"
  8. #include <gtest/gtest-spi.h>
  9. #include <cstring>
  10. #include <memory>
  11. #include <stdexcept>
  12. #include "fmt/os.h"
  13. #include "util.h"
  14. // Tests that assertion macros evaluate their arguments exactly once.
  15. namespace {
  16. class single_evaluation_test : public ::testing::Test {
  17. protected:
  18. single_evaluation_test() {
  19. p_ = s_;
  20. a_ = 0;
  21. b_ = 0;
  22. }
  23. static const char* const s_;
  24. static const char* p_;
  25. static int a_;
  26. static int b_;
  27. };
  28. } // namespace
  29. const char* const single_evaluation_test::s_ = "01234";
  30. const char* single_evaluation_test::p_;
  31. int single_evaluation_test::a_;
  32. int single_evaluation_test::b_;
  33. void do_nothing() {}
  34. FMT_NORETURN void throw_exception() { throw std::runtime_error("test"); }
  35. FMT_NORETURN void throw_system_error() {
  36. throw fmt::system_error(EDOM, "test");
  37. }
  38. // Tests that when EXPECT_THROW_MSG fails, it evaluates its message argument
  39. // exactly once.
  40. TEST_F(single_evaluation_test, failed_expect_throw_msg) {
  41. EXPECT_NONFATAL_FAILURE(
  42. EXPECT_THROW_MSG(throw_exception(), std::exception, p_++), "01234");
  43. EXPECT_EQ(s_ + 1, p_);
  44. }
  45. // Tests that when EXPECT_SYSTEM_ERROR fails, it evaluates its message argument
  46. // exactly once.
  47. TEST_F(single_evaluation_test, failed_expect_system_error) {
  48. EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, p_++),
  49. "01234");
  50. EXPECT_EQ(s_ + 1, p_);
  51. }
  52. // Tests that assertion arguments are evaluated exactly once.
  53. TEST_F(single_evaluation_test, exception_tests) {
  54. // successful EXPECT_THROW_MSG
  55. EXPECT_THROW_MSG(
  56. { // NOLINT
  57. a_++;
  58. throw_exception();
  59. },
  60. std::exception, (b_++, "test"));
  61. EXPECT_EQ(1, a_);
  62. EXPECT_EQ(1, b_);
  63. // failed EXPECT_THROW_MSG, throws different type
  64. EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(
  65. { // NOLINT
  66. a_++;
  67. throw_exception();
  68. },
  69. std::logic_error, (b_++, "test")),
  70. "throws a different type");
  71. EXPECT_EQ(2, a_);
  72. EXPECT_EQ(2, b_);
  73. // failed EXPECT_THROW_MSG, throws an exception with different message
  74. EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(
  75. { // NOLINT
  76. a_++;
  77. throw_exception();
  78. },
  79. std::exception, (b_++, "other")),
  80. "throws an exception with a different message");
  81. EXPECT_EQ(3, a_);
  82. EXPECT_EQ(3, b_);
  83. // failed EXPECT_THROW_MSG, throws nothing
  84. EXPECT_NONFATAL_FAILURE(
  85. EXPECT_THROW_MSG(a_++, std::exception, (b_++, "test")), "throws nothing");
  86. EXPECT_EQ(4, a_);
  87. EXPECT_EQ(4, b_);
  88. }
  89. TEST_F(single_evaluation_test, system_error_tests) {
  90. // successful EXPECT_SYSTEM_ERROR
  91. EXPECT_SYSTEM_ERROR(
  92. { // NOLINT
  93. a_++;
  94. throw_system_error();
  95. },
  96. EDOM, (b_++, "test"));
  97. EXPECT_EQ(1, a_);
  98. EXPECT_EQ(1, b_);
  99. // failed EXPECT_SYSTEM_ERROR, throws different type
  100. EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(
  101. { // NOLINT
  102. a_++;
  103. throw_exception();
  104. },
  105. EDOM, (b_++, "test")),
  106. "throws a different type");
  107. EXPECT_EQ(2, a_);
  108. EXPECT_EQ(2, b_);
  109. // failed EXPECT_SYSTEM_ERROR, throws an exception with different message
  110. EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(
  111. { // NOLINT
  112. a_++;
  113. throw_system_error();
  114. },
  115. EDOM, (b_++, "other")),
  116. "throws an exception with a different message");
  117. EXPECT_EQ(3, a_);
  118. EXPECT_EQ(3, b_);
  119. // failed EXPECT_SYSTEM_ERROR, throws nothing
  120. EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(a_++, EDOM, (b_++, "test")),
  121. "throws nothing");
  122. EXPECT_EQ(4, a_);
  123. EXPECT_EQ(4, b_);
  124. }
  125. #if FMT_USE_FCNTL
  126. // Tests that when EXPECT_WRITE fails, it evaluates its message argument
  127. // exactly once.
  128. TEST_F(single_evaluation_test, failed_expect_write) {
  129. EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout, std::printf("test"), p_++),
  130. "01234");
  131. EXPECT_EQ(s_ + 1, p_);
  132. }
  133. // Tests that assertion arguments are evaluated exactly once.
  134. TEST_F(single_evaluation_test, write_tests) {
  135. // successful EXPECT_WRITE
  136. EXPECT_WRITE(
  137. stdout,
  138. { // NOLINT
  139. a_++;
  140. std::printf("test");
  141. },
  142. (b_++, "test"));
  143. EXPECT_EQ(1, a_);
  144. EXPECT_EQ(1, b_);
  145. // failed EXPECT_WRITE
  146. EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(
  147. stdout,
  148. { // NOLINT
  149. a_++;
  150. std::printf("test");
  151. },
  152. (b_++, "other")),
  153. "Actual: test");
  154. EXPECT_EQ(2, a_);
  155. EXPECT_EQ(2, b_);
  156. }
  157. // Tests EXPECT_WRITE.
  158. TEST(gtest_extra_test, expect_write) {
  159. EXPECT_WRITE(stdout, do_nothing(), "");
  160. EXPECT_WRITE(stdout, std::printf("test"), "test");
  161. EXPECT_WRITE(stderr, std::fprintf(stderr, "test"), "test");
  162. EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout, std::printf("that"), "this"),
  163. "Expected: this\n"
  164. " Actual: that");
  165. }
  166. TEST(gtest_extra_test, expect_write_streaming) {
  167. EXPECT_WRITE(stdout, std::printf("test"), "test") << "unexpected failure";
  168. EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout, std::printf("test"), "other")
  169. << "expected failure",
  170. "expected failure");
  171. }
  172. #endif // FMT_USE_FCNTL
  173. // Tests that the compiler will not complain about unreachable code in the
  174. // EXPECT_THROW_MSG macro.
  175. TEST(gtest_extra_test, expect_throw_no_unreachable_code_warning) {
  176. int n = 0;
  177. using std::runtime_error;
  178. EXPECT_THROW_MSG(throw runtime_error(""), runtime_error, "");
  179. EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(n++, runtime_error, ""), "");
  180. EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(throw 1, runtime_error, ""), "");
  181. EXPECT_NONFATAL_FAILURE(
  182. EXPECT_THROW_MSG(throw runtime_error("a"), runtime_error, "b"), "");
  183. }
  184. // Tests that the compiler will not complain about unreachable code in the
  185. // EXPECT_SYSTEM_ERROR macro.
  186. TEST(gtest_extra_test, expect_system_error_no_unreachable_code_warning) {
  187. int n = 0;
  188. EXPECT_SYSTEM_ERROR(throw fmt::system_error(EDOM, "test"), EDOM, "test");
  189. EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(n++, EDOM, ""), "");
  190. EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(throw 1, EDOM, ""), "");
  191. EXPECT_NONFATAL_FAILURE(
  192. EXPECT_SYSTEM_ERROR(throw fmt::system_error(EDOM, "aaa"), EDOM, "bbb"),
  193. "");
  194. }
  195. TEST(gtest_extra_test, expect_throw_behaves_like_single_statement) {
  196. if (::testing::internal::AlwaysFalse())
  197. EXPECT_THROW_MSG(do_nothing(), std::exception, "");
  198. if (::testing::internal::AlwaysTrue())
  199. EXPECT_THROW_MSG(throw_exception(), std::exception, "test");
  200. else
  201. do_nothing();
  202. }
  203. TEST(gtest_extra_test, expect_system_error_behaves_like_single_statement) {
  204. if (::testing::internal::AlwaysFalse())
  205. EXPECT_SYSTEM_ERROR(do_nothing(), EDOM, "");
  206. if (::testing::internal::AlwaysTrue())
  207. EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "test");
  208. else
  209. do_nothing();
  210. }
  211. TEST(gtest_extra_test, expect_write_behaves_like_single_statement) {
  212. if (::testing::internal::AlwaysFalse())
  213. EXPECT_WRITE(stdout, std::printf("x"), "x");
  214. if (::testing::internal::AlwaysTrue())
  215. EXPECT_WRITE(stdout, std::printf("x"), "x");
  216. else
  217. do_nothing();
  218. }
  219. // Tests EXPECT_THROW_MSG.
  220. TEST(gtest_extra_test, expect_throw_msg) {
  221. EXPECT_THROW_MSG(throw_exception(), std::exception, "test");
  222. EXPECT_NONFATAL_FAILURE(
  223. EXPECT_THROW_MSG(throw_exception(), std::logic_error, "test"),
  224. "Expected: throw_exception() throws an exception of "
  225. "type std::logic_error.\n Actual: it throws a different type.");
  226. EXPECT_NONFATAL_FAILURE(
  227. EXPECT_THROW_MSG(do_nothing(), std::exception, "test"),
  228. "Expected: do_nothing() throws an exception of type std::exception.\n"
  229. " Actual: it throws nothing.");
  230. EXPECT_NONFATAL_FAILURE(
  231. EXPECT_THROW_MSG(throw_exception(), std::exception, "other"),
  232. "throw_exception() throws an exception with a different message.\n"
  233. "Expected: other\n"
  234. " Actual: test");
  235. }
  236. // Tests EXPECT_SYSTEM_ERROR.
  237. TEST(gtest_extra_test, expect_system_error) {
  238. EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "test");
  239. EXPECT_NONFATAL_FAILURE(
  240. EXPECT_SYSTEM_ERROR(throw_exception(), EDOM, "test"),
  241. "Expected: throw_exception() throws an exception of "
  242. "type std::system_error.\n Actual: it throws a different type.");
  243. EXPECT_NONFATAL_FAILURE(
  244. EXPECT_SYSTEM_ERROR(do_nothing(), EDOM, "test"),
  245. "Expected: do_nothing() throws an exception of type std::system_error.\n"
  246. " Actual: it throws nothing.");
  247. EXPECT_NONFATAL_FAILURE(
  248. EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "other"),
  249. fmt::format(
  250. "throw_system_error() throws an exception with a different message.\n"
  251. "Expected: {}\n"
  252. " Actual: {}",
  253. system_error_message(EDOM, "other"),
  254. system_error_message(EDOM, "test")));
  255. }
  256. TEST(gtest_extra_test, expect_throw_msg_streaming) {
  257. EXPECT_THROW_MSG(throw_exception(), std::exception, "test")
  258. << "unexpected failure";
  259. EXPECT_NONFATAL_FAILURE(
  260. EXPECT_THROW_MSG(throw_exception(), std::exception, "other")
  261. << "expected failure",
  262. "expected failure");
  263. }
  264. TEST(gtest_extra_test, expect_system_error_streaming) {
  265. EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "test")
  266. << "unexpected failure";
  267. EXPECT_NONFATAL_FAILURE(
  268. EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "other")
  269. << "expected failure",
  270. "expected failure");
  271. }
  272. #if FMT_USE_FCNTL
  273. using fmt::buffered_file;
  274. using fmt::file;
  275. TEST(output_redirect_test, scoped_redirect) {
  276. file read_end, write_end;
  277. file::pipe(read_end, write_end);
  278. {
  279. buffered_file file(write_end.fdopen("w"));
  280. std::fprintf(file.get(), "[[[");
  281. {
  282. output_redirect redir(file.get());
  283. std::fprintf(file.get(), "censored");
  284. }
  285. std::fprintf(file.get(), "]]]");
  286. }
  287. EXPECT_READ(read_end, "[[[]]]");
  288. }
  289. // Test that output_redirect handles errors in flush correctly.
  290. TEST(output_redirect_test, flush_error_in_ctor) {
  291. file read_end, write_end;
  292. file::pipe(read_end, write_end);
  293. int write_fd = write_end.descriptor();
  294. file write_copy = write_end.dup(write_fd);
  295. buffered_file f = write_end.fdopen("w");
  296. // Put a character in a file buffer.
  297. EXPECT_EQ('x', fputc('x', f.get()));
  298. FMT_POSIX(close(write_fd));
  299. std::unique_ptr<output_redirect> redir{nullptr};
  300. EXPECT_SYSTEM_ERROR_NOASSERT(redir.reset(new output_redirect(f.get())), EBADF,
  301. "cannot flush stream");
  302. redir.reset(nullptr);
  303. write_copy.dup2(write_fd); // "undo" close or dtor will fail
  304. }
  305. TEST(output_redirect_test, dup_error_in_ctor) {
  306. buffered_file f = open_buffered_file();
  307. int fd = (f.descriptor)();
  308. file copy = file::dup(fd);
  309. FMT_POSIX(close(fd));
  310. std::unique_ptr<output_redirect> redir{nullptr};
  311. EXPECT_SYSTEM_ERROR_NOASSERT(
  312. redir.reset(new output_redirect(f.get())), EBADF,
  313. fmt::format("cannot duplicate file descriptor {}", fd));
  314. copy.dup2(fd); // "undo" close or dtor will fail
  315. }
  316. TEST(output_redirect_test, restore_and_read) {
  317. file read_end, write_end;
  318. file::pipe(read_end, write_end);
  319. buffered_file file(write_end.fdopen("w"));
  320. std::fprintf(file.get(), "[[[");
  321. output_redirect redir(file.get());
  322. std::fprintf(file.get(), "censored");
  323. EXPECT_EQ("censored", redir.restore_and_read());
  324. EXPECT_EQ("", redir.restore_and_read());
  325. std::fprintf(file.get(), "]]]");
  326. file = buffered_file();
  327. EXPECT_READ(read_end, "[[[]]]");
  328. }
  329. // Test that OutputRedirect handles errors in flush correctly.
  330. TEST(output_redirect_test, flush_error_in_restore_and_read) {
  331. file read_end, write_end;
  332. file::pipe(read_end, write_end);
  333. int write_fd = write_end.descriptor();
  334. file write_copy = write_end.dup(write_fd);
  335. buffered_file f = write_end.fdopen("w");
  336. output_redirect redir(f.get());
  337. // Put a character in a file buffer.
  338. EXPECT_EQ('x', fputc('x', f.get()));
  339. FMT_POSIX(close(write_fd));
  340. EXPECT_SYSTEM_ERROR_NOASSERT(redir.restore_and_read(), EBADF,
  341. "cannot flush stream");
  342. write_copy.dup2(write_fd); // "undo" close or dtor will fail
  343. }
  344. TEST(output_redirect_test, error_in_dtor) {
  345. file read_end, write_end;
  346. file::pipe(read_end, write_end);
  347. int write_fd = write_end.descriptor();
  348. file write_copy = write_end.dup(write_fd);
  349. buffered_file f = write_end.fdopen("w");
  350. std::unique_ptr<output_redirect> redir(new output_redirect(f.get()));
  351. // Put a character in a file buffer.
  352. EXPECT_EQ('x', fputc('x', f.get()));
  353. EXPECT_WRITE(
  354. stderr,
  355. {
  356. // The close function must be called inside EXPECT_WRITE,
  357. // otherwise the system may recycle closed file descriptor when
  358. // redirecting the output in EXPECT_STDERR and the second close
  359. // will break output redirection.
  360. FMT_POSIX(close(write_fd));
  361. SUPPRESS_ASSERT(redir.reset(nullptr));
  362. },
  363. system_error_message(EBADF, "cannot flush stream"));
  364. write_copy.dup2(write_fd); // "undo" close or dtor of buffered_file will fail
  365. }
  366. #endif // FMT_USE_FCNTL