Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sample9_unittest.cc
Go to the documentation of this file.
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.
28//
29// Author: vladl@google.com (Vlad Losev)
30
31// This sample shows how to use Google Test listener API to implement
32// an alternative console output and how to use the UnitTest reflection API
33// to enumerate test cases and tests and to inspect their results.
34
35#include <stdio.h>
36
37#include "gtest/gtest.h"
38
39using ::testing::EmptyTestEventListener;
40using ::testing::InitGoogleTest;
41using ::testing::Test;
42using ::testing::TestCase;
43using ::testing::TestEventListeners;
44using ::testing::TestInfo;
45using ::testing::TestPartResult;
46using ::testing::UnitTest;
47namespace {
48// Provides alternative output mode which produces minimal amount of
49// information about tests.
50class TersePrinter : public EmptyTestEventListener {
51 private:
52 // Called before any test activity starts.
53 virtual void OnTestProgramStart(const UnitTest& /* unit_test */) {}
54
55 // Called after all test activities have ended.
56 virtual void OnTestProgramEnd(const UnitTest& unit_test) {
57 fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
58 fflush(stdout);
59 }
60
61 // Called before a test starts.
62 virtual void OnTestStart(const TestInfo& test_info) {
63 fprintf(stdout,
64 "*** Test %s.%s starting.\n",
65 test_info.test_case_name(),
66 test_info.name());
67 fflush(stdout);
68 }
69
70 // Called after a failed assertion or a SUCCEED() invocation.
71 virtual void OnTestPartResult(const TestPartResult& test_part_result) {
72 fprintf(stdout,
73 "%s in %s:%d\n%s\n",
74 test_part_result.failed() ? "*** Failure" : "Success",
75 test_part_result.file_name(),
76 test_part_result.line_number(),
77 test_part_result.summary());
78 fflush(stdout);
79 }
80
81 // Called after a test ends.
82 virtual void OnTestEnd(const TestInfo& test_info) {
83 fprintf(stdout,
84 "*** Test %s.%s ending.\n",
85 test_info.test_case_name(),
86 test_info.name());
87 fflush(stdout);
88 }
89}; // class TersePrinter
90
91TEST(CustomOutputTest, PrintsMessage) {
92 printf("Printing something from the test body...\n");
93}
94
95TEST(CustomOutputTest, Succeeds) {
96 SUCCEED() << "SUCCEED() has been invoked from here";
97}
98
99TEST(CustomOutputTest, Fails) {
100 EXPECT_EQ(1, 2)
101 << "This test fails in order to demonstrate alternative failure messages";
102}
103} // namespace
104
105int main(int argc, char **argv) {
106 InitGoogleTest(&argc, argv);
107
108 bool terse_output = false;
109 if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 )
110 terse_output = true;
111 else
112 printf("%s\n", "Run this program with --terse_output to change the way "
113 "it prints its output.");
114
115 UnitTest& unit_test = *UnitTest::GetInstance();
116
117 // If we are given the --terse_output command line flag, suppresses the
118 // standard output and attaches own result printer.
119 if (terse_output) {
120 TestEventListeners& listeners = unit_test.listeners();
121
122 // Removes the default console output listener from the list so it will
123 // not receive events from Google Test and won't print any output. Since
124 // this operation transfers ownership of the listener to the caller we
125 // have to delete it as well.
126 delete listeners.Release(listeners.default_result_printer());
127
128 // Adds the custom output listener to the list. It will now receive
129 // events from Google Test and print the alternative output. We don't
130 // have to worry about deleting it since Google Test assumes ownership
131 // over it after adding it to the list.
132 listeners.Append(new TersePrinter);
133 }
134 int ret_val = RUN_ALL_TESTS();
135
136 // This is an example of using the UnitTest reflection API to inspect test
137 // results. Here we discount failures from the tests we expected to fail.
138 int unexpectedly_failed_tests = 0;
139 for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
140 const TestCase& test_case = *unit_test.GetTestCase(i);
141 for (int j = 0; j < test_case.total_test_count(); ++j) {
142 const TestInfo& test_info = *test_case.GetTestInfo(j);
143 // Counts failed tests that were not meant to fail (those without
144 // 'Fails' in the name).
145 if (test_info.result()->Failed() &&
146 strcmp(test_info.name(), "Fails") != 0) {
147 unexpectedly_failed_tests++;
148 }
149 }
150 }
151
152 // Test that were meant to fail should not affect the test program outcome.
153 if (unexpectedly_failed_tests == 0)
154 ret_val = 0;
155
156 return ret_val;
157}
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1954
#define SUCCEED()
Definition gtest.h:1867
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition gtest.h:2328
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
char ** argv
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
uint16_t j