1
// Copyright 2007, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
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
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.
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.
30
// Author: wan@google.com (Zhanyong Wan)
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.
37
// We still need to write the negative-compilation test itself, which
38
// will be tightly coupled with the build environment.
40
// TODO(wan@google.com): finish the negative-compilation test.
42
#ifdef TEST_CANNOT_IGNORE_RUN_ALL_TESTS_RESULT
43
// Tests that the result of RUN_ALL_TESTS() cannot be ignored.
45
#include <gtest/gtest.h>
47
int main(int argc, char** argv) {
48
testing::InitGoogleTest(&argc, argv);
49
RUN_ALL_TESTS(); // This line shouldn't compile.
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.
55
#include "src/gtest-internal-inl.h"
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.
61
#include <gtest/gtest.h>
63
class MyTest : public testing::Test {
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.
72
#include <gtest/gtest.h>
74
class MyTest : public testing::Test {
76
virtual void SetUp() {
77
testing::Test::Setup(); // Tries to call SetUp() in the parent class.
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.
85
#include <gtest/gtest.h>
87
class MyEnvironment : public testing::Environment {
92
#elif defined(TEST_CATCHES_CALLING_SETUP_IN_ENVIRONMENT_WITH_TYPO)
93
// Tests that the compiler catches the typo when a user calls Setup()
96
#include <gtest/gtest.h>
98
class MyEnvironment : public testing::Environment {
100
virtual void SetUp() {
101
// Tries to call SetUp() in the parent class.
102
testing::Environment::Setup();
106
#elif defined(TEST_CATCHES_WRONG_CASE_IN_TYPED_TEST_P)
107
// Tests that the compiler catches using the wrong test case name in
110
#include <gtest/gtest.h>
112
template <typename T>
113
class FooTest : public testing::Test {
116
template <typename T>
117
class BarTest : public testing::Test {
120
TYPED_TEST_CASE_P(FooTest);
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>);
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.
129
#include <gtest/gtest.h>
131
template <typename T>
132
class FooTest : public testing::Test {
135
template <typename T>
136
class BarTest : public testing::Test {
139
TYPED_TEST_CASE_P(FooTest);
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>);
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.
148
#include <gtest/gtest.h>
150
template <typename T>
151
class FooTest : public testing::Test {
154
template <typename T>
155
class BarTest : public testing::Test {
158
TYPED_TEST_CASE_P(FooTest);
159
TYPED_TEST_P(FooTest, A) {}
160
REGISTER_TYPED_TEST_CASE_P(FooTest, A);
162
// Wrong test case name.
163
INSTANTIATE_TYPED_TEST_CASE_P(My, BarTest, testing::Types<int>);
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.
169
#include <gtest/gtest.h>
171
template <typename T>
172
class FooTest : public testing::Test {
175
TYPED_TEST_CASE_P(FooTest);
176
TYPED_TEST_P(FooTest, A) {}
177
REGISTER_TYPED_TEST_CASE_P(FooTest, A);
179
INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, testing::Types<int>);
181
// Wrong name prefix: "My" has been used.
182
INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, testing::Types<double>);
184
#elif defined(TEST_STATIC_ASSERT_TYPE_EQ_IS_NOT_A_TYPE)
186
#include <gtest/gtest.h>
188
// Tests that StaticAssertTypeEq<T1, T2> cannot be used as a type.
189
testing::StaticAssertTypeEq<int, int> dummy;
191
#elif defined(TEST_STATIC_ASSERT_TYPE_EQ_WORKS_IN_NAMESPACE)
193
#include <gtest/gtest.h>
195
// Tests that StaticAssertTypeEq<T1, T2> works in a namespace scope.
196
static bool dummy = testing::StaticAssertTypeEq<int, const int>();
198
#elif defined(TEST_STATIC_ASSERT_TYPE_EQ_WORKS_IN_CLASS)
200
#include <gtest/gtest.h>
202
template <typename T>
205
// Tests that StaticAssertTypeEq<T1, T2> works in a class.
206
Helper() { testing::StaticAssertTypeEq<int, T>(); }
208
void DoSomething() {}
213
h.DoSomething(); // To avoid the "unused variable" warning.
216
#elif defined(TEST_STATIC_ASSERT_TYPE_EQ_WORKS_IN_FUNCTION)
218
#include <gtest/gtest.h>
221
// Tests that StaticAssertTypeEq<T1, T2> works inside a function.
222
testing::StaticAssertTypeEq<const int, int>();
226
// A sanity test. This should compile.
228
#include <gtest/gtest.h>
231
return RUN_ALL_TESTS();