gtest_throw_on_failure_ex_test.cc 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2009, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: wan@google.com (Zhanyong Wan)
  31. // Tests Google Test's throw-on-failure mode with exceptions enabled.
  32. #include "gtest/gtest.h"
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <stdexcept>
  37. // Prints the given failure message and exits the program with
  38. // non-zero. We use this instead of a Google Test assertion to
  39. // indicate a failure, as the latter is been tested and cannot be
  40. // relied on.
  41. void Fail(const char* msg) {
  42. printf("FAILURE: %s\n", msg);
  43. fflush(stdout);
  44. exit(1);
  45. }
  46. // Tests that an assertion failure throws a subclass of
  47. // std::runtime_error.
  48. void TestFailureThrowsRuntimeError() {
  49. testing::GTEST_FLAG(throw_on_failure) = true;
  50. // A successful assertion shouldn't throw.
  51. try {
  52. EXPECT_EQ(3, 3);
  53. } catch(...) {
  54. Fail("A successful assertion wrongfully threw.");
  55. }
  56. // A failed assertion should throw a subclass of std::runtime_error.
  57. try {
  58. EXPECT_EQ(2, 3) << "Expected failure";
  59. } catch(const std::runtime_error& e) {
  60. if (strstr(e.what(), "Expected failure") != NULL)
  61. return;
  62. printf("%s",
  63. "A failed assertion did throw an exception of the right type, "
  64. "but the message is incorrect. Instead of containing \"Expected "
  65. "failure\", it is:\n");
  66. Fail(e.what());
  67. } catch(...) {
  68. Fail("A failed assertion threw the wrong type of exception.");
  69. }
  70. Fail("A failed assertion should've thrown but didn't.");
  71. }
  72. int main(int argc, char** argv) {
  73. testing::InitGoogleTest(&argc, argv);
  74. // We want to ensure that people can use Google Test assertions in
  75. // other testing frameworks, as long as they initialize Google Test
  76. // properly and set the thrown-on-failure mode. Therefore, we don't
  77. // use Google Test's constructs for defining and running tests
  78. // (e.g. TEST and RUN_ALL_TESTS) here.
  79. TestFailureThrowsRuntimeError();
  80. return 0;
  81. }