Home | History | Annotate | Download | only in test
      1 // Copyright 2008, 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 (at) google.com (Zhanyong Wan)
     31 
     32 // Google Mock - a framework for writing C++ mock classes.
     33 //
     34 // This file tests code in gmock.cc.
     35 
     36 #include "gmock/gmock.h"
     37 
     38 #include <string>
     39 #include "gtest/gtest.h"
     40 #include "gtest/internal/custom/gtest.h"
     41 
     42 #if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
     43 
     44 using testing::GMOCK_FLAG(verbose);
     45 using testing::InitGoogleMock;
     46 
     47 // Verifies that calling InitGoogleMock() on argv results in new_argv,
     48 // and the gmock_verbose flag's value is set to expected_gmock_verbose.
     49 template <typename Char, int M, int N>
     50 void TestInitGoogleMock(const Char* (&argv)[M], const Char* (&new_argv)[N],
     51                         const ::std::string& expected_gmock_verbose) {
     52   const ::std::string old_verbose = GMOCK_FLAG(verbose);
     53 
     54   int argc = M;
     55   InitGoogleMock(&argc, const_cast<Char**>(argv));
     56   ASSERT_EQ(N, argc) << "The new argv has wrong number of elements.";
     57 
     58   for (int i = 0; i < N; i++) {
     59     EXPECT_STREQ(new_argv[i], argv[i]);
     60   }
     61 
     62   EXPECT_EQ(expected_gmock_verbose, GMOCK_FLAG(verbose).c_str());
     63   GMOCK_FLAG(verbose) = old_verbose;  // Restores the gmock_verbose flag.
     64 }
     65 
     66 TEST(InitGoogleMockTest, ParsesInvalidCommandLine) {
     67   const char* argv[] = {
     68     NULL
     69   };
     70 
     71   const char* new_argv[] = {
     72     NULL
     73   };
     74 
     75   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
     76 }
     77 
     78 TEST(InitGoogleMockTest, ParsesEmptyCommandLine) {
     79   const char* argv[] = {
     80     "foo.exe",
     81     NULL
     82   };
     83 
     84   const char* new_argv[] = {
     85     "foo.exe",
     86     NULL
     87   };
     88 
     89   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
     90 }
     91 
     92 TEST(InitGoogleMockTest, ParsesSingleFlag) {
     93   const char* argv[] = {
     94     "foo.exe",
     95     "--gmock_verbose=info",
     96     NULL
     97   };
     98 
     99   const char* new_argv[] = {
    100     "foo.exe",
    101     NULL
    102   };
    103 
    104   TestInitGoogleMock(argv, new_argv, "info");
    105 }
    106 
    107 TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) {
    108   const char* argv[] = {
    109     "foo.exe",
    110     "--non_gmock_flag=blah",
    111     NULL
    112   };
    113 
    114   const char* new_argv[] = {
    115     "foo.exe",
    116     "--non_gmock_flag=blah",
    117     NULL
    118   };
    119 
    120   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
    121 }
    122 
    123 TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
    124   const char* argv[] = {
    125     "foo.exe",
    126     "--non_gmock_flag=blah",
    127     "--gmock_verbose=error",
    128     NULL
    129   };
    130 
    131   const char* new_argv[] = {
    132     "foo.exe",
    133     "--non_gmock_flag=blah",
    134     NULL
    135   };
    136 
    137   TestInitGoogleMock(argv, new_argv, "error");
    138 }
    139 
    140 TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) {
    141   const wchar_t* argv[] = {
    142     NULL
    143   };
    144 
    145   const wchar_t* new_argv[] = {
    146     NULL
    147   };
    148 
    149   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
    150 }
    151 
    152 TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) {
    153   const wchar_t* argv[] = {
    154     L"foo.exe",
    155     NULL
    156   };
    157 
    158   const wchar_t* new_argv[] = {
    159     L"foo.exe",
    160     NULL
    161   };
    162 
    163   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
    164 }
    165 
    166 TEST(WideInitGoogleMockTest, ParsesSingleFlag) {
    167   const wchar_t* argv[] = {
    168     L"foo.exe",
    169     L"--gmock_verbose=info",
    170     NULL
    171   };
    172 
    173   const wchar_t* new_argv[] = {
    174     L"foo.exe",
    175     NULL
    176   };
    177 
    178   TestInitGoogleMock(argv, new_argv, "info");
    179 }
    180 
    181 TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) {
    182   const wchar_t* argv[] = {
    183     L"foo.exe",
    184     L"--non_gmock_flag=blah",
    185     NULL
    186   };
    187 
    188   const wchar_t* new_argv[] = {
    189     L"foo.exe",
    190     L"--non_gmock_flag=blah",
    191     NULL
    192   };
    193 
    194   TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
    195 }
    196 
    197 TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
    198   const wchar_t* argv[] = {
    199     L"foo.exe",
    200     L"--non_gmock_flag=blah",
    201     L"--gmock_verbose=error",
    202     NULL
    203   };
    204 
    205   const wchar_t* new_argv[] = {
    206     L"foo.exe",
    207     L"--non_gmock_flag=blah",
    208     NULL
    209   };
    210 
    211   TestInitGoogleMock(argv, new_argv, "error");
    212 }
    213 
    214 #endif  // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
    215 
    216 // Makes sure Google Mock flags can be accessed in code.
    217 TEST(FlagTest, IsAccessibleInCode) {
    218   bool dummy = testing::GMOCK_FLAG(catch_leaked_mocks) &&
    219       testing::GMOCK_FLAG(verbose) == "";
    220   (void)dummy;  // Avoids the "unused local variable" warning.
    221 }
    222