1#!/usr/bin/env python
2#
3# Copyright 2009, 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"""Tests the --help flag of Google C++ Testing Framework.
33
34SYNOPSIS
35       gtest_help_test.py --gtest_build_dir=BUILD/DIR
36         # where BUILD/DIR contains the built gtest_help_test_ file.
37       gtest_help_test.py
38"""
39
40__author__ = 'wan@google.com (Zhanyong Wan)'
41
42import gtest_test_utils
43import os
44import re
45import unittest
46
47
48IS_WINDOWS = os.name == 'nt'
49
50if IS_WINDOWS:
51  PROGRAM = 'gtest_help_test_.exe'
52else:
53  PROGRAM = 'gtest_help_test_'
54
55PROGRAM_PATH = os.path.join(gtest_test_utils.GetBuildDir(), PROGRAM)
56FLAG_PREFIX = '--gtest_'
57CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions'
58
59# The help message must match this regex.
60HELP_REGEX = re.compile(
61    FLAG_PREFIX + r'list_tests.*' +
62    FLAG_PREFIX + r'filter=.*' +
63    FLAG_PREFIX + r'also_run_disabled_tests.*' +
64    FLAG_PREFIX + r'repeat=.*' +
65    FLAG_PREFIX + r'color=.*' +
66    FLAG_PREFIX + r'print_time.*' +
67    FLAG_PREFIX + r'output=.*' +
68    FLAG_PREFIX + r'break_on_failure.*' +
69    FLAG_PREFIX + r'throw_on_failure.*',
70    re.DOTALL)
71
72
73def RunWithFlag(flag):
74  """Runs gtest_help_test_ with the given flag.
75
76  Returns:
77    the exit code and the text output as a tuple.
78  Args:
79    flag: the command-line flag to pass to gtest_help_test_, or None.
80  """
81
82  if flag is None:
83    command = [PROGRAM_PATH]
84  else:
85    command = [PROGRAM_PATH, flag]
86  child = gtest_test_utils.Subprocess(command)
87  return child.exit_code, child.output
88
89
90class GTestHelpTest(unittest.TestCase):
91  """Tests the --help flag and its equivalent forms."""
92
93  def TestHelpFlag(self, flag):
94    """Verifies that the right message is printed and the tests are
95    skipped when the given flag is specified."""
96
97    exit_code, output = RunWithFlag(flag)
98    self.assertEquals(0, exit_code)
99    self.assert_(HELP_REGEX.search(output), output)
100    if IS_WINDOWS:
101      self.assert_(CATCH_EXCEPTIONS_FLAG in output, output)
102    else:
103      self.assert_(CATCH_EXCEPTIONS_FLAG not in output, output)
104
105  def testPrintsHelpWithFullFlag(self):
106    self.TestHelpFlag('--help')
107
108  def testPrintsHelpWithShortFlag(self):
109    self.TestHelpFlag('-h')
110
111  def testPrintsHelpWithQuestionFlag(self):
112    self.TestHelpFlag('-?')
113
114  def testPrintsHelpWithWindowsStyleQuestionFlag(self):
115    self.TestHelpFlag('/?')
116
117  def testRunsTestsWithoutHelpFlag(self):
118    """Verifies that when no help flag is specified, the tests are run
119    and the help message is not printed."""
120
121    exit_code, output = RunWithFlag(None)
122    self.assert_(exit_code != 0)
123    self.assert_(not HELP_REGEX.search(output), output)
124
125
126if __name__ == '__main__':
127  gtest_test_utils.Main()
128