1b89a7cc2SEnji Cooper // Copyright 2009, Google Inc.
2b89a7cc2SEnji Cooper // All rights reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b89a7cc2SEnji Cooper 
30b89a7cc2SEnji Cooper // Tests Google Test's assert-by-exception mode with exceptions enabled.
31b89a7cc2SEnji Cooper 
32b89a7cc2SEnji Cooper #include <stdio.h>
3328f6c2f2SEnji Cooper #include <stdlib.h>
34b89a7cc2SEnji Cooper #include <string.h>
3528f6c2f2SEnji Cooper 
36b89a7cc2SEnji Cooper #include <stdexcept>
37b89a7cc2SEnji Cooper 
3828f6c2f2SEnji Cooper #include "gtest/gtest.h"
3928f6c2f2SEnji Cooper 
40b89a7cc2SEnji Cooper class ThrowListener : public testing::EmptyTestEventListener {
OnTestPartResult(const testing::TestPartResult & result)41b89a7cc2SEnji Cooper   void OnTestPartResult(const testing::TestPartResult& result) override {
42b89a7cc2SEnji Cooper     if (result.type() == testing::TestPartResult::kFatalFailure) {
43b89a7cc2SEnji Cooper       throw testing::AssertionException(result);
44b89a7cc2SEnji Cooper     }
45b89a7cc2SEnji Cooper   }
46b89a7cc2SEnji Cooper };
47b89a7cc2SEnji Cooper 
48b89a7cc2SEnji Cooper // Prints the given failure message and exits the program with
49b89a7cc2SEnji Cooper // non-zero.  We use this instead of a Google Test assertion to
50b89a7cc2SEnji Cooper // indicate a failure, as the latter is been tested and cannot be
51b89a7cc2SEnji Cooper // relied on.
Fail(const char * msg)52b89a7cc2SEnji Cooper void Fail(const char* msg) {
53b89a7cc2SEnji Cooper   printf("FAILURE: %s\n", msg);
54b89a7cc2SEnji Cooper   fflush(stdout);
55b89a7cc2SEnji Cooper   exit(1);
56b89a7cc2SEnji Cooper }
57b89a7cc2SEnji Cooper 
AssertFalse()5828f6c2f2SEnji Cooper static void AssertFalse() { ASSERT_EQ(2, 3) << "Expected failure"; }
59b89a7cc2SEnji Cooper 
60b89a7cc2SEnji Cooper // Tests that an assertion failure throws a subclass of
61b89a7cc2SEnji Cooper // std::runtime_error.
TEST(Test,Test)62b89a7cc2SEnji Cooper TEST(Test, Test) {
63b89a7cc2SEnji Cooper   // A successful assertion shouldn't throw.
64b89a7cc2SEnji Cooper   try {
65b89a7cc2SEnji Cooper     EXPECT_EQ(3, 3);
66b89a7cc2SEnji Cooper   } catch (...) {
67b89a7cc2SEnji Cooper     Fail("A successful assertion wrongfully threw.");
68b89a7cc2SEnji Cooper   }
69b89a7cc2SEnji Cooper 
70b89a7cc2SEnji Cooper   // A successful assertion shouldn't throw.
71b89a7cc2SEnji Cooper   try {
72b89a7cc2SEnji Cooper     EXPECT_EQ(3, 4);
73b89a7cc2SEnji Cooper   } catch (...) {
74b89a7cc2SEnji Cooper     Fail("A failed non-fatal assertion wrongfully threw.");
75b89a7cc2SEnji Cooper   }
76b89a7cc2SEnji Cooper 
77b89a7cc2SEnji Cooper   // A failed assertion should throw.
78b89a7cc2SEnji Cooper   try {
79b89a7cc2SEnji Cooper     AssertFalse();
80b89a7cc2SEnji Cooper   } catch (const testing::AssertionException& e) {
8128f6c2f2SEnji Cooper     if (strstr(e.what(), "Expected failure") != nullptr) throw;
82b89a7cc2SEnji Cooper 
83b89a7cc2SEnji Cooper     printf("%s",
84b89a7cc2SEnji Cooper            "A failed assertion did throw an exception of the right type, "
85b89a7cc2SEnji Cooper            "but the message is incorrect.  Instead of containing \"Expected "
86b89a7cc2SEnji Cooper            "failure\", it is:\n");
87b89a7cc2SEnji Cooper     Fail(e.what());
88b89a7cc2SEnji Cooper   } catch (...) {
89b89a7cc2SEnji Cooper     Fail("A failed assertion threw the wrong type of exception.");
90b89a7cc2SEnji Cooper   }
91b89a7cc2SEnji Cooper   Fail("A failed assertion should've thrown but didn't.");
92b89a7cc2SEnji Cooper }
93b89a7cc2SEnji Cooper 
94b89a7cc2SEnji Cooper int kTestForContinuingTest = 0;
95b89a7cc2SEnji Cooper 
TEST(Test,Test2)9628f6c2f2SEnji Cooper TEST(Test, Test2) { kTestForContinuingTest = 1; }
97b89a7cc2SEnji Cooper 
main(int argc,char ** argv)98b89a7cc2SEnji Cooper int main(int argc, char** argv) {
99b89a7cc2SEnji Cooper   testing::InitGoogleTest(&argc, argv);
100b89a7cc2SEnji Cooper   testing::UnitTest::GetInstance()->listeners().Append(new ThrowListener);
101b89a7cc2SEnji Cooper 
102b89a7cc2SEnji Cooper   int result = RUN_ALL_TESTS();
103b89a7cc2SEnji Cooper   if (result == 0) {
104b89a7cc2SEnji Cooper     printf("RUN_ALL_TESTS returned %d\n", result);
105b89a7cc2SEnji Cooper     Fail("Expected failure instead.");
106b89a7cc2SEnji Cooper   }
107b89a7cc2SEnji Cooper 
108b89a7cc2SEnji Cooper   if (kTestForContinuingTest == 0) {
109b89a7cc2SEnji Cooper     Fail("Should have continued with other tests, but did not.");
110b89a7cc2SEnji Cooper   }
111b89a7cc2SEnji Cooper   return 0;
112b89a7cc2SEnji Cooper }
113