11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#!/usr/bin/env python
21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Copyright 2006, Google Inc.
41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# All rights reserved.
51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Redistribution and use in source and binary forms, with or without
71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# modification, are permitted provided that the following conditions are
81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# met:
91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Redistributions of source code must retain the above copyright
111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# notice, this list of conditions and the following disclaimer.
121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Redistributions in binary form must reproduce the above
131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# copyright notice, this list of conditions and the following disclaimer
141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# in the documentation and/or other materials provided with the
151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# distribution.
161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Neither the name of Google Inc. nor the names of its
171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# contributors may be used to endorse or promote products derived from
181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# this software without specific prior written permission.
191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"""Unit test for Google Test's --gtest_list_tests flag.
331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
341be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaA user can ask Google Test to list all tests by specifying the
351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania--gtest_list_tests flag.  This script tests such functionality
361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaby invoking gtest_list_tests_unittest_ (a program written with
371be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGoogle Test) the command line flags.
381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"""
391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania__author__ = 'phanna@google.com (Patrick Hanna)'
411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport gtest_test_utils
43fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesimport re
441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Constants.
471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# The command line flag for enabling/disabling listing all tests.
491be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaLIST_TESTS_FLAG = 'gtest_list_tests'
501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Path to the gtest_list_tests_unittest_ program.
5241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotEXE_PATH = gtest_test_utils.GetTestExecutablePath('gtest_list_tests_unittest_')
531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# The expected output when running gtest_list_tests_unittest_ with
551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# --gtest_list_tests
56fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesEXPECTED_OUTPUT_NO_FILTER_RE = re.compile(r"""FooDeathTest\.
571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Test1
58fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesFoo\.
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Bar1
601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Bar2
6141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  DISABLED_Bar3
62fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesAbc\.
631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Xyz
641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Def
65fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesFooBar\.
661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Baz
67fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesFooTest\.
681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Test1
6941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  DISABLED_Test2
701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Test3
71fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesTypedTest/0\.  # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\.
72fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestA
73fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestB
74fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesTypedTest/1\.  # TypeParam = int\s*\*
75fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestA
76fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestB
77fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesTypedTest/2\.  # TypeParam = .*MyArray<bool,\s*42>
78fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestA
79fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestB
80fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesMy/TypeParamTest/0\.  # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\.
81fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestA
82fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestB
83fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesMy/TypeParamTest/1\.  # TypeParam = int\s*\*
84fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestA
85fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestB
86fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesMy/TypeParamTest/2\.  # TypeParam = .*MyArray<bool,\s*42>
87fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestA
88fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestB
89fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesMyInstantiation/ValueParamTest\.
90fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestA/0  # GetParam\(\) = one line
91fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestA/1  # GetParam\(\) = two\\nlines
92fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestA/2  # GetParam\(\) = a very\\nlo{241}\.\.\.
93fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestB/0  # GetParam\(\) = one line
94fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestB/1  # GetParam\(\) = two\\nlines
95fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestB/2  # GetParam\(\) = a very\\nlo{241}\.\.\.
96fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes""")
971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# The expected output when running gtest_list_tests_unittest_ with
9941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# --gtest_list_tests and --gtest_filter=Foo*.
100fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesEXPECTED_OUTPUT_FILTER_FOO_RE = re.compile(r"""FooDeathTest\.
10141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  Test1
102fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesFoo\.
10341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  Bar1
10441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  Bar2
10541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  DISABLED_Bar3
106fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesFooBar\.
10741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  Baz
108fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesFooTest\.
10941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  Test1
11041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  DISABLED_Test2
11141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  Test3
112fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes""")
1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# Utilities.
1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotdef Run(args):
11841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  """Runs gtest_list_tests_unittest_ and returns the list of tests printed."""
1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return gtest_test_utils.Subprocess([EXE_PATH] + args,
12141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                     capture_stderr=False).output
1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# The unit test.
1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTestListTestsUnitTest(gtest_test_utils.TestCase):
12741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  """Tests using the --gtest_list_tests flag to list all tests."""
1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
129fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  def RunAndVerify(self, flag_value, expected_output_re, other_flag):
1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """Runs gtest_list_tests_unittest_ and verifies that it prints
1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    the correct tests.
1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    Args:
134fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      flag_value:         value of the --gtest_list_tests flag;
135fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                          None if the flag should not be present.
136fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      expected_output_re: regular expression that matches the expected
137fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                          output after running command;
138fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      other_flag:         a different flag to be passed to command
139fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                          along with gtest_list_tests;
140fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                          None if the flag should not be present.
1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """
1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if flag_value is None:
1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      flag = ''
14541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      flag_expression = 'not set'
1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    elif flag_value == '0':
14741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      flag = '--%s=0' % LIST_TESTS_FLAG
14841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      flag_expression = '0'
1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    else:
15041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      flag = '--%s' % LIST_TESTS_FLAG
15141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      flag_expression = '1'
1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
15341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    args = [flag]
1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if other_flag is not None:
15641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      args += [other_flag]
1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
15841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    output = Run(args)
1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
160fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    if expected_output_re:
161fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      self.assert_(
162fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          expected_output_re.match(output),
163fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          ('when %s is %s, the output of "%s" is "%s",\n'
164fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes           'which does not match regex "%s"' %
165fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes           (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output,
166fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes            expected_output_re.pattern)))
1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    else:
168fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      self.assert_(
169fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          not EXPECTED_OUTPUT_NO_FILTER_RE.match(output),
170fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          ('when %s is %s, the output of "%s" is "%s"'%
171fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes           (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output)))
1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def testDefaultBehavior(self):
1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """Tests the behavior of the default mode."""
1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.RunAndVerify(flag_value=None,
177fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                      expected_output_re=None,
1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      other_flag=None)
1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def testFlag(self):
1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """Tests using the --gtest_list_tests flag."""
1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.RunAndVerify(flag_value='0',
184fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                      expected_output_re=None,
1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      other_flag=None)
1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.RunAndVerify(flag_value='1',
187fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                      expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE,
1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      other_flag=None)
1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
19041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  def testOverrideNonFilterFlags(self):
19141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    """Tests that --gtest_list_tests overrides the non-filter flags."""
19241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
19341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    self.RunAndVerify(flag_value='1',
194fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                      expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE,
19541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                      other_flag='--gtest_break_on_failure')
19641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
19741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  def testWithFilterFlags(self):
19841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    """Tests that --gtest_list_tests takes into account the
19941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    --gtest_filter flag."""
20041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
20141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    self.RunAndVerify(flag_value='1',
202fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                      expected_output_re=EXPECTED_OUTPUT_FILTER_FOO_RE,
20341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                      other_flag='--gtest_filter=Foo*')
2041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaif __name__ == '__main__':
2071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  gtest_test_utils.Main()
208