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 // This file is the input to a negative-compilation test for Google
33 // Test. Code here is NOT supposed to compile. Its purpose is to
34 // verify that certain incorrect usages of the Google Test API are
35 // indeed rejected by the compiler.
36 //
37 // We still need to write the negative-compilation test itself, which
38 // will be tightly coupled with the build environment.
39 //
40 // TODO(wan@google.com): finish the negative-compilation test.
41
42 #ifdef TEST_CANNOT_IGNORE_RUN_ALL_TESTS_RESULT
43 // Tests that the result of RUN_ALL_TESTS() cannot be ignored.
44
45 #include <gtest/gtest.h>
46
main(int argc,char ** argv)47 int main(int argc, char** argv) {
48 testing::InitGoogleTest(&argc, argv);
49 RUN_ALL_TESTS(); // This line shouldn't compile.
50 }
51
52 #elif defined(TEST_USER_CANNOT_INCLUDE_GTEST_INTERNAL_INL_H)
53 // Tests that a user cannot include gtest-internal-inl.h in his code.
54
55 #include "src/gtest-internal-inl.h"
56
57 #elif defined(TEST_CATCHES_DECLARING_SETUP_IN_TEST_FIXTURE_WITH_TYPO)
58 // Tests that the compiler catches the typo when a user declares a
59 // Setup() method in a test fixture.
60
61 #include <gtest/gtest.h>
62
63 class MyTest : public testing::Test {
64 protected:
Setup()65 void Setup() {}
66 };
67
68 #elif defined(TEST_CATCHES_CALLING_SETUP_IN_TEST_WITH_TYPO)
69 // Tests that the compiler catches the typo when a user calls Setup()
70 // from a test fixture.
71
72 #include <gtest/gtest.h>
73
74 class MyTest : public testing::Test {
75 protected:
SetUp()76 virtual void SetUp() {
77 testing::Test::Setup(); // Tries to call SetUp() in the parent class.
78 }
79 };
80
81 #elif defined(TEST_CATCHES_DECLARING_SETUP_IN_ENVIRONMENT_WITH_TYPO)
82 // Tests that the compiler catches the typo when a user declares a
83 // Setup() method in a subclass of Environment.
84
85 #include <gtest/gtest.h>
86
87 class MyEnvironment : public testing::Environment {
88 public:
Setup()89 void Setup() {}
90 };
91
92 #elif defined(TEST_CATCHES_CALLING_SETUP_IN_ENVIRONMENT_WITH_TYPO)
93 // Tests that the compiler catches the typo when a user calls Setup()
94 // in an Environment.
95
96 #include <gtest/gtest.h>
97
98 class MyEnvironment : public testing::Environment {
99 protected:
SetUp()100 virtual void SetUp() {
101 // Tries to call SetUp() in the parent class.
102 testing::Environment::Setup();
103 }
104 };
105
106 #elif defined(TEST_CATCHES_WRONG_CASE_IN_TYPED_TEST_P)
107 // Tests that the compiler catches using the wrong test case name in
108 // TYPED_TEST_P.
109
110 #include <gtest/gtest.h>
111
112 template <typename T>
113 class FooTest : public testing::Test {
114 };
115
116 template <typename T>
117 class BarTest : public testing::Test {
118 };
119
120 TYPED_TEST_CASE_P(FooTest);
TYPED_TEST_P(BarTest,A)121 TYPED_TEST_P(BarTest, A) {} // Wrong test case name.
122 REGISTER_TYPED_TEST_CASE_P(FooTest, A);
123 INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, testing::Types<int>);
124
125 #elif defined(TEST_CATCHES_WRONG_CASE_IN_REGISTER_TYPED_TEST_CASE_P)
126 // Tests that the compiler catches using the wrong test case name in
127 // REGISTER_TYPED_TEST_CASE_P.
128
129 #include <gtest/gtest.h>
130
131 template <typename T>
132 class FooTest : public testing::Test {
133 };
134
135 template <typename T>
136 class BarTest : public testing::Test {
137 };
138
139 TYPED_TEST_CASE_P(FooTest);
TYPED_TEST_P(FooTest,A)140 TYPED_TEST_P(FooTest, A) {}
141 REGISTER_TYPED_TEST_CASE_P(BarTest, A); // Wrong test case name.
142 INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, testing::Types<int>);
143
144 #elif defined(TEST_CATCHES_WRONG_CASE_IN_INSTANTIATE_TYPED_TEST_CASE_P)
145 // Tests that the compiler catches using the wrong test case name in
146 // INSTANTIATE_TYPED_TEST_CASE_P.
147
148 #include <gtest/gtest.h>
149
150 template <typename T>
151 class FooTest : public testing::Test {
152 };
153
154 template <typename T>
155 class BarTest : public testing::Test {
156 };
157
158 TYPED_TEST_CASE_P(FooTest);
TYPED_TEST_P(FooTest,A)159 TYPED_TEST_P(FooTest, A) {}
160 REGISTER_TYPED_TEST_CASE_P(FooTest, A);
161
162 // Wrong test case name.
163 INSTANTIATE_TYPED_TEST_CASE_P(My, BarTest, testing::Types<int>);
164
165 #elif defined(TEST_CATCHES_INSTANTIATE_TYPED_TESET_CASE_P_WITH_SAME_NAME_PREFIX)
166 // Tests that the compiler catches instantiating TYPED_TEST_CASE_P
167 // twice with the same name prefix.
168
169 #include <gtest/gtest.h>
170
171 template <typename T>
172 class FooTest : public testing::Test {
173 };
174
175 TYPED_TEST_CASE_P(FooTest);
TYPED_TEST_P(FooTest,A)176 TYPED_TEST_P(FooTest, A) {}
177 REGISTER_TYPED_TEST_CASE_P(FooTest, A);
178
179 INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, testing::Types<int>);
180
181 // Wrong name prefix: "My" has been used.
182 INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, testing::Types<double>);
183
184 #elif defined(TEST_STATIC_ASSERT_TYPE_EQ_IS_NOT_A_TYPE)
185
186 #include <gtest/gtest.h>
187
188 // Tests that StaticAssertTypeEq<T1, T2> cannot be used as a type.
189 testing::StaticAssertTypeEq<int, int> dummy;
190
191 #elif defined(TEST_STATIC_ASSERT_TYPE_EQ_WORKS_IN_NAMESPACE)
192
193 #include <gtest/gtest.h>
194
195 // Tests that StaticAssertTypeEq<T1, T2> works in a namespace scope.
196 static bool dummy = testing::StaticAssertTypeEq<int, const int>();
197
198 #elif defined(TEST_STATIC_ASSERT_TYPE_EQ_WORKS_IN_CLASS)
199
200 #include <gtest/gtest.h>
201
202 template <typename T>
203 class Helper {
204 public:
205 // Tests that StaticAssertTypeEq<T1, T2> works in a class.
Helper()206 Helper() { testing::StaticAssertTypeEq<int, T>(); }
207
DoSomething()208 void DoSomething() {}
209 };
210
Test()211 void Test() {
212 Helper<bool> h;
213 h.DoSomething(); // To avoid the "unused variable" warning.
214 }
215
216 #elif defined(TEST_STATIC_ASSERT_TYPE_EQ_WORKS_IN_FUNCTION)
217
218 #include <gtest/gtest.h>
219
Test()220 void Test() {
221 // Tests that StaticAssertTypeEq<T1, T2> works inside a function.
222 testing::StaticAssertTypeEq<const int, int>();
223 }
224
225 #else
226 // A sanity test. This should compile.
227
228 #include <gtest/gtest.h>
229
main()230 int main() {
231 return RUN_ALL_TESTS();
232 }
233
234 #endif
235