Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 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 |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 36 | by invoking googletest-list-tests-unittest_ (a program written with |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 37 | Google Test) the command line flags. |
| 38 | """ |
| 39 | |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 40 | import re |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 41 | import gtest_test_utils |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 42 | |
| 43 | # Constants. |
| 44 | |
| 45 | # The command line flag for enabling/disabling listing all tests. |
| 46 | LIST_TESTS_FLAG = 'gtest_list_tests' |
| 47 | |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 48 | # Path to the googletest-list-tests-unittest_ program. |
| 49 | EXE_PATH = gtest_test_utils.GetTestExecutablePath('googletest-list-tests-unittest_') |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 50 | |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 51 | # The expected output when running googletest-list-tests-unittest_ with |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 52 | # --gtest_list_tests |
| 53 | EXPECTED_OUTPUT_NO_FILTER_RE = re.compile(r"""FooDeathTest\. |
| 54 | Test1 |
| 55 | Foo\. |
| 56 | Bar1 |
| 57 | Bar2 |
| 58 | DISABLED_Bar3 |
| 59 | Abc\. |
| 60 | Xyz |
| 61 | Def |
| 62 | FooBar\. |
| 63 | Baz |
| 64 | FooTest\. |
| 65 | Test1 |
| 66 | DISABLED_Test2 |
| 67 | Test3 |
| 68 | TypedTest/0\. # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\. |
| 69 | TestA |
| 70 | TestB |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 71 | TypedTest/1\. # TypeParam = int\s*\*( __ptr64)? |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 72 | TestA |
| 73 | TestB |
| 74 | TypedTest/2\. # TypeParam = .*MyArray<bool,\s*42> |
| 75 | TestA |
| 76 | TestB |
| 77 | My/TypeParamTest/0\. # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\. |
| 78 | TestA |
| 79 | TestB |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 80 | My/TypeParamTest/1\. # TypeParam = int\s*\*( __ptr64)? |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 81 | TestA |
| 82 | TestB |
| 83 | My/TypeParamTest/2\. # TypeParam = .*MyArray<bool,\s*42> |
| 84 | TestA |
| 85 | TestB |
| 86 | MyInstantiation/ValueParamTest\. |
| 87 | TestA/0 # GetParam\(\) = one line |
| 88 | TestA/1 # GetParam\(\) = two\\nlines |
| 89 | TestA/2 # GetParam\(\) = a very\\nlo{241}\.\.\. |
| 90 | TestB/0 # GetParam\(\) = one line |
| 91 | TestB/1 # GetParam\(\) = two\\nlines |
| 92 | TestB/2 # GetParam\(\) = a very\\nlo{241}\.\.\. |
| 93 | """) |
| 94 | |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 95 | # The expected output when running googletest-list-tests-unittest_ with |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 96 | # --gtest_list_tests and --gtest_filter=Foo*. |
| 97 | EXPECTED_OUTPUT_FILTER_FOO_RE = re.compile(r"""FooDeathTest\. |
| 98 | Test1 |
| 99 | Foo\. |
| 100 | Bar1 |
| 101 | Bar2 |
| 102 | DISABLED_Bar3 |
| 103 | FooBar\. |
| 104 | Baz |
| 105 | FooTest\. |
| 106 | Test1 |
| 107 | DISABLED_Test2 |
| 108 | Test3 |
| 109 | """) |
| 110 | |
| 111 | # Utilities. |
| 112 | |
| 113 | |
| 114 | def Run(args): |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 115 | """Runs googletest-list-tests-unittest_ and returns the list of tests printed.""" |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 116 | |
| 117 | return gtest_test_utils.Subprocess([EXE_PATH] + args, |
| 118 | capture_stderr=False).output |
| 119 | |
| 120 | |
| 121 | # The unit test. |
| 122 | |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 123 | |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 124 | class GTestListTestsUnitTest(gtest_test_utils.TestCase): |
| 125 | """Tests using the --gtest_list_tests flag to list all tests.""" |
| 126 | |
| 127 | def RunAndVerify(self, flag_value, expected_output_re, other_flag): |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 128 | """Runs googletest-list-tests-unittest_ and verifies that it prints |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 129 | the correct tests. |
| 130 | |
| 131 | Args: |
| 132 | flag_value: value of the --gtest_list_tests flag; |
| 133 | None if the flag should not be present. |
| 134 | expected_output_re: regular expression that matches the expected |
| 135 | output after running command; |
| 136 | other_flag: a different flag to be passed to command |
| 137 | along with gtest_list_tests; |
| 138 | None if the flag should not be present. |
| 139 | """ |
| 140 | |
| 141 | if flag_value is None: |
| 142 | flag = '' |
| 143 | flag_expression = 'not set' |
| 144 | elif flag_value == '0': |
| 145 | flag = '--%s=0' % LIST_TESTS_FLAG |
| 146 | flag_expression = '0' |
| 147 | else: |
| 148 | flag = '--%s' % LIST_TESTS_FLAG |
| 149 | flag_expression = '1' |
| 150 | |
| 151 | args = [flag] |
| 152 | |
| 153 | if other_flag is not None: |
| 154 | args += [other_flag] |
| 155 | |
| 156 | output = Run(args) |
| 157 | |
| 158 | if expected_output_re: |
| 159 | self.assert_( |
| 160 | expected_output_re.match(output), |
| 161 | ('when %s is %s, the output of "%s" is "%s",\n' |
| 162 | 'which does not match regex "%s"' % |
| 163 | (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output, |
| 164 | expected_output_re.pattern))) |
| 165 | else: |
| 166 | self.assert_( |
| 167 | not EXPECTED_OUTPUT_NO_FILTER_RE.match(output), |
| 168 | ('when %s is %s, the output of "%s" is "%s"'% |
| 169 | (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output))) |
| 170 | |
| 171 | def testDefaultBehavior(self): |
| 172 | """Tests the behavior of the default mode.""" |
| 173 | |
| 174 | self.RunAndVerify(flag_value=None, |
| 175 | expected_output_re=None, |
| 176 | other_flag=None) |
| 177 | |
| 178 | def testFlag(self): |
| 179 | """Tests using the --gtest_list_tests flag.""" |
| 180 | |
| 181 | self.RunAndVerify(flag_value='0', |
| 182 | expected_output_re=None, |
| 183 | other_flag=None) |
| 184 | self.RunAndVerify(flag_value='1', |
| 185 | expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE, |
| 186 | other_flag=None) |
| 187 | |
| 188 | def testOverrideNonFilterFlags(self): |
| 189 | """Tests that --gtest_list_tests overrides the non-filter flags.""" |
| 190 | |
| 191 | self.RunAndVerify(flag_value='1', |
| 192 | expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE, |
| 193 | other_flag='--gtest_break_on_failure') |
| 194 | |
| 195 | def testWithFilterFlags(self): |
| 196 | """Tests that --gtest_list_tests takes into account the |
| 197 | --gtest_filter flag.""" |
| 198 | |
| 199 | self.RunAndVerify(flag_value='1', |
| 200 | expected_output_re=EXPECTED_OUTPUT_FILTER_FOO_RE, |
| 201 | other_flag='--gtest_filter=Foo*') |
| 202 | |
| 203 | |
| 204 | if __name__ == '__main__': |
| 205 | gtest_test_utils.Main() |