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 47int 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 63class MyTest : public testing::Test { 64 protected: 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 74class MyTest : public testing::Test { 75 protected: 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 87class MyEnvironment : public testing::Environment { 88 public: 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 98class MyEnvironment : public testing::Environment { 99 protected: 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 112template <typename T> 113class FooTest : public testing::Test { 114}; 115 116template <typename T> 117class BarTest : public testing::Test { 118}; 119 120TYPED_TEST_CASE_P(FooTest); 121TYPED_TEST_P(BarTest, A) {} // Wrong test case name. 122REGISTER_TYPED_TEST_CASE_P(FooTest, A); 123INSTANTIATE_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 131template <typename T> 132class FooTest : public testing::Test { 133}; 134 135template <typename T> 136class BarTest : public testing::Test { 137}; 138 139TYPED_TEST_CASE_P(FooTest); 140TYPED_TEST_P(FooTest, A) {} 141REGISTER_TYPED_TEST_CASE_P(BarTest, A); // Wrong test case name. 142INSTANTIATE_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 150template <typename T> 151class FooTest : public testing::Test { 152}; 153 154template <typename T> 155class BarTest : public testing::Test { 156}; 157 158TYPED_TEST_CASE_P(FooTest); 159TYPED_TEST_P(FooTest, A) {} 160REGISTER_TYPED_TEST_CASE_P(FooTest, A); 161 162// Wrong test case name. 163INSTANTIATE_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 171template <typename T> 172class FooTest : public testing::Test { 173}; 174 175TYPED_TEST_CASE_P(FooTest); 176TYPED_TEST_P(FooTest, A) {} 177REGISTER_TYPED_TEST_CASE_P(FooTest, A); 178 179INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, testing::Types<int>); 180 181// Wrong name prefix: "My" has been used. 182INSTANTIATE_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. 189testing::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. 196static 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 202template <typename T> 203class Helper { 204 public: 205 // Tests that StaticAssertTypeEq<T1, T2> works in a class. 206 Helper() { testing::StaticAssertTypeEq<int, T>(); } 207 208 void DoSomething() {} 209}; 210 211void 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 220void 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 230int main() { 231 return RUN_ALL_TESTS(); 232} 233 234#endif 235