media/webrtc/trunk/testing/gtest/test/gtest_environment_test.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 2007, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29 //
michael@0 30 // Author: wan@google.com (Zhanyong Wan)
michael@0 31 //
michael@0 32 // Tests using global test environments.
michael@0 33
michael@0 34 #include <stdlib.h>
michael@0 35 #include <stdio.h>
michael@0 36 #include "gtest/gtest.h"
michael@0 37
michael@0 38 #define GTEST_IMPLEMENTATION_ 1 // Required for the next #include.
michael@0 39 #include "src/gtest-internal-inl.h"
michael@0 40 #undef GTEST_IMPLEMENTATION_
michael@0 41
michael@0 42 namespace testing {
michael@0 43 GTEST_DECLARE_string_(filter);
michael@0 44 }
michael@0 45
michael@0 46 namespace {
michael@0 47
michael@0 48 enum FailureType {
michael@0 49 NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE
michael@0 50 };
michael@0 51
michael@0 52 // For testing using global test environments.
michael@0 53 class MyEnvironment : public testing::Environment {
michael@0 54 public:
michael@0 55 MyEnvironment() { Reset(); }
michael@0 56
michael@0 57 // Depending on the value of failure_in_set_up_, SetUp() will
michael@0 58 // generate a non-fatal failure, generate a fatal failure, or
michael@0 59 // succeed.
michael@0 60 virtual void SetUp() {
michael@0 61 set_up_was_run_ = true;
michael@0 62
michael@0 63 switch (failure_in_set_up_) {
michael@0 64 case NON_FATAL_FAILURE:
michael@0 65 ADD_FAILURE() << "Expected non-fatal failure in global set-up.";
michael@0 66 break;
michael@0 67 case FATAL_FAILURE:
michael@0 68 FAIL() << "Expected fatal failure in global set-up.";
michael@0 69 break;
michael@0 70 default:
michael@0 71 break;
michael@0 72 }
michael@0 73 }
michael@0 74
michael@0 75 // Generates a non-fatal failure.
michael@0 76 virtual void TearDown() {
michael@0 77 tear_down_was_run_ = true;
michael@0 78 ADD_FAILURE() << "Expected non-fatal failure in global tear-down.";
michael@0 79 }
michael@0 80
michael@0 81 // Resets the state of the environment s.t. it can be reused.
michael@0 82 void Reset() {
michael@0 83 failure_in_set_up_ = NO_FAILURE;
michael@0 84 set_up_was_run_ = false;
michael@0 85 tear_down_was_run_ = false;
michael@0 86 }
michael@0 87
michael@0 88 // We call this function to set the type of failure SetUp() should
michael@0 89 // generate.
michael@0 90 void set_failure_in_set_up(FailureType type) {
michael@0 91 failure_in_set_up_ = type;
michael@0 92 }
michael@0 93
michael@0 94 // Was SetUp() run?
michael@0 95 bool set_up_was_run() const { return set_up_was_run_; }
michael@0 96
michael@0 97 // Was TearDown() run?
michael@0 98 bool tear_down_was_run() const { return tear_down_was_run_; }
michael@0 99
michael@0 100 private:
michael@0 101 FailureType failure_in_set_up_;
michael@0 102 bool set_up_was_run_;
michael@0 103 bool tear_down_was_run_;
michael@0 104 };
michael@0 105
michael@0 106 // Was the TEST run?
michael@0 107 bool test_was_run;
michael@0 108
michael@0 109 // The sole purpose of this TEST is to enable us to check whether it
michael@0 110 // was run.
michael@0 111 TEST(FooTest, Bar) {
michael@0 112 test_was_run = true;
michael@0 113 }
michael@0 114
michael@0 115 // Prints the message and aborts the program if condition is false.
michael@0 116 void Check(bool condition, const char* msg) {
michael@0 117 if (!condition) {
michael@0 118 printf("FAILED: %s\n", msg);
michael@0 119 testing::internal::posix::Abort();
michael@0 120 }
michael@0 121 }
michael@0 122
michael@0 123 // Runs the tests. Return true iff successful.
michael@0 124 //
michael@0 125 // The 'failure' parameter specifies the type of failure that should
michael@0 126 // be generated by the global set-up.
michael@0 127 int RunAllTests(MyEnvironment* env, FailureType failure) {
michael@0 128 env->Reset();
michael@0 129 env->set_failure_in_set_up(failure);
michael@0 130 test_was_run = false;
michael@0 131 testing::internal::GetUnitTestImpl()->ClearAdHocTestResult();
michael@0 132 return RUN_ALL_TESTS();
michael@0 133 }
michael@0 134
michael@0 135 } // namespace
michael@0 136
michael@0 137 int main(int argc, char **argv) {
michael@0 138 testing::InitGoogleTest(&argc, argv);
michael@0 139
michael@0 140 // Registers a global test environment, and verifies that the
michael@0 141 // registration function returns its argument.
michael@0 142 MyEnvironment* const env = new MyEnvironment;
michael@0 143 Check(testing::AddGlobalTestEnvironment(env) == env,
michael@0 144 "AddGlobalTestEnvironment() should return its argument.");
michael@0 145
michael@0 146 // Verifies that RUN_ALL_TESTS() runs the tests when the global
michael@0 147 // set-up is successful.
michael@0 148 Check(RunAllTests(env, NO_FAILURE) != 0,
michael@0 149 "RUN_ALL_TESTS() should return non-zero, as the global tear-down "
michael@0 150 "should generate a failure.");
michael@0 151 Check(test_was_run,
michael@0 152 "The tests should run, as the global set-up should generate no "
michael@0 153 "failure");
michael@0 154 Check(env->tear_down_was_run(),
michael@0 155 "The global tear-down should run, as the global set-up was run.");
michael@0 156
michael@0 157 // Verifies that RUN_ALL_TESTS() runs the tests when the global
michael@0 158 // set-up generates no fatal failure.
michael@0 159 Check(RunAllTests(env, NON_FATAL_FAILURE) != 0,
michael@0 160 "RUN_ALL_TESTS() should return non-zero, as both the global set-up "
michael@0 161 "and the global tear-down should generate a non-fatal failure.");
michael@0 162 Check(test_was_run,
michael@0 163 "The tests should run, as the global set-up should generate no "
michael@0 164 "fatal failure.");
michael@0 165 Check(env->tear_down_was_run(),
michael@0 166 "The global tear-down should run, as the global set-up was run.");
michael@0 167
michael@0 168 // Verifies that RUN_ALL_TESTS() runs no test when the global set-up
michael@0 169 // generates a fatal failure.
michael@0 170 Check(RunAllTests(env, FATAL_FAILURE) != 0,
michael@0 171 "RUN_ALL_TESTS() should return non-zero, as the global set-up "
michael@0 172 "should generate a fatal failure.");
michael@0 173 Check(!test_was_run,
michael@0 174 "The tests should not run, as the global set-up should generate "
michael@0 175 "a fatal failure.");
michael@0 176 Check(env->tear_down_was_run(),
michael@0 177 "The global tear-down should run, as the global set-up was run.");
michael@0 178
michael@0 179 // Verifies that RUN_ALL_TESTS() doesn't do global set-up or
michael@0 180 // tear-down when there is no test to run.
michael@0 181 testing::GTEST_FLAG(filter) = "-*";
michael@0 182 Check(RunAllTests(env, NO_FAILURE) == 0,
michael@0 183 "RUN_ALL_TESTS() should return zero, as there is no test to run.");
michael@0 184 Check(!env->set_up_was_run(),
michael@0 185 "The global set-up should not run, as there is no test to run.");
michael@0 186 Check(!env->tear_down_was_run(),
michael@0 187 "The global tear-down should not run, "
michael@0 188 "as the global set-up was not run.");
michael@0 189
michael@0 190 printf("PASS\n");
michael@0 191 return 0;
michael@0 192 }

mercurial