xref: /freebsd-src/contrib/googletest/googletest/test/googletest-list-tests-unittest.py (revision 28f6c2f292806bf31230a959bc4b19d7081669a7)
1b89a7cc2SEnji Cooper#!/usr/bin/env python
2b89a7cc2SEnji Cooper#
3b89a7cc2SEnji Cooper# Copyright 2006, Google Inc.
4b89a7cc2SEnji Cooper# All rights reserved.
5b89a7cc2SEnji Cooper#
6b89a7cc2SEnji Cooper# Redistribution and use in source and binary forms, with or without
7b89a7cc2SEnji Cooper# modification, are permitted provided that the following conditions are
8b89a7cc2SEnji Cooper# met:
9b89a7cc2SEnji Cooper#
10b89a7cc2SEnji Cooper#     * Redistributions of source code must retain the above copyright
11b89a7cc2SEnji Cooper# notice, this list of conditions and the following disclaimer.
12b89a7cc2SEnji Cooper#     * Redistributions in binary form must reproduce the above
13b89a7cc2SEnji Cooper# copyright notice, this list of conditions and the following disclaimer
14b89a7cc2SEnji Cooper# in the documentation and/or other materials provided with the
15b89a7cc2SEnji Cooper# distribution.
16b89a7cc2SEnji Cooper#     * Neither the name of Google Inc. nor the names of its
17b89a7cc2SEnji Cooper# contributors may be used to endorse or promote products derived from
18b89a7cc2SEnji Cooper# this software without specific prior written permission.
19b89a7cc2SEnji Cooper#
20b89a7cc2SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21b89a7cc2SEnji Cooper# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22b89a7cc2SEnji Cooper# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23b89a7cc2SEnji Cooper# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24b89a7cc2SEnji Cooper# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25b89a7cc2SEnji Cooper# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26b89a7cc2SEnji Cooper# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27b89a7cc2SEnji Cooper# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28b89a7cc2SEnji Cooper# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29b89a7cc2SEnji Cooper# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30b89a7cc2SEnji Cooper# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31b89a7cc2SEnji Cooper
32b89a7cc2SEnji Cooper"""Unit test for Google Test's --gtest_list_tests flag.
33b89a7cc2SEnji Cooper
34b89a7cc2SEnji CooperA user can ask Google Test to list all tests by specifying the
35b89a7cc2SEnji Cooper--gtest_list_tests flag.  This script tests such functionality
36b89a7cc2SEnji Cooperby invoking googletest-list-tests-unittest_ (a program written with
37b89a7cc2SEnji CooperGoogle Test) the command line flags.
38b89a7cc2SEnji Cooper"""
39b89a7cc2SEnji Cooper
40b89a7cc2SEnji Cooperimport re
41*28f6c2f2SEnji Cooperfrom googletest.test import gtest_test_utils
42b89a7cc2SEnji Cooper
43b89a7cc2SEnji Cooper# Constants.
44b89a7cc2SEnji Cooper
45b89a7cc2SEnji Cooper# The command line flag for enabling/disabling listing all tests.
46b89a7cc2SEnji CooperLIST_TESTS_FLAG = 'gtest_list_tests'
47b89a7cc2SEnji Cooper
48b89a7cc2SEnji Cooper# Path to the googletest-list-tests-unittest_ program.
49*28f6c2f2SEnji CooperEXE_PATH = gtest_test_utils.GetTestExecutablePath(
50*28f6c2f2SEnji Cooper    'googletest-list-tests-unittest_'
51*28f6c2f2SEnji Cooper)
52b89a7cc2SEnji Cooper
53b89a7cc2SEnji Cooper# The expected output when running googletest-list-tests-unittest_ with
54b89a7cc2SEnji Cooper# --gtest_list_tests
55*28f6c2f2SEnji CooperEXPECTED_OUTPUT_NO_FILTER_RE = re.compile(
56*28f6c2f2SEnji Cooper    r"""FooDeathTest\.
57b89a7cc2SEnji Cooper  Test1
58b89a7cc2SEnji CooperFoo\.
59b89a7cc2SEnji Cooper  Bar1
60b89a7cc2SEnji Cooper  Bar2
61b89a7cc2SEnji Cooper  DISABLED_Bar3
62b89a7cc2SEnji CooperAbc\.
63b89a7cc2SEnji Cooper  Xyz
64b89a7cc2SEnji Cooper  Def
65b89a7cc2SEnji CooperFooBar\.
66b89a7cc2SEnji Cooper  Baz
67b89a7cc2SEnji CooperFooTest\.
68b89a7cc2SEnji Cooper  Test1
69b89a7cc2SEnji Cooper  DISABLED_Test2
70b89a7cc2SEnji Cooper  Test3
71b89a7cc2SEnji CooperTypedTest/0\.  # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\.
72b89a7cc2SEnji Cooper  TestA
73b89a7cc2SEnji Cooper  TestB
74b89a7cc2SEnji CooperTypedTest/1\.  # TypeParam = int\s*\*( __ptr64)?
75b89a7cc2SEnji Cooper  TestA
76b89a7cc2SEnji Cooper  TestB
77b89a7cc2SEnji CooperTypedTest/2\.  # TypeParam = .*MyArray<bool,\s*42>
78b89a7cc2SEnji Cooper  TestA
79b89a7cc2SEnji Cooper  TestB
80b89a7cc2SEnji CooperMy/TypeParamTest/0\.  # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\.
81b89a7cc2SEnji Cooper  TestA
82b89a7cc2SEnji Cooper  TestB
83b89a7cc2SEnji CooperMy/TypeParamTest/1\.  # TypeParam = int\s*\*( __ptr64)?
84b89a7cc2SEnji Cooper  TestA
85b89a7cc2SEnji Cooper  TestB
86b89a7cc2SEnji CooperMy/TypeParamTest/2\.  # TypeParam = .*MyArray<bool,\s*42>
87b89a7cc2SEnji Cooper  TestA
88b89a7cc2SEnji Cooper  TestB
89b89a7cc2SEnji CooperMyInstantiation/ValueParamTest\.
90b89a7cc2SEnji Cooper  TestA/0  # GetParam\(\) = one line
91b89a7cc2SEnji Cooper  TestA/1  # GetParam\(\) = two\\nlines
92b89a7cc2SEnji Cooper  TestA/2  # GetParam\(\) = a very\\nlo{241}\.\.\.
93b89a7cc2SEnji Cooper  TestB/0  # GetParam\(\) = one line
94b89a7cc2SEnji Cooper  TestB/1  # GetParam\(\) = two\\nlines
95b89a7cc2SEnji Cooper  TestB/2  # GetParam\(\) = a very\\nlo{241}\.\.\.
96*28f6c2f2SEnji Cooper"""
97*28f6c2f2SEnji Cooper)
98b89a7cc2SEnji Cooper
99b89a7cc2SEnji Cooper# The expected output when running googletest-list-tests-unittest_ with
100b89a7cc2SEnji Cooper# --gtest_list_tests and --gtest_filter=Foo*.
101*28f6c2f2SEnji CooperEXPECTED_OUTPUT_FILTER_FOO_RE = re.compile(
102*28f6c2f2SEnji Cooper    r"""FooDeathTest\.
103b89a7cc2SEnji Cooper  Test1
104b89a7cc2SEnji CooperFoo\.
105b89a7cc2SEnji Cooper  Bar1
106b89a7cc2SEnji Cooper  Bar2
107b89a7cc2SEnji Cooper  DISABLED_Bar3
108b89a7cc2SEnji CooperFooBar\.
109b89a7cc2SEnji Cooper  Baz
110b89a7cc2SEnji CooperFooTest\.
111b89a7cc2SEnji Cooper  Test1
112b89a7cc2SEnji Cooper  DISABLED_Test2
113b89a7cc2SEnji Cooper  Test3
114*28f6c2f2SEnji Cooper"""
115*28f6c2f2SEnji Cooper)
116b89a7cc2SEnji Cooper
117b89a7cc2SEnji Cooper# Utilities.
118b89a7cc2SEnji Cooper
119b89a7cc2SEnji Cooper
120b89a7cc2SEnji Cooperdef Run(args):
121b89a7cc2SEnji Cooper  """Runs googletest-list-tests-unittest_ and returns the list of tests printed."""
122b89a7cc2SEnji Cooper
123*28f6c2f2SEnji Cooper  return gtest_test_utils.Subprocess(
124*28f6c2f2SEnji Cooper      [EXE_PATH] + args, capture_stderr=False
125*28f6c2f2SEnji Cooper  ).output
126b89a7cc2SEnji Cooper
127b89a7cc2SEnji Cooper
128b89a7cc2SEnji Cooper# The unit test.
129b89a7cc2SEnji Cooper
130b89a7cc2SEnji Cooper
131b89a7cc2SEnji Cooperclass GTestListTestsUnitTest(gtest_test_utils.TestCase):
132b89a7cc2SEnji Cooper  """Tests using the --gtest_list_tests flag to list all tests."""
133b89a7cc2SEnji Cooper
134b89a7cc2SEnji Cooper  def RunAndVerify(self, flag_value, expected_output_re, other_flag):
135*28f6c2f2SEnji Cooper    """Run googletest-list-tests-unittest_ and verify the output.
136*28f6c2f2SEnji Cooper
137*28f6c2f2SEnji Cooper    Runs googletest-list-tests-unittest_ and verifies that it prints
138b89a7cc2SEnji Cooper    the correct tests.
139b89a7cc2SEnji Cooper
140b89a7cc2SEnji Cooper    Args:
141*28f6c2f2SEnji Cooper      flag_value:         value of the --gtest_list_tests flag; None if the flag
142*28f6c2f2SEnji Cooper        should not be present.
143*28f6c2f2SEnji Cooper      expected_output_re: regular expression that matches the expected output
144*28f6c2f2SEnji Cooper        after running command;
145*28f6c2f2SEnji Cooper      other_flag:         a different flag to be passed to command along with
146*28f6c2f2SEnji Cooper        gtest_list_tests; None if the flag should not be present.
147b89a7cc2SEnji Cooper    """
148b89a7cc2SEnji Cooper
149b89a7cc2SEnji Cooper    if flag_value is None:
150b89a7cc2SEnji Cooper      flag = ''
151b89a7cc2SEnji Cooper      flag_expression = 'not set'
152b89a7cc2SEnji Cooper    elif flag_value == '0':
153b89a7cc2SEnji Cooper      flag = '--%s=0' % LIST_TESTS_FLAG
154b89a7cc2SEnji Cooper      flag_expression = '0'
155b89a7cc2SEnji Cooper    else:
156b89a7cc2SEnji Cooper      flag = '--%s' % LIST_TESTS_FLAG
157b89a7cc2SEnji Cooper      flag_expression = '1'
158b89a7cc2SEnji Cooper
159b89a7cc2SEnji Cooper    args = [flag]
160b89a7cc2SEnji Cooper
161b89a7cc2SEnji Cooper    if other_flag is not None:
162b89a7cc2SEnji Cooper      args += [other_flag]
163b89a7cc2SEnji Cooper
164b89a7cc2SEnji Cooper    output = Run(args)
165b89a7cc2SEnji Cooper
166b89a7cc2SEnji Cooper    if expected_output_re:
167*28f6c2f2SEnji Cooper      self.assertTrue(
168b89a7cc2SEnji Cooper          expected_output_re.match(output),
169*28f6c2f2SEnji Cooper          'when %s is %s, the output of "%s" is "%s",\n'
170*28f6c2f2SEnji Cooper          'which does not match regex "%s"'
171*28f6c2f2SEnji Cooper          % (
172*28f6c2f2SEnji Cooper              LIST_TESTS_FLAG,
173*28f6c2f2SEnji Cooper              flag_expression,
174*28f6c2f2SEnji Cooper              ' '.join(args),
175*28f6c2f2SEnji Cooper              output,
176*28f6c2f2SEnji Cooper              expected_output_re.pattern,
177*28f6c2f2SEnji Cooper          ),
178*28f6c2f2SEnji Cooper      )
179b89a7cc2SEnji Cooper    else:
180*28f6c2f2SEnji Cooper      self.assertTrue(
181b89a7cc2SEnji Cooper          not EXPECTED_OUTPUT_NO_FILTER_RE.match(output),
182*28f6c2f2SEnji Cooper          'when %s is %s, the output of "%s" is "%s"'
183*28f6c2f2SEnji Cooper          % (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output),
184*28f6c2f2SEnji Cooper      )
185b89a7cc2SEnji Cooper
186b89a7cc2SEnji Cooper  def testDefaultBehavior(self):
187b89a7cc2SEnji Cooper    """Tests the behavior of the default mode."""
188b89a7cc2SEnji Cooper
189*28f6c2f2SEnji Cooper    self.RunAndVerify(flag_value=None, expected_output_re=None, other_flag=None)
190b89a7cc2SEnji Cooper
191b89a7cc2SEnji Cooper  def testFlag(self):
192b89a7cc2SEnji Cooper    """Tests using the --gtest_list_tests flag."""
193b89a7cc2SEnji Cooper
194*28f6c2f2SEnji Cooper    self.RunAndVerify(flag_value='0', expected_output_re=None, other_flag=None)
195*28f6c2f2SEnji Cooper    self.RunAndVerify(
196*28f6c2f2SEnji Cooper        flag_value='1',
197b89a7cc2SEnji Cooper        expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE,
198*28f6c2f2SEnji Cooper        other_flag=None,
199*28f6c2f2SEnji Cooper    )
200b89a7cc2SEnji Cooper
201b89a7cc2SEnji Cooper  def testOverrideNonFilterFlags(self):
202b89a7cc2SEnji Cooper    """Tests that --gtest_list_tests overrides the non-filter flags."""
203b89a7cc2SEnji Cooper
204*28f6c2f2SEnji Cooper    self.RunAndVerify(
205*28f6c2f2SEnji Cooper        flag_value='1',
206b89a7cc2SEnji Cooper        expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE,
207*28f6c2f2SEnji Cooper        other_flag='--gtest_break_on_failure',
208*28f6c2f2SEnji Cooper    )
209b89a7cc2SEnji Cooper
210b89a7cc2SEnji Cooper  def testWithFilterFlags(self):
211*28f6c2f2SEnji Cooper    """Tests that --gtest_list_tests takes into account the filter flags.
212b89a7cc2SEnji Cooper
213*28f6c2f2SEnji Cooper    Tests that --gtest_list_tests takes into account the
214*28f6c2f2SEnji Cooper    --gtest_filter flag.
215*28f6c2f2SEnji Cooper    """
216*28f6c2f2SEnji Cooper
217*28f6c2f2SEnji Cooper    self.RunAndVerify(
218*28f6c2f2SEnji Cooper        flag_value='1',
219b89a7cc2SEnji Cooper        expected_output_re=EXPECTED_OUTPUT_FILTER_FOO_RE,
220*28f6c2f2SEnji Cooper        other_flag='--gtest_filter=Foo*',
221*28f6c2f2SEnji Cooper    )
222b89a7cc2SEnji Cooper
223b89a7cc2SEnji Cooper
224b89a7cc2SEnji Cooperif __name__ == '__main__':
225b89a7cc2SEnji Cooper  gtest_test_utils.Main()
226