Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
gtest_environment_test.cc
Go to the documentation of this file.
1// Copyright 2007, 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 using global test environments.
33
34#include <stdlib.h>
35#include <stdio.h>
36#include "gtest/gtest.h"
38
39namespace testing {
41}
42
43namespace {
44
45enum FailureType {
46 NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE
47};
48
49// For testing using global test environments.
50class MyEnvironment : public testing::Environment {
51 public:
52 MyEnvironment() { Reset(); }
53
54 // Depending on the value of failure_in_set_up_, SetUp() will
55 // generate a non-fatal failure, generate a fatal failure, or
56 // succeed.
57 virtual void SetUp() {
58 set_up_was_run_ = true;
59
60 switch (failure_in_set_up_) {
61 case NON_FATAL_FAILURE:
62 ADD_FAILURE() << "Expected non-fatal failure in global set-up.";
63 break;
64 case FATAL_FAILURE:
65 FAIL() << "Expected fatal failure in global set-up.";
66 break;
67 default:
68 break;
69 }
70 }
71
72 // Generates a non-fatal failure.
73 virtual void TearDown() {
74 tear_down_was_run_ = true;
75 ADD_FAILURE() << "Expected non-fatal failure in global tear-down.";
76 }
77
78 // Resets the state of the environment s.t. it can be reused.
79 void Reset() {
80 failure_in_set_up_ = NO_FAILURE;
81 set_up_was_run_ = false;
82 tear_down_was_run_ = false;
83 }
84
85 // We call this function to set the type of failure SetUp() should
86 // generate.
87 void set_failure_in_set_up(FailureType type) {
88 failure_in_set_up_ = type;
89 }
90
91 // Was SetUp() run?
92 bool set_up_was_run() const { return set_up_was_run_; }
93
94 // Was TearDown() run?
95 bool tear_down_was_run() const { return tear_down_was_run_; }
96
97 private:
98 FailureType failure_in_set_up_;
99 bool set_up_was_run_;
100 bool tear_down_was_run_;
101};
102
103// Was the TEST run?
104bool test_was_run;
105
106// The sole purpose of this TEST is to enable us to check whether it
107// was run.
108TEST(FooTest, Bar) {
109 test_was_run = true;
110}
111
112// Prints the message and aborts the program if condition is false.
113void Check(bool condition, const char* msg) {
114 if (!condition) {
115 printf("FAILED: %s\n", msg);
117 }
118}
119
120// Runs the tests. Return true iff successful.
121//
122// The 'failure' parameter specifies the type of failure that should
123// be generated by the global set-up.
124int RunAllTests(MyEnvironment* env, FailureType failure) {
125 env->Reset();
126 env->set_failure_in_set_up(failure);
127 test_was_run = false;
129 return RUN_ALL_TESTS();
130}
131
132} // namespace
133
134int main(int argc, char **argv) {
136
137 // Registers a global test environment, and verifies that the
138 // registration function returns its argument.
139 MyEnvironment* const env = new MyEnvironment;
140 Check(testing::AddGlobalTestEnvironment(env) == env,
141 "AddGlobalTestEnvironment() should return its argument.");
142
143 // Verifies that RUN_ALL_TESTS() runs the tests when the global
144 // set-up is successful.
145 Check(RunAllTests(env, NO_FAILURE) != 0,
146 "RUN_ALL_TESTS() should return non-zero, as the global tear-down "
147 "should generate a failure.");
148 Check(test_was_run,
149 "The tests should run, as the global set-up should generate no "
150 "failure");
151 Check(env->tear_down_was_run(),
152 "The global tear-down should run, as the global set-up was run.");
153
154 // Verifies that RUN_ALL_TESTS() runs the tests when the global
155 // set-up generates no fatal failure.
156 Check(RunAllTests(env, NON_FATAL_FAILURE) != 0,
157 "RUN_ALL_TESTS() should return non-zero, as both the global set-up "
158 "and the global tear-down should generate a non-fatal failure.");
159 Check(test_was_run,
160 "The tests should run, as the global set-up should generate no "
161 "fatal failure.");
162 Check(env->tear_down_was_run(),
163 "The global tear-down should run, as the global set-up was run.");
164
165 // Verifies that RUN_ALL_TESTS() runs no test when the global set-up
166 // generates a fatal failure.
167 Check(RunAllTests(env, FATAL_FAILURE) != 0,
168 "RUN_ALL_TESTS() should return non-zero, as the global set-up "
169 "should generate a fatal failure.");
170 Check(!test_was_run,
171 "The tests should not run, as the global set-up should generate "
172 "a fatal failure.");
173 Check(env->tear_down_was_run(),
174 "The global tear-down should run, as the global set-up was run.");
175
176 // Verifies that RUN_ALL_TESTS() doesn't do global set-up or
177 // tear-down when there is no test to run.
178 testing::GTEST_FLAG(filter) = "-*";
179 Check(RunAllTests(env, NO_FAILURE) == 0,
180 "RUN_ALL_TESTS() should return zero, as there is no test to run.");
181 Check(!env->set_up_was_run(),
182 "The global set-up should not run, as there is no test to run.");
183 Check(!env->tear_down_was_run(),
184 "The global tear-down should not run, "
185 "as the global set-up was not run.");
186
187 printf("PASS\n");
188 return 0;
189}
#define GTEST_DECLARE_string_(name)
#define FAIL()
Definition gtest.h:1858
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition gtest.h:2328
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
#define ADD_FAILURE()
Definition gtest.h:1844
int RunAllTests()
char ** argv
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
class UnitTestImpl * GetUnitTestImpl()
Environment * AddGlobalTestEnvironment(Environment *env)
Definition gtest.h:1391
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition gtest.cc:5787
yh_object_type type
Definition yubihsm.h:672