Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SuperOrxoBros_HS18/src/external/gmock/src/gmock.cc @ 12177

Last change on this file since 12177 was 12177, checked in by siramesh, 5 years ago

Super Orxo Bros Final (Sidharth Ramesh, Nisa Balta, Jeff Ren)

File size: 6.8 KB
Line 
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@google.com (Zhanyong Wan)
31
32#include "gmock/gmock.h"
33#include "gmock/internal/gmock-port.h"
34
35namespace testing {
36
37// TODO(wan@google.com): support using environment variables to
38// control the flag values, like what Google Test does.
39
40GMOCK_DEFINE_bool_(catch_leaked_mocks, true,
41                   "true iff Google Mock should report leaked mock objects "
42                   "as failures.");
43
44GMOCK_DEFINE_string_(verbose, internal::kWarningVerbosity,
45                     "Controls how verbose Google Mock's output is."
46                     "  Valid values:\n"
47                     "  info    - prints all messages.\n"
48                     "  warning - prints warnings and errors.\n"
49                     "  error   - prints errors only.");
50
51namespace internal {
52
53// Parses a string as a command line flag.  The string should have the
54// format "--gmock_flag=value".  When def_optional is true, the
55// "=value" part can be omitted.
56//
57// Returns the value of the flag, or NULL if the parsing failed.
58static const char* ParseGoogleMockFlagValue(const char* str,
59                                            const char* flag,
60                                            bool def_optional) {
61  // str and flag must not be NULL.
62  if (str == NULL || flag == NULL) return NULL;
63
64  // The flag must start with "--gmock_".
65  const String flag_str = String::Format("--gmock_%s", flag);
66  const size_t flag_len = flag_str.length();
67  if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL;
68
69  // Skips the flag name.
70  const char* flag_end = str + flag_len;
71
72  // When def_optional is true, it's OK to not have a "=value" part.
73  if (def_optional && (flag_end[0] == '\0')) {
74    return flag_end;
75  }
76
77  // If def_optional is true and there are more characters after the
78  // flag name, or if def_optional is false, there must be a '=' after
79  // the flag name.
80  if (flag_end[0] != '=') return NULL;
81
82  // Returns the string after "=".
83  return flag_end + 1;
84}
85
86// Parses a string for a Google Mock bool flag, in the form of
87// "--gmock_flag=value".
88//
89// On success, stores the value of the flag in *value, and returns
90// true.  On failure, returns false without changing *value.
91static bool ParseGoogleMockBoolFlag(const char* str, const char* flag,
92                                    bool* value) {
93  // Gets the value of the flag as a string.
94  const char* const value_str = ParseGoogleMockFlagValue(str, flag, true);
95
96  // Aborts if the parsing failed.
97  if (value_str == NULL) return false;
98
99  // Converts the string value to a bool.
100  *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');
101  return true;
102}
103
104// Parses a string for a Google Mock string flag, in the form of
105// "--gmock_flag=value".
106//
107// On success, stores the value of the flag in *value, and returns
108// true.  On failure, returns false without changing *value.
109static bool ParseGoogleMockStringFlag(const char* str, const char* flag,
110                                      String* value) {
111  // Gets the value of the flag as a string.
112  const char* const value_str = ParseGoogleMockFlagValue(str, flag, false);
113
114  // Aborts if the parsing failed.
115  if (value_str == NULL) return false;
116
117  // Sets *value to the value of the flag.
118  *value = value_str;
119  return true;
120}
121
122// The internal implementation of InitGoogleMock().
123//
124// The type parameter CharType can be instantiated to either char or
125// wchar_t.
126template <typename CharType>
127void InitGoogleMockImpl(int* argc, CharType** argv) {
128  // Makes sure Google Test is initialized.  InitGoogleTest() is
129  // idempotent, so it's fine if the user has already called it.
130  InitGoogleTest(argc, argv);
131  if (*argc <= 0) return;
132
133  for (int i = 1; i != *argc; i++) {
134    const String arg_string = StreamableToString(argv[i]);
135    const char* const arg = arg_string.c_str();
136
137    // Do we see a Google Mock flag?
138    if (ParseGoogleMockBoolFlag(arg, "catch_leaked_mocks",
139                                &GMOCK_FLAG(catch_leaked_mocks)) ||
140        ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose))) {
141      // Yes.  Shift the remainder of the argv list left by one.  Note
142      // that argv has (*argc + 1) elements, the last one always being
143      // NULL.  The following loop moves the trailing NULL element as
144      // well.
145      for (int j = i; j != *argc; j++) {
146        argv[j] = argv[j + 1];
147      }
148
149      // Decrements the argument count.
150      (*argc)--;
151
152      // We also need to decrement the iterator as we just removed
153      // an element.
154      i--;
155    }
156  }
157}
158
159}  // namespace internal
160
161// Initializes Google Mock.  This must be called before running the
162// tests.  In particular, it parses a command line for the flags that
163// Google Mock recognizes.  Whenever a Google Mock flag is seen, it is
164// removed from argv, and *argc is decremented.
165//
166// No value is returned.  Instead, the Google Mock flag variables are
167// updated.
168//
169// Since Google Test is needed for Google Mock to work, this function
170// also initializes Google Test and parses its flags, if that hasn't
171// been done.
172void InitGoogleMock(int* argc, char** argv) {
173  internal::InitGoogleMockImpl(argc, argv);
174}
175
176// This overloaded version can be used in Windows programs compiled in
177// UNICODE mode.
178void InitGoogleMock(int* argc, wchar_t** argv) {
179  internal::InitGoogleMockImpl(argc, argv);
180}
181
182}  // namespace testing
Note: See TracBrowser for help on using the repository browser.