/firmware/tests/gtest-1.4.0/test/gtest_help_test.py

http://github.com/makerbot/G3Firmware · Python · 126 lines · 54 code · 22 blank · 50 comment · 5 complexity · adf373c6fad2e5e9ef41ade636b4d77d MD5 · raw file

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