gtest_help_test.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2009, Google Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are
00008 # met:
00009 #
00010 #     * Redistributions of source code must retain the above copyright
00011 # notice, this list of conditions and the following disclaimer.
00012 #     * Redistributions in binary form must reproduce the above
00013 # copyright notice, this list of conditions and the following disclaimer
00014 # in the documentation and/or other materials provided with the
00015 # distribution.
00016 #     * Neither the name of Google Inc. nor the names of its
00017 # contributors may be used to endorse or promote products derived from
00018 # this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00023 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00024 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00026 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00027 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00028 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00030 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 
00032 """Tests the --help flag of Google C++ Testing Framework.
00033 
00034 SYNOPSIS
00035        gtest_help_test.py --build_dir=BUILD/DIR
00036          # where BUILD/DIR contains the built gtest_help_test_ file.
00037        gtest_help_test.py
00038 """
00039 
00040 __author__ = 'wan@google.com (Zhanyong Wan)'
00041 
00042 import os
00043 import re
00044 import gtest_test_utils
00045 
00046 
00047 IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux'
00048 IS_WINDOWS = os.name == 'nt'
00049 
00050 PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_')
00051 FLAG_PREFIX = '--gtest_'
00052 DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style'
00053 STREAM_RESULT_TO_FLAG = FLAG_PREFIX + 'stream_result_to'
00054 UNKNOWN_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing'
00055 LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests'
00056 INCORRECT_FLAG_VARIANTS = [re.sub('^--', '-', LIST_TESTS_FLAG),
00057                            re.sub('^--', '/', LIST_TESTS_FLAG),
00058                            re.sub('_', '-', LIST_TESTS_FLAG)]
00059 INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing'
00060 
00061 SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess(
00062     [PROGRAM_PATH, LIST_TESTS_FLAG]).output
00063 
00064 # The help message must match this regex.
00065 HELP_REGEX = re.compile(
00066     FLAG_PREFIX + r'list_tests.*' +
00067     FLAG_PREFIX + r'filter=.*' +
00068     FLAG_PREFIX + r'also_run_disabled_tests.*' +
00069     FLAG_PREFIX + r'repeat=.*' +
00070     FLAG_PREFIX + r'shuffle.*' +
00071     FLAG_PREFIX + r'random_seed=.*' +
00072     FLAG_PREFIX + r'color=.*' +
00073     FLAG_PREFIX + r'print_time.*' +
00074     FLAG_PREFIX + r'output=.*' +
00075     FLAG_PREFIX + r'break_on_failure.*' +
00076     FLAG_PREFIX + r'throw_on_failure.*' +
00077     FLAG_PREFIX + r'catch_exceptions=0.*',
00078     re.DOTALL)
00079 
00080 
00081 def RunWithFlag(flag):
00082   """Runs gtest_help_test_ with the given flag.
00083 
00084   Returns:
00085     the exit code and the text output as a tuple.
00086   Args:
00087     flag: the command-line flag to pass to gtest_help_test_, or None.
00088   """
00089 
00090   if flag is None:
00091     command = [PROGRAM_PATH]
00092   else:
00093     command = [PROGRAM_PATH, flag]
00094   child = gtest_test_utils.Subprocess(command)
00095   return child.exit_code, child.output
00096 
00097 
00098 class GTestHelpTest(gtest_test_utils.TestCase):
00099   """Tests the --help flag and its equivalent forms."""
00100 
00101   def TestHelpFlag(self, flag):
00102     """Verifies correct behavior when help flag is specified.
00103 
00104     The right message must be printed and the tests must
00105     skipped when the given flag is specified.
00106 
00107     Args:
00108       flag:  A flag to pass to the binary or None.
00109     """
00110 
00111     exit_code, output = RunWithFlag(flag)
00112     self.assertEquals(0, exit_code)
00113     self.assert_(HELP_REGEX.search(output), output)
00114 
00115     if IS_LINUX:
00116       self.assert_(STREAM_RESULT_TO_FLAG in output, output)
00117     else:
00118       self.assert_(STREAM_RESULT_TO_FLAG not in output, output)
00119 
00120     if SUPPORTS_DEATH_TESTS and not IS_WINDOWS:
00121       self.assert_(DEATH_TEST_STYLE_FLAG in output, output)
00122     else:
00123       self.assert_(DEATH_TEST_STYLE_FLAG not in output, output)
00124 
00125   def TestNonHelpFlag(self, flag):
00126     """Verifies correct behavior when no help flag is specified.
00127 
00128     Verifies that when no help flag is specified, the tests are run
00129     and the help message is not printed.
00130 
00131     Args:
00132       flag:  A flag to pass to the binary or None.
00133     """
00134 
00135     exit_code, output = RunWithFlag(flag)
00136     self.assert_(exit_code != 0)
00137     self.assert_(not HELP_REGEX.search(output), output)
00138 
00139   def testPrintsHelpWithFullFlag(self):
00140     self.TestHelpFlag('--help')
00141 
00142   def testPrintsHelpWithShortFlag(self):
00143     self.TestHelpFlag('-h')
00144 
00145   def testPrintsHelpWithQuestionFlag(self):
00146     self.TestHelpFlag('-?')
00147 
00148   def testPrintsHelpWithWindowsStyleQuestionFlag(self):
00149     self.TestHelpFlag('/?')
00150 
00151   def testPrintsHelpWithUnrecognizedGoogleTestFlag(self):
00152     self.TestHelpFlag(UNKNOWN_FLAG)
00153 
00154   def testPrintsHelpWithIncorrectFlagStyle(self):
00155     for incorrect_flag in INCORRECT_FLAG_VARIANTS:
00156       self.TestHelpFlag(incorrect_flag)
00157 
00158   def testRunsTestsWithoutHelpFlag(self):
00159     """Verifies that when no help flag is specified, the tests are run
00160     and the help message is not printed."""
00161 
00162     self.TestNonHelpFlag(None)
00163 
00164   def testRunsTestsWithGtestInternalFlag(self):
00165     """Verifies that the tests are run and no help message is printed when
00166     a flag starting with Google Test prefix and 'internal_' is supplied."""
00167 
00168     self.TestNonHelpFlag(INTERNAL_FLAG_FOR_TESTING)
00169 
00170 
00171 if __name__ == '__main__':
00172   gtest_test_utils.Main()


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04