1 #!/usr/bin/env python 2 # 3 # Copyright 2006, Google Inc. 4 # All rights reserved. 5 # 6 # Redistribution and use in source and binary forms, with or without 7 # modification, are permitted provided that the following conditions are 8 # met: 9 # 10 # * Redistributions of source code must retain the above copyright 11 # notice, this list of conditions and the following disclaimer. 12 # * Redistributions in binary form must reproduce the above 13 # copyright notice, this list of conditions and the following disclaimer 14 # in the documentation and/or other materials provided with the 15 # distribution. 16 # * Neither the name of Google Inc. nor the names of its 17 # contributors may be used to endorse or promote products derived from 18 # this software without specific prior written permission. 19 # 20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32 """Unit test for Google Test's --gtest_list_tests flag. 33 34 A user can ask Google Test to list all tests by specifying the 35 --gtest_list_tests flag. This script tests such functionality 36 by invoking gtest_list_tests_unittest_ (a program written with 37 Google Test) the command line flags. 38 """ 39 40 __author__ = 'phanna (at] google.com (Patrick Hanna)' 41 42 import gtest_test_utils 43 import os 44 import re 45 import sys 46 import unittest 47 48 49 # Constants. 50 51 # The command line flag for enabling/disabling listing all tests. 52 LIST_TESTS_FLAG = 'gtest_list_tests' 53 54 # Path to the gtest_list_tests_unittest_ program. 55 EXE_PATH = os.path.join(gtest_test_utils.GetBuildDir(), 56 'gtest_list_tests_unittest_'); 57 58 # The expected output when running gtest_list_tests_unittest_ with 59 # --gtest_list_tests 60 EXPECTED_OUTPUT = """FooDeathTest. 61 Test1 62 Foo. 63 Bar1 64 Bar2 65 Bar3 66 Abc. 67 Xyz 68 Def 69 FooBar. 70 Baz 71 FooTest. 72 Test1 73 Test2 74 Test3 75 """ 76 77 # Utilities. 78 79 def Run(command): 80 """Runs a command and returns the list of tests printed. 81 """ 82 83 stdout_file = os.popen(command, "r") 84 85 output = stdout_file.read() 86 87 stdout_file.close() 88 return output 89 90 91 # The unit test. 92 93 class GTestListTestsUnitTest(unittest.TestCase): 94 """Tests using the --gtest_list_tests flag to list all tests. 95 """ 96 97 def RunAndVerify(self, flag_value, expected_output, other_flag): 98 """Runs gtest_list_tests_unittest_ and verifies that it prints 99 the correct tests. 100 101 Args: 102 flag_value: value of the --gtest_list_tests flag; 103 None if the flag should not be present. 104 105 expected_output: the expected output after running command; 106 107 other_flag: a different flag to be passed to command 108 along with gtest_list_tests; 109 None if the flag should not be present. 110 """ 111 112 if flag_value is None: 113 flag = '' 114 flag_expression = "not set" 115 elif flag_value == '0': 116 flag = ' --%s=0' % LIST_TESTS_FLAG 117 flag_expression = "0" 118 else: 119 flag = ' --%s' % LIST_TESTS_FLAG 120 flag_expression = "1" 121 122 command = EXE_PATH + flag 123 124 if other_flag is not None: 125 command += " " + other_flag 126 127 output = Run(command) 128 129 msg = ('when %s is %s, the output of "%s" is "%s".' % 130 (LIST_TESTS_FLAG, flag_expression, command, output)) 131 132 if expected_output is not None: 133 self.assert_(output == expected_output, msg) 134 else: 135 self.assert_(output != EXPECTED_OUTPUT, msg) 136 137 def testDefaultBehavior(self): 138 """Tests the behavior of the default mode.""" 139 140 self.RunAndVerify(flag_value=None, 141 expected_output=None, 142 other_flag=None) 143 144 def testFlag(self): 145 """Tests using the --gtest_list_tests flag.""" 146 147 self.RunAndVerify(flag_value='0', 148 expected_output=None, 149 other_flag=None) 150 self.RunAndVerify(flag_value='1', 151 expected_output=EXPECTED_OUTPUT, 152 other_flag=None) 153 154 def testOverrideOtherFlags(self): 155 """Tests that --gtest_list_tests overrides all other flags.""" 156 157 self.RunAndVerify(flag_value="1", 158 expected_output=EXPECTED_OUTPUT, 159 other_flag="--gtest_filter=*") 160 self.RunAndVerify(flag_value="1", 161 expected_output=EXPECTED_OUTPUT, 162 other_flag="--gtest_break_on_failure") 163 164 if __name__ == '__main__': 165 gtest_test_utils.Main() 166