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
34A user can ask Google Test to list all tests by specifying the
35--gtest_list_tests flag.  This script tests such functionality
36by invoking gtest_list_tests_unittest_ (a program written with
37Google Test) the command line flags.
38"""
39
40__author__ = 'phanna@google.com (Patrick Hanna)'
41
42import gtest_test_utils
43
44
45# Constants.
46
47# The command line flag for enabling/disabling listing all tests.
48LIST_TESTS_FLAG = 'gtest_list_tests'
49
50# Path to the gtest_list_tests_unittest_ program.
51EXE_PATH = gtest_test_utils.GetTestExecutablePath('gtest_list_tests_unittest_')
52
53# The expected output when running gtest_list_tests_unittest_ with
54# --gtest_list_tests
55EXPECTED_OUTPUT_NO_FILTER = """FooDeathTest.
56  Test1
57Foo.
58  Bar1
59  Bar2
60  DISABLED_Bar3
61Abc.
62  Xyz
63  Def
64FooBar.
65  Baz
66FooTest.
67  Test1
68  DISABLED_Test2
69  Test3
70"""
71
72# The expected output when running gtest_list_tests_unittest_ with
73# --gtest_list_tests and --gtest_filter=Foo*.
74EXPECTED_OUTPUT_FILTER_FOO = """FooDeathTest.
75  Test1
76Foo.
77  Bar1
78  Bar2
79  DISABLED_Bar3
80FooBar.
81  Baz
82FooTest.
83  Test1
84  DISABLED_Test2
85  Test3
86"""
87
88# Utilities.
89
90
91def Run(args):
92  """Runs gtest_list_tests_unittest_ and returns the list of tests printed."""
93
94  return gtest_test_utils.Subprocess([EXE_PATH] + args,
95                                     capture_stderr=False).output
96
97
98# The unit test.
99
100class GTestListTestsUnitTest(gtest_test_utils.TestCase):
101  """Tests using the --gtest_list_tests flag to list all tests."""
102
103  def RunAndVerify(self, flag_value, expected_output, other_flag):
104    """Runs gtest_list_tests_unittest_ and verifies that it prints
105    the correct tests.
106
107    Args:
108      flag_value:       value of the --gtest_list_tests flag;
109                        None if the flag should not be present.
110
111      expected_output:  the expected output after running command;
112
113      other_flag:       a different flag to be passed to command
114                        along with gtest_list_tests;
115                        None if the flag should not be present.
116    """
117
118    if flag_value is None:
119      flag = ''
120      flag_expression = 'not set'
121    elif flag_value == '0':
122      flag = '--%s=0' % LIST_TESTS_FLAG
123      flag_expression = '0'
124    else:
125      flag = '--%s' % LIST_TESTS_FLAG
126      flag_expression = '1'
127
128    args = [flag]
129
130    if other_flag is not None:
131      args += [other_flag]
132
133    output = Run(args)
134
135    msg = ('when %s is %s, the output of "%s" is "%s".' %
136           (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output))
137
138    if expected_output is not None:
139      self.assert_(output == expected_output, msg)
140    else:
141      self.assert_(output != EXPECTED_OUTPUT_NO_FILTER, msg)
142
143  def testDefaultBehavior(self):
144    """Tests the behavior of the default mode."""
145
146    self.RunAndVerify(flag_value=None,
147                      expected_output=None,
148                      other_flag=None)
149
150  def testFlag(self):
151    """Tests using the --gtest_list_tests flag."""
152
153    self.RunAndVerify(flag_value='0',
154                      expected_output=None,
155                      other_flag=None)
156    self.RunAndVerify(flag_value='1',
157                      expected_output=EXPECTED_OUTPUT_NO_FILTER,
158                      other_flag=None)
159
160  def testOverrideNonFilterFlags(self):
161    """Tests that --gtest_list_tests overrides the non-filter flags."""
162
163    self.RunAndVerify(flag_value='1',
164                      expected_output=EXPECTED_OUTPUT_NO_FILTER,
165                      other_flag='--gtest_break_on_failure')
166
167  def testWithFilterFlags(self):
168    """Tests that --gtest_list_tests takes into account the
169    --gtest_filter flag."""
170
171    self.RunAndVerify(flag_value='1',
172                      expected_output=EXPECTED_OUTPUT_FILTER_FOO,
173                      other_flag='--gtest_filter=Foo*')
174
175
176if __name__ == '__main__':
177  gtest_test_utils.Main()
178