media/webrtc/trunk/testing/gtest/samples/sample9_unittest.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial