1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/testing/gtest/test/gtest_help_test.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,172 @@ 1.4 +#!/usr/bin/env python 1.5 +# 1.6 +# Copyright 2009, Google Inc. 1.7 +# All rights reserved. 1.8 +# 1.9 +# Redistribution and use in source and binary forms, with or without 1.10 +# modification, are permitted provided that the following conditions are 1.11 +# met: 1.12 +# 1.13 +# * Redistributions of source code must retain the above copyright 1.14 +# notice, this list of conditions and the following disclaimer. 1.15 +# * Redistributions in binary form must reproduce the above 1.16 +# copyright notice, this list of conditions and the following disclaimer 1.17 +# in the documentation and/or other materials provided with the 1.18 +# distribution. 1.19 +# * Neither the name of Google Inc. nor the names of its 1.20 +# contributors may be used to endorse or promote products derived from 1.21 +# this software without specific prior written permission. 1.22 +# 1.23 +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.27 +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.28 +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.29 +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.30 +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.31 +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.32 +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.33 +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + 1.35 +"""Tests the --help flag of Google C++ Testing Framework. 1.36 + 1.37 +SYNOPSIS 1.38 + gtest_help_test.py --build_dir=BUILD/DIR 1.39 + # where BUILD/DIR contains the built gtest_help_test_ file. 1.40 + gtest_help_test.py 1.41 +""" 1.42 + 1.43 +__author__ = 'wan@google.com (Zhanyong Wan)' 1.44 + 1.45 +import os 1.46 +import re 1.47 +import gtest_test_utils 1.48 + 1.49 + 1.50 +IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux' 1.51 +IS_WINDOWS = os.name == 'nt' 1.52 + 1.53 +PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_') 1.54 +FLAG_PREFIX = '--gtest_' 1.55 +DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style' 1.56 +STREAM_RESULT_TO_FLAG = FLAG_PREFIX + 'stream_result_to' 1.57 +UNKNOWN_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing' 1.58 +LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests' 1.59 +INCORRECT_FLAG_VARIANTS = [re.sub('^--', '-', LIST_TESTS_FLAG), 1.60 + re.sub('^--', '/', LIST_TESTS_FLAG), 1.61 + re.sub('_', '-', LIST_TESTS_FLAG)] 1.62 +INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing' 1.63 + 1.64 +SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess( 1.65 + [PROGRAM_PATH, LIST_TESTS_FLAG]).output 1.66 + 1.67 +# The help message must match this regex. 1.68 +HELP_REGEX = re.compile( 1.69 + FLAG_PREFIX + r'list_tests.*' + 1.70 + FLAG_PREFIX + r'filter=.*' + 1.71 + FLAG_PREFIX + r'also_run_disabled_tests.*' + 1.72 + FLAG_PREFIX + r'repeat=.*' + 1.73 + FLAG_PREFIX + r'shuffle.*' + 1.74 + FLAG_PREFIX + r'random_seed=.*' + 1.75 + FLAG_PREFIX + r'color=.*' + 1.76 + FLAG_PREFIX + r'print_time.*' + 1.77 + FLAG_PREFIX + r'output=.*' + 1.78 + FLAG_PREFIX + r'break_on_failure.*' + 1.79 + FLAG_PREFIX + r'throw_on_failure.*' + 1.80 + FLAG_PREFIX + r'catch_exceptions=0.*', 1.81 + re.DOTALL) 1.82 + 1.83 + 1.84 +def RunWithFlag(flag): 1.85 + """Runs gtest_help_test_ with the given flag. 1.86 + 1.87 + Returns: 1.88 + the exit code and the text output as a tuple. 1.89 + Args: 1.90 + flag: the command-line flag to pass to gtest_help_test_, or None. 1.91 + """ 1.92 + 1.93 + if flag is None: 1.94 + command = [PROGRAM_PATH] 1.95 + else: 1.96 + command = [PROGRAM_PATH, flag] 1.97 + child = gtest_test_utils.Subprocess(command) 1.98 + return child.exit_code, child.output 1.99 + 1.100 + 1.101 +class GTestHelpTest(gtest_test_utils.TestCase): 1.102 + """Tests the --help flag and its equivalent forms.""" 1.103 + 1.104 + def TestHelpFlag(self, flag): 1.105 + """Verifies correct behavior when help flag is specified. 1.106 + 1.107 + The right message must be printed and the tests must 1.108 + skipped when the given flag is specified. 1.109 + 1.110 + Args: 1.111 + flag: A flag to pass to the binary or None. 1.112 + """ 1.113 + 1.114 + exit_code, output = RunWithFlag(flag) 1.115 + self.assertEquals(0, exit_code) 1.116 + self.assert_(HELP_REGEX.search(output), output) 1.117 + 1.118 + if IS_LINUX: 1.119 + self.assert_(STREAM_RESULT_TO_FLAG in output, output) 1.120 + else: 1.121 + self.assert_(STREAM_RESULT_TO_FLAG not in output, output) 1.122 + 1.123 + if SUPPORTS_DEATH_TESTS and not IS_WINDOWS: 1.124 + self.assert_(DEATH_TEST_STYLE_FLAG in output, output) 1.125 + else: 1.126 + self.assert_(DEATH_TEST_STYLE_FLAG not in output, output) 1.127 + 1.128 + def TestNonHelpFlag(self, flag): 1.129 + """Verifies correct behavior when no help flag is specified. 1.130 + 1.131 + Verifies that when no help flag is specified, the tests are run 1.132 + and the help message is not printed. 1.133 + 1.134 + Args: 1.135 + flag: A flag to pass to the binary or None. 1.136 + """ 1.137 + 1.138 + exit_code, output = RunWithFlag(flag) 1.139 + self.assert_(exit_code != 0) 1.140 + self.assert_(not HELP_REGEX.search(output), output) 1.141 + 1.142 + def testPrintsHelpWithFullFlag(self): 1.143 + self.TestHelpFlag('--help') 1.144 + 1.145 + def testPrintsHelpWithShortFlag(self): 1.146 + self.TestHelpFlag('-h') 1.147 + 1.148 + def testPrintsHelpWithQuestionFlag(self): 1.149 + self.TestHelpFlag('-?') 1.150 + 1.151 + def testPrintsHelpWithWindowsStyleQuestionFlag(self): 1.152 + self.TestHelpFlag('/?') 1.153 + 1.154 + def testPrintsHelpWithUnrecognizedGoogleTestFlag(self): 1.155 + self.TestHelpFlag(UNKNOWN_FLAG) 1.156 + 1.157 + def testPrintsHelpWithIncorrectFlagStyle(self): 1.158 + for incorrect_flag in INCORRECT_FLAG_VARIANTS: 1.159 + self.TestHelpFlag(incorrect_flag) 1.160 + 1.161 + def testRunsTestsWithoutHelpFlag(self): 1.162 + """Verifies that when no help flag is specified, the tests are run 1.163 + and the help message is not printed.""" 1.164 + 1.165 + self.TestNonHelpFlag(None) 1.166 + 1.167 + def testRunsTestsWithGtestInternalFlag(self): 1.168 + """Verifies that the tests are run and no help message is printed when 1.169 + a flag starting with Google Test prefix and 'internal_' is supplied.""" 1.170 + 1.171 + self.TestNonHelpFlag(INTERNAL_FLAG_FOR_TESTING) 1.172 + 1.173 + 1.174 +if __name__ == '__main__': 1.175 + gtest_test_utils.Main()