blob: c017917d7eddb63a2cae12c6d05def6fdefaf332 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// 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
zhanyong.wane0d051e2009-02-19 00:33:37 +000037GMOCK_DEFINE_string_(verbose, internal::kWarningVerbosity,
38 "Controls how verbose Google Mock's output is."
39 " Valid values:\n"
40 " info - prints all messages.\n"
41 " warning - prints warnings and errors.\n"
42 " error - prints errors only.");
shiqiane35fdd92008-12-10 05:08:54 +000043
44namespace internal {
45
46// Parses a string as a command line flag. The string should have the
47// format "--gmock_flag=value". When def_optional is true, the
48// "=value" part can be omitted.
49//
50// Returns the value of the flag, or NULL if the parsing failed.
51static const char* ParseGoogleMockFlagValue(const char* str,
52 const char* flag,
53 bool def_optional) {
54 // str and flag must not be NULL.
55 if (str == NULL || flag == NULL) return NULL;
56
57 // The flag must start with "--gmock_".
58 const String flag_str = String::Format("--gmock_%s", flag);
59 const size_t flag_len = flag_str.GetLength();
60 if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL;
61
62 // Skips the flag name.
63 const char* flag_end = str + flag_len;
64
65 // When def_optional is true, it's OK to not have a "=value" part.
66 if (def_optional && (flag_end[0] == '\0')) {
67 return flag_end;
68 }
69
70 // If def_optional is true and there are more characters after the
71 // flag name, or if def_optional is false, there must be a '=' after
72 // the flag name.
73 if (flag_end[0] != '=') return NULL;
74
75 // Returns the string after "=".
76 return flag_end + 1;
77}
78
79// Parses a string for a Google Mock string flag, in the form of
80// "--gmock_flag=value".
81//
82// On success, stores the value of the flag in *value, and returns
83// true. On failure, returns false without changing *value.
84static bool ParseGoogleMockStringFlag(const char* str, const char* flag,
85 String* value) {
86 // Gets the value of the flag as a string.
87 const char* const value_str = ParseGoogleMockFlagValue(str, flag, false);
88
89 // Aborts if the parsing failed.
90 if (value_str == NULL) return false;
91
92 // Sets *value to the value of the flag.
93 *value = value_str;
94 return true;
95}
96
97// The internal implementation of InitGoogleMock().
98//
99// The type parameter CharType can be instantiated to either char or
100// wchar_t.
101template <typename CharType>
102void InitGoogleMockImpl(int* argc, CharType** argv) {
103 // Makes sure Google Test is initialized. InitGoogleTest() is
104 // idempotent, so it's fine if the user has already called it.
105 InitGoogleTest(argc, argv);
106 if (*argc <= 0) return;
107
108 for (int i = 1; i != *argc; i++) {
109 const String arg_string = StreamableToString(argv[i]);
110 const char* const arg = arg_string.c_str();
111
112 // Do we see a Google Mock flag?
113 if (ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose))) {
114 // Yes. Shift the remainder of the argv list left by one. Note
115 // that argv has (*argc + 1) elements, the last one always being
116 // NULL. The following loop moves the trailing NULL element as
117 // well.
118 for (int j = i; j != *argc; j++) {
119 argv[j] = argv[j + 1];
120 }
121
122 // Decrements the argument count.
123 (*argc)--;
124
125 // We also need to decrement the iterator as we just removed
126 // an element.
127 i--;
128 }
129 }
130}
131
132} // namespace internal
133
134// Initializes Google Mock. This must be called before running the
135// tests. In particular, it parses a command line for the flags that
136// Google Mock recognizes. Whenever a Google Mock flag is seen, it is
137// removed from argv, and *argc is decremented.
138//
139// No value is returned. Instead, the Google Mock flag variables are
140// updated.
141//
142// Since Google Test is needed for Google Mock to work, this function
143// also initializes Google Test and parses its flags, if that hasn't
144// been done.
145void InitGoogleMock(int* argc, char** argv) {
146 internal::InitGoogleMockImpl(argc, argv);
147}
148
149// This overloaded version can be used in Windows programs compiled in
150// UNICODE mode.
151void InitGoogleMock(int* argc, wchar_t** argv) {
152 internal::InitGoogleMockImpl(argc, argv);
153}
154
155} // namespace testing