]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/opentelemetry-cpp/third_party/googletest/googletest/samples/sample9_unittest.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / third_party / googletest / googletest / samples / sample9_unittest.cc
CommitLineData
31f18b77
FG
1// Copyright 2009 Google Inc. All Rights Reserved.
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are
5// met:
6//
7// * Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above
10// copyright notice, this list of conditions and the following disclaimer
11// in the documentation and/or other materials provided with the
12// distribution.
13// * Neither the name of Google Inc. nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1e59de90 28
31f18b77
FG
29
30// This sample shows how to use Google Test listener API to implement
31// an alternative console output and how to use the UnitTest reflection API
32// to enumerate test cases and tests and to inspect their results.
33
34#include <stdio.h>
35
36#include "gtest/gtest.h"
37
38using ::testing::EmptyTestEventListener;
39using ::testing::InitGoogleTest;
40using ::testing::Test;
41using ::testing::TestCase;
42using ::testing::TestEventListeners;
43using ::testing::TestInfo;
44using ::testing::TestPartResult;
45using ::testing::UnitTest;
31f18b77 46namespace {
31f18b77
FG
47// Provides alternative output mode which produces minimal amount of
48// information about tests.
49class TersePrinter : public EmptyTestEventListener {
50 private:
51 // Called before any test activity starts.
1e59de90 52 void OnTestProgramStart(const UnitTest& /* unit_test */) override {}
31f18b77
FG
53
54 // Called after all test activities have ended.
1e59de90 55 void OnTestProgramEnd(const UnitTest& unit_test) override {
31f18b77
FG
56 fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
57 fflush(stdout);
58 }
59
60 // Called before a test starts.
1e59de90 61 void OnTestStart(const TestInfo& test_info) override {
31f18b77
FG
62 fprintf(stdout,
63 "*** Test %s.%s starting.\n",
64 test_info.test_case_name(),
65 test_info.name());
66 fflush(stdout);
67 }
68
69 // Called after a failed assertion or a SUCCEED() invocation.
1e59de90 70 void OnTestPartResult(const TestPartResult& test_part_result) override {
31f18b77
FG
71 fprintf(stdout,
72 "%s in %s:%d\n%s\n",
73 test_part_result.failed() ? "*** Failure" : "Success",
74 test_part_result.file_name(),
75 test_part_result.line_number(),
76 test_part_result.summary());
77 fflush(stdout);
78 }
79
80 // Called after a test ends.
1e59de90 81 void OnTestEnd(const TestInfo& test_info) override {
31f18b77
FG
82 fprintf(stdout,
83 "*** Test %s.%s ending.\n",
84 test_info.test_case_name(),
85 test_info.name());
86 fflush(stdout);
87 }
88}; // class TersePrinter
89
90TEST(CustomOutputTest, PrintsMessage) {
91 printf("Printing something from the test body...\n");
92}
93
94TEST(CustomOutputTest, Succeeds) {
95 SUCCEED() << "SUCCEED() has been invoked from here";
96}
97
98TEST(CustomOutputTest, Fails) {
99 EXPECT_EQ(1, 2)
100 << "This test fails in order to demonstrate alternative failure messages";
101}
31f18b77
FG
102} // namespace
103
104int main(int argc, char **argv) {
105 InitGoogleTest(&argc, argv);
106
107 bool terse_output = false;
108 if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 )
109 terse_output = true;
110 else
111 printf("%s\n", "Run this program with --terse_output to change the way "
112 "it prints its output.");
113
114 UnitTest& unit_test = *UnitTest::GetInstance();
115
116 // If we are given the --terse_output command line flag, suppresses the
117 // standard output and attaches own result printer.
118 if (terse_output) {
119 TestEventListeners& listeners = unit_test.listeners();
120
121 // Removes the default console output listener from the list so it will
122 // not receive events from Google Test and won't print any output. Since
123 // this operation transfers ownership of the listener to the caller we
124 // have to delete it as well.
125 delete listeners.Release(listeners.default_result_printer());
126
127 // Adds the custom output listener to the list. It will now receive
128 // events from Google Test and print the alternative output. We don't
129 // have to worry about deleting it since Google Test assumes ownership
130 // over it after adding it to the list.
131 listeners.Append(new TersePrinter);
132 }
133 int ret_val = RUN_ALL_TESTS();
134
135 // This is an example of using the UnitTest reflection API to inspect test
136 // results. Here we discount failures from the tests we expected to fail.
137 int unexpectedly_failed_tests = 0;
1e59de90
TL
138 for (int i = 0; i < unit_test.total_test_suite_count(); ++i) {
139 const testing::TestSuite& test_suite = *unit_test.GetTestSuite(i);
140 for (int j = 0; j < test_suite.total_test_count(); ++j) {
141 const TestInfo& test_info = *test_suite.GetTestInfo(j);
31f18b77
FG
142 // Counts failed tests that were not meant to fail (those without
143 // 'Fails' in the name).
144 if (test_info.result()->Failed() &&
145 strcmp(test_info.name(), "Fails") != 0) {
146 unexpectedly_failed_tests++;
147 }
148 }
149 }
150
151 // Test that were meant to fail should not affect the test program outcome.
152 if (unexpectedly_failed_tests == 0)
153 ret_val = 0;
154
155 return ret_val;
156}