Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
gtest_assert_by_exception_test.cc
Go to the documentation of this file.
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
32// Tests Google Test's assert-by-exception mode with exceptions enabled.
33
34#include "gtest/gtest.h"
35
36#include <stdlib.h>
37#include <stdio.h>
38#include <string.h>
39#include <stdexcept>
40
42 void OnTestPartResult(const testing::TestPartResult& result) override {
43 if (result.type() == testing::TestPartResult::kFatalFailure) {
44 throw testing::AssertionException(result);
45 }
46 }
47};
48
49// Prints the given failure message and exits the program with
50// non-zero. We use this instead of a Google Test assertion to
51// indicate a failure, as the latter is been tested and cannot be
52// relied on.
53void Fail(const char* msg) {
54 printf("FAILURE: %s\n", msg);
55 fflush(stdout);
56 exit(1);
57}
58
59static void AssertFalse() {
60 ASSERT_EQ(2, 3) << "Expected failure";
61}
62
63// Tests that an assertion failure throws a subclass of
64// std::runtime_error.
66 // A successful assertion shouldn't throw.
67 try {
68 EXPECT_EQ(3, 3);
69 } catch(...) {
70 Fail("A successful assertion wrongfully threw.");
71 }
72
73 // A successful assertion shouldn't throw.
74 try {
75 EXPECT_EQ(3, 4);
76 } catch(...) {
77 Fail("A failed non-fatal assertion wrongfully threw.");
78 }
79
80 // A failed assertion should throw.
81 try {
82 AssertFalse();
83 } catch(const testing::AssertionException& e) {
84 if (strstr(e.what(), "Expected failure") != NULL)
85 throw;
86
87 printf("%s",
88 "A failed assertion did throw an exception of the right type, "
89 "but the message is incorrect. Instead of containing \"Expected "
90 "failure\", it is:\n");
91 Fail(e.what());
92 } catch(...) {
93 Fail("A failed assertion threw the wrong type of exception.");
94 }
95 Fail("A failed assertion should've thrown but didn't.");
96}
97
99
100TEST(Test, Test2) {
101 // FIXME(sokolov): how to force Test2 to be after Test?
103}
104
105int main(int argc, char** argv) {
108
109 int result = RUN_ALL_TESTS();
110 if (result == 0) {
111 printf("RUN_ALL_TESTS returned %d\n", result);
112 Fail("Expected failure instead.");
113 }
114
115 if (kTestForContinuingTest == 0) {
116 Fail("Should have continued with other tests, but did not.");
117 }
118 return 0;
119}
void Append(TestEventListener *listener)
Definition gtest.cc:4304
static UnitTest * GetInstance()
Definition gtest.cc:4374
TestEventListeners & listeners()
Definition gtest.cc:4483
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1988
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1954
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition gtest.h:2328
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
void Fail(const char *msg)
char ** argv
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition gtest.cc:5787