1b89a7cc2SEnji Cooper // Copyright 2008, Google Inc.
2b89a7cc2SEnji Cooper // All rights reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b89a7cc2SEnji Cooper 
30b89a7cc2SEnji Cooper #include "gmock/gmock.h"
3128f6c2f2SEnji Cooper 
3228f6c2f2SEnji Cooper #include <string>
3328f6c2f2SEnji Cooper 
34b89a7cc2SEnji Cooper #include "gmock/internal/gmock-port.h"
35b89a7cc2SEnji Cooper 
36b89a7cc2SEnji Cooper GMOCK_DEFINE_bool_(catch_leaked_mocks, true,
3728f6c2f2SEnji Cooper                    "true if and only if Google Mock should report leaked "
3828f6c2f2SEnji Cooper                    "mock objects as failures.");
39b89a7cc2SEnji Cooper 
4028f6c2f2SEnji Cooper GMOCK_DEFINE_string_(verbose, testing::internal::kWarningVerbosity,
41b89a7cc2SEnji Cooper                      "Controls how verbose Google Mock's output is."
42b89a7cc2SEnji Cooper                      "  Valid values:\n"
43b89a7cc2SEnji Cooper                      "  info    - prints all messages.\n"
44b89a7cc2SEnji Cooper                      "  warning - prints warnings and errors.\n"
45b89a7cc2SEnji Cooper                      "  error   - prints errors only.");
46b89a7cc2SEnji Cooper 
47b89a7cc2SEnji Cooper GMOCK_DEFINE_int32_(default_mock_behavior, 1,
48b89a7cc2SEnji Cooper                     "Controls the default behavior of mocks."
49b89a7cc2SEnji Cooper                     "  Valid values:\n"
50b89a7cc2SEnji Cooper                     "  0 - by default, mocks act as NiceMocks.\n"
51b89a7cc2SEnji Cooper                     "  1 - by default, mocks act as NaggyMocks.\n"
52b89a7cc2SEnji Cooper                     "  2 - by default, mocks act as StrictMocks.");
53b89a7cc2SEnji Cooper 
5428f6c2f2SEnji Cooper namespace testing {
55b89a7cc2SEnji Cooper namespace internal {
56b89a7cc2SEnji Cooper 
57b89a7cc2SEnji Cooper // Parses a string as a command line flag.  The string should have the
58b89a7cc2SEnji Cooper // format "--gmock_flag=value".  When def_optional is true, the
59b89a7cc2SEnji Cooper // "=value" part can be omitted.
60b89a7cc2SEnji Cooper //
61b89a7cc2SEnji Cooper // Returns the value of the flag, or NULL if the parsing failed.
ParseGoogleMockFlagValue(const char * str,const char * flag_name,bool def_optional)62b89a7cc2SEnji Cooper static const char* ParseGoogleMockFlagValue(const char* str,
6328f6c2f2SEnji Cooper                                             const char* flag_name,
64b89a7cc2SEnji Cooper                                             bool def_optional) {
65b89a7cc2SEnji Cooper   // str and flag must not be NULL.
6628f6c2f2SEnji Cooper   if (str == nullptr || flag_name == nullptr) return nullptr;
67b89a7cc2SEnji Cooper 
68b89a7cc2SEnji Cooper   // The flag must start with "--gmock_".
6928f6c2f2SEnji Cooper   const std::string flag_name_str = std::string("--gmock_") + flag_name;
7028f6c2f2SEnji Cooper   const size_t flag_name_len = flag_name_str.length();
7128f6c2f2SEnji Cooper   if (strncmp(str, flag_name_str.c_str(), flag_name_len) != 0) return nullptr;
72b89a7cc2SEnji Cooper 
73b89a7cc2SEnji Cooper   // Skips the flag name.
7428f6c2f2SEnji Cooper   const char* flag_end = str + flag_name_len;
75b89a7cc2SEnji Cooper 
76b89a7cc2SEnji Cooper   // When def_optional is true, it's OK to not have a "=value" part.
77b89a7cc2SEnji Cooper   if (def_optional && (flag_end[0] == '\0')) {
78b89a7cc2SEnji Cooper     return flag_end;
79b89a7cc2SEnji Cooper   }
80b89a7cc2SEnji Cooper 
81b89a7cc2SEnji Cooper   // If def_optional is true and there are more characters after the
82b89a7cc2SEnji Cooper   // flag name, or if def_optional is false, there must be a '=' after
83b89a7cc2SEnji Cooper   // the flag name.
8428f6c2f2SEnji Cooper   if (flag_end[0] != '=') return nullptr;
85b89a7cc2SEnji Cooper 
86b89a7cc2SEnji Cooper   // Returns the string after "=".
87b89a7cc2SEnji Cooper   return flag_end + 1;
88b89a7cc2SEnji Cooper }
89b89a7cc2SEnji Cooper 
90b89a7cc2SEnji Cooper // Parses a string for a Google Mock bool flag, in the form of
91b89a7cc2SEnji Cooper // "--gmock_flag=value".
92b89a7cc2SEnji Cooper //
93b89a7cc2SEnji Cooper // On success, stores the value of the flag in *value, and returns
94b89a7cc2SEnji Cooper // true.  On failure, returns false without changing *value.
ParseGoogleMockFlag(const char * str,const char * flag_name,bool * value)9528f6c2f2SEnji Cooper static bool ParseGoogleMockFlag(const char* str, const char* flag_name,
96b89a7cc2SEnji Cooper                                 bool* value) {
97b89a7cc2SEnji Cooper   // Gets the value of the flag as a string.
9828f6c2f2SEnji Cooper   const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, true);
99b89a7cc2SEnji Cooper 
100b89a7cc2SEnji Cooper   // Aborts if the parsing failed.
10128f6c2f2SEnji Cooper   if (value_str == nullptr) return false;
102b89a7cc2SEnji Cooper 
103b89a7cc2SEnji Cooper   // Converts the string value to a bool.
104b89a7cc2SEnji Cooper   *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');
105b89a7cc2SEnji Cooper   return true;
106b89a7cc2SEnji Cooper }
107b89a7cc2SEnji Cooper 
108b89a7cc2SEnji Cooper // Parses a string for a Google Mock string flag, in the form of
109b89a7cc2SEnji Cooper // "--gmock_flag=value".
110b89a7cc2SEnji Cooper //
111b89a7cc2SEnji Cooper // On success, stores the value of the flag in *value, and returns
112b89a7cc2SEnji Cooper // true.  On failure, returns false without changing *value.
113b89a7cc2SEnji Cooper template <typename String>
ParseGoogleMockFlag(const char * str,const char * flag_name,String * value)11428f6c2f2SEnji Cooper static bool ParseGoogleMockFlag(const char* str, const char* flag_name,
115b89a7cc2SEnji Cooper                                 String* value) {
116b89a7cc2SEnji Cooper   // Gets the value of the flag as a string.
11728f6c2f2SEnji Cooper   const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, false);
118b89a7cc2SEnji Cooper 
119b89a7cc2SEnji Cooper   // Aborts if the parsing failed.
12028f6c2f2SEnji Cooper   if (value_str == nullptr) return false;
121b89a7cc2SEnji Cooper 
122b89a7cc2SEnji Cooper   // Sets *value to the value of the flag.
123b89a7cc2SEnji Cooper   *value = value_str;
124b89a7cc2SEnji Cooper   return true;
125b89a7cc2SEnji Cooper }
126b89a7cc2SEnji Cooper 
ParseGoogleMockFlag(const char * str,const char * flag_name,int32_t * value)12728f6c2f2SEnji Cooper static bool ParseGoogleMockFlag(const char* str, const char* flag_name,
12828f6c2f2SEnji Cooper                                 int32_t* value) {
129b89a7cc2SEnji Cooper   // Gets the value of the flag as a string.
13028f6c2f2SEnji Cooper   const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, true);
131b89a7cc2SEnji Cooper 
132b89a7cc2SEnji Cooper   // Aborts if the parsing failed.
13328f6c2f2SEnji Cooper   if (value_str == nullptr) return false;
134b89a7cc2SEnji Cooper 
135b89a7cc2SEnji Cooper   // Sets *value to the value of the flag.
13628f6c2f2SEnji Cooper   return ParseInt32(Message() << "The value of flag --" << flag_name, value_str,
13728f6c2f2SEnji Cooper                     value);
138b89a7cc2SEnji Cooper }
139b89a7cc2SEnji Cooper 
140b89a7cc2SEnji Cooper // The internal implementation of InitGoogleMock().
141b89a7cc2SEnji Cooper //
142b89a7cc2SEnji Cooper // The type parameter CharType can be instantiated to either char or
143b89a7cc2SEnji Cooper // wchar_t.
144b89a7cc2SEnji Cooper template <typename CharType>
InitGoogleMockImpl(int * argc,CharType ** argv)145b89a7cc2SEnji Cooper void InitGoogleMockImpl(int* argc, CharType** argv) {
146b89a7cc2SEnji Cooper   // Makes sure Google Test is initialized.  InitGoogleTest() is
147b89a7cc2SEnji Cooper   // idempotent, so it's fine if the user has already called it.
148b89a7cc2SEnji Cooper   InitGoogleTest(argc, argv);
149b89a7cc2SEnji Cooper   if (*argc <= 0) return;
150b89a7cc2SEnji Cooper 
151b89a7cc2SEnji Cooper   for (int i = 1; i != *argc; i++) {
152b89a7cc2SEnji Cooper     const std::string arg_string = StreamableToString(argv[i]);
153b89a7cc2SEnji Cooper     const char* const arg = arg_string.c_str();
154b89a7cc2SEnji Cooper 
155b89a7cc2SEnji Cooper     // Do we see a Google Mock flag?
15628f6c2f2SEnji Cooper     bool found_gmock_flag = false;
15728f6c2f2SEnji Cooper 
15828f6c2f2SEnji Cooper #define GMOCK_INTERNAL_PARSE_FLAG(flag_name)            \
15928f6c2f2SEnji Cooper   if (!found_gmock_flag) {                              \
16028f6c2f2SEnji Cooper     auto value = GMOCK_FLAG_GET(flag_name);             \
16128f6c2f2SEnji Cooper     if (ParseGoogleMockFlag(arg, #flag_name, &value)) { \
16228f6c2f2SEnji Cooper       GMOCK_FLAG_SET(flag_name, value);                 \
16328f6c2f2SEnji Cooper       found_gmock_flag = true;                          \
16428f6c2f2SEnji Cooper     }                                                   \
16528f6c2f2SEnji Cooper   }
16628f6c2f2SEnji Cooper 
16728f6c2f2SEnji Cooper     GMOCK_INTERNAL_PARSE_FLAG(catch_leaked_mocks)
16828f6c2f2SEnji Cooper     GMOCK_INTERNAL_PARSE_FLAG(verbose)
16928f6c2f2SEnji Cooper     GMOCK_INTERNAL_PARSE_FLAG(default_mock_behavior)
17028f6c2f2SEnji Cooper 
17128f6c2f2SEnji Cooper     if (found_gmock_flag) {
172b89a7cc2SEnji Cooper       // Yes.  Shift the remainder of the argv list left by one.  Note
173b89a7cc2SEnji Cooper       // that argv has (*argc + 1) elements, the last one always being
174b89a7cc2SEnji Cooper       // NULL.  The following loop moves the trailing NULL element as
175b89a7cc2SEnji Cooper       // well.
176b89a7cc2SEnji Cooper       for (int j = i; j != *argc; j++) {
177b89a7cc2SEnji Cooper         argv[j] = argv[j + 1];
178b89a7cc2SEnji Cooper       }
179b89a7cc2SEnji Cooper 
180b89a7cc2SEnji Cooper       // Decrements the argument count.
181b89a7cc2SEnji Cooper       (*argc)--;
182b89a7cc2SEnji Cooper 
183b89a7cc2SEnji Cooper       // We also need to decrement the iterator as we just removed
184b89a7cc2SEnji Cooper       // an element.
185b89a7cc2SEnji Cooper       i--;
186b89a7cc2SEnji Cooper     }
187b89a7cc2SEnji Cooper   }
188b89a7cc2SEnji Cooper }
189b89a7cc2SEnji Cooper 
190b89a7cc2SEnji Cooper }  // namespace internal
191b89a7cc2SEnji Cooper 
192b89a7cc2SEnji Cooper // Initializes Google Mock.  This must be called before running the
193b89a7cc2SEnji Cooper // tests.  In particular, it parses a command line for the flags that
194b89a7cc2SEnji Cooper // Google Mock recognizes.  Whenever a Google Mock flag is seen, it is
195b89a7cc2SEnji Cooper // removed from argv, and *argc is decremented.
196b89a7cc2SEnji Cooper //
197b89a7cc2SEnji Cooper // No value is returned.  Instead, the Google Mock flag variables are
198b89a7cc2SEnji Cooper // updated.
199b89a7cc2SEnji Cooper //
200b89a7cc2SEnji Cooper // Since Google Test is needed for Google Mock to work, this function
201b89a7cc2SEnji Cooper // also initializes Google Test and parses its flags, if that hasn't
202b89a7cc2SEnji Cooper // been done.
InitGoogleMock(int * argc,char ** argv)203b89a7cc2SEnji Cooper GTEST_API_ void InitGoogleMock(int* argc, char** argv) {
204b89a7cc2SEnji Cooper   internal::InitGoogleMockImpl(argc, argv);
205b89a7cc2SEnji Cooper }
206b89a7cc2SEnji Cooper 
207b89a7cc2SEnji Cooper // This overloaded version can be used in Windows programs compiled in
208b89a7cc2SEnji Cooper // UNICODE mode.
InitGoogleMock(int * argc,wchar_t ** argv)209b89a7cc2SEnji Cooper GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) {
210b89a7cc2SEnji Cooper   internal::InitGoogleMockImpl(argc, argv);
211b89a7cc2SEnji Cooper }
212b89a7cc2SEnji Cooper 
21328f6c2f2SEnji Cooper // This overloaded version can be used on Arduino/embedded platforms where
21428f6c2f2SEnji Cooper // there is no argc/argv.
InitGoogleMock()21528f6c2f2SEnji Cooper GTEST_API_ void InitGoogleMock() {
21628f6c2f2SEnji Cooper   // Since Arduino doesn't have a command line, fake out the argc/argv arguments
21728f6c2f2SEnji Cooper   int argc = 1;
21828f6c2f2SEnji Cooper   const auto arg0 = "dummy";
21928f6c2f2SEnji Cooper   char* argv0 = const_cast<char*>(arg0);
22028f6c2f2SEnji Cooper   char** argv = &argv0;
22128f6c2f2SEnji Cooper 
22228f6c2f2SEnji Cooper   internal::InitGoogleMockImpl(&argc, argv);
22328f6c2f2SEnji Cooper }
22428f6c2f2SEnji Cooper 
225b89a7cc2SEnji Cooper }  // namespace testing
226