Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
yangql
googletest
Commits
6bfc4b2b
Commit
6bfc4b2b
authored
Oct 22, 2009
by
vladlosev
Browse files
Prints help when encountering unrecognized Google Test flags.
parent
bad778ca
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
73 additions
and
6 deletions
+73
-6
include/gtest/internal/gtest-port.h
include/gtest/internal/gtest-port.h
+1
-0
src/gtest.cc
src/gtest.cc
+31
-1
test/gtest_help_test.py
test/gtest_help_test.py
+41
-5
No files found.
include/gtest/internal/gtest-port.h
View file @
6bfc4b2b
...
@@ -169,6 +169,7 @@
...
@@ -169,6 +169,7 @@
#define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
#define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
#define GTEST_FLAG_PREFIX_ "gtest_"
#define GTEST_FLAG_PREFIX_ "gtest_"
#define GTEST_FLAG_PREFIX_DASH_ "gtest-"
#define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
#define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
#define GTEST_NAME_ "Google Test"
#define GTEST_NAME_ "Google Test"
#define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/"
#define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/"
...
...
src/gtest.cc
View file @
6bfc4b2b
...
@@ -4444,6 +4444,33 @@ bool ParseStringFlag(const char* str, const char* flag, String* value) {
...
@@ -4444,6 +4444,33 @@ bool ParseStringFlag(const char* str, const char* flag, String* value) {
return
true
;
return
true
;
}
}
// Determines whether a string pointed by *str has the prefix parameter as
// its prefix and advances it to point past the prefix if it does.
bool
SkipPrefix
(
const
char
*
prefix
,
const
char
**
str
)
{
const
int
prefix_len
=
strlen
(
prefix
);
if
(
strncmp
(
*
str
,
prefix
,
prefix_len
)
!=
0
)
return
false
;
*
str
+=
prefix_len
;
return
true
;
}
// Determines whether a string has a prefix that Google Test uses for its
// flags, i.e., starts with GTEST_FLAG_PREFIX_ or GTEST_FLAG_PREFIX_DASH_.
// If Google Test detects that a command line flag has its prefix but is not
// recognized, it will print its help message. Flags starting with
// GTEST_INTERNAL_PREFIX_ followed by "internal_" are considered Google Test
// internal flags and do not trigger the help message.
bool
HasGoogleTestFlagPrefix
(
const
char
*
str
)
{
return
(
SkipPrefix
(
"--"
,
&
str
)
||
SkipPrefix
(
"-"
,
&
str
)
||
SkipPrefix
(
"/"
,
&
str
))
&&
!
SkipPrefix
(
GTEST_FLAG_PREFIX_
"internal_"
,
&
str
)
&&
(
SkipPrefix
(
GTEST_FLAG_PREFIX_
,
&
str
)
||
SkipPrefix
(
GTEST_FLAG_PREFIX_DASH_
,
&
str
));
}
// Prints a string containing code-encoded text. The following escape
// Prints a string containing code-encoded text. The following escape
// sequences can be used in the string to control the text color:
// sequences can be used in the string to control the text color:
//
//
...
@@ -4601,7 +4628,10 @@ void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) {
...
@@ -4601,7 +4628,10 @@ void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) {
// an element.
// an element.
i
--
;
i
--
;
}
else
if
(
arg_string
==
"--help"
||
arg_string
==
"-h"
||
}
else
if
(
arg_string
==
"--help"
||
arg_string
==
"-h"
||
arg_string
==
"-?"
||
arg_string
==
"/?"
)
{
arg_string
==
"-?"
||
arg_string
==
"/?"
||
HasGoogleTestFlagPrefix
(
arg
))
{
// Both help flag and unrecognized Google Test flags (excluding
// internal ones) trigger help display.
g_help_flag
=
true
;
g_help_flag
=
true
;
}
}
}
}
...
...
test/gtest_help_test.py
View file @
6bfc4b2b
...
@@ -50,6 +50,11 @@ PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_')
...
@@ -50,6 +50,11 @@ PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_')
FLAG_PREFIX
=
'--gtest_'
FLAG_PREFIX
=
'--gtest_'
CATCH_EXCEPTIONS_FLAG
=
FLAG_PREFIX
+
'catch_exceptions'
CATCH_EXCEPTIONS_FLAG
=
FLAG_PREFIX
+
'catch_exceptions'
DEATH_TEST_STYLE_FLAG
=
FLAG_PREFIX
+
'death_test_style'
DEATH_TEST_STYLE_FLAG
=
FLAG_PREFIX
+
'death_test_style'
UNKNOWN_FLAG
=
FLAG_PREFIX
+
'unknown_flag_for_testing'
INCORRECT_FLAG_VARIANTS
=
[
re
.
sub
(
'^--'
,
'-'
,
DEATH_TEST_STYLE_FLAG
),
re
.
sub
(
'^--'
,
'/'
,
DEATH_TEST_STYLE_FLAG
),
re
.
sub
(
'_'
,
'-'
,
DEATH_TEST_STYLE_FLAG
)]
INTERNAL_FLAG_FOR_TESTING
=
FLAG_PREFIX
+
'internal_flag_for_testing'
# The help message must match this regex.
# The help message must match this regex.
HELP_REGEX
=
re
.
compile
(
HELP_REGEX
=
re
.
compile
(
...
@@ -88,8 +93,14 @@ class GTestHelpTest(gtest_test_utils.TestCase):
...
@@ -88,8 +93,14 @@ class GTestHelpTest(gtest_test_utils.TestCase):
"""Tests the --help flag and its equivalent forms."""
"""Tests the --help flag and its equivalent forms."""
def
TestHelpFlag
(
self
,
flag
):
def
TestHelpFlag
(
self
,
flag
):
"""Verifies that the right message is printed and the tests are
"""Verifies correct behavior when help flag is specified.
skipped when the given flag is specified."""
The right message must be printed and the tests must
skipped when the given flag is specified.
Args:
flag: A flag to pass to the binary or None.
"""
exit_code
,
output
=
RunWithFlag
(
flag
)
exit_code
,
output
=
RunWithFlag
(
flag
)
self
.
assertEquals
(
0
,
exit_code
)
self
.
assertEquals
(
0
,
exit_code
)
...
@@ -101,6 +112,20 @@ class GTestHelpTest(gtest_test_utils.TestCase):
...
@@ -101,6 +112,20 @@ class GTestHelpTest(gtest_test_utils.TestCase):
self
.
assert_
(
CATCH_EXCEPTIONS_FLAG
not
in
output
,
output
)
self
.
assert_
(
CATCH_EXCEPTIONS_FLAG
not
in
output
,
output
)
self
.
assert_
(
DEATH_TEST_STYLE_FLAG
in
output
,
output
)
self
.
assert_
(
DEATH_TEST_STYLE_FLAG
in
output
,
output
)
def
TestNonHelpFlag
(
self
,
flag
):
"""Verifies correct behavior when no help flag is specified.
Verifies that when no help flag is specified, the tests are run
and the help message is not printed.
Args:
flag: A flag to pass to the binary or None.
"""
exit_code
,
output
=
RunWithFlag
(
flag
)
self
.
assert_
(
exit_code
!=
0
)
self
.
assert_
(
not
HELP_REGEX
.
search
(
output
),
output
)
def
testPrintsHelpWithFullFlag
(
self
):
def
testPrintsHelpWithFullFlag
(
self
):
self
.
TestHelpFlag
(
'--help'
)
self
.
TestHelpFlag
(
'--help'
)
...
@@ -113,13 +138,24 @@ class GTestHelpTest(gtest_test_utils.TestCase):
...
@@ -113,13 +138,24 @@ class GTestHelpTest(gtest_test_utils.TestCase):
def
testPrintsHelpWithWindowsStyleQuestionFlag
(
self
):
def
testPrintsHelpWithWindowsStyleQuestionFlag
(
self
):
self
.
TestHelpFlag
(
'/?'
)
self
.
TestHelpFlag
(
'/?'
)
def
testPrintsHelpWithUnrecognizedGoogleTestFlag
(
self
):
self
.
TestHelpFlag
(
UNKNOWN_FLAG
)
def
testPrintsHelpWithIncorrectFlagStyle
(
self
):
for
incorrect_flag
in
INCORRECT_FLAG_VARIANTS
:
self
.
TestHelpFlag
(
incorrect_flag
)
def
testRunsTestsWithoutHelpFlag
(
self
):
def
testRunsTestsWithoutHelpFlag
(
self
):
"""Verifies that when no help flag is specified, the tests are run
"""Verifies that when no help flag is specified, the tests are run
and the help message is not printed."""
and the help message is not printed."""
exit_code
,
output
=
RunWithFlag
(
None
)
self
.
TestNonHelpFlag
(
None
)
self
.
assert_
(
exit_code
!=
0
)
self
.
assert_
(
not
HELP_REGEX
.
search
(
output
),
output
)
def
testRunsTestsWithGtestInternalFlag
(
self
):
"""Verifies that the tests are run and no help message is printed when
a flag starting with Google Test prefix and 'internal_' is supplied."""
self
.
TestNonHelpFlag
(
INTERNAL_FLAG_FOR_TESTING
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment