gmock_leak_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 that leaked mock objects can be caught be Google Mock."""
  32. from googlemock.test import gmock_test_utils
  33. PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_leak_test_')
  34. TEST_WITH_EXPECT_CALL = [PROGRAM_PATH, '--gtest_filter=*ExpectCall*']
  35. TEST_WITH_ON_CALL = [PROGRAM_PATH, '--gtest_filter=*OnCall*']
  36. TEST_MULTIPLE_LEAKS = [PROGRAM_PATH, '--gtest_filter=*MultipleLeaked*']
  37. environ = gmock_test_utils.environ
  38. SetEnvVar = gmock_test_utils.SetEnvVar
  39. # Tests in this file run a Google-Test-based test program and expect it
  40. # to terminate prematurely. Therefore they are incompatible with
  41. # the premature-exit-file protocol by design. Unset the
  42. # premature-exit filepath to prevent Google Test from creating
  43. # the file.
  44. SetEnvVar(gmock_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
  45. class GMockLeakTest(gmock_test_utils.TestCase):
  46. def testCatchesLeakedMockByDefault(self):
  47. self.assertNotEqual(
  48. 0,
  49. gmock_test_utils.Subprocess(
  50. TEST_WITH_EXPECT_CALL, env=environ
  51. ).exit_code,
  52. )
  53. self.assertNotEqual(
  54. 0, gmock_test_utils.Subprocess(TEST_WITH_ON_CALL, env=environ).exit_code
  55. )
  56. def testDoesNotCatchLeakedMockWhenDisabled(self):
  57. self.assertEqual(
  58. 0,
  59. gmock_test_utils.Subprocess(
  60. TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks=0'],
  61. env=environ,
  62. ).exit_code,
  63. )
  64. self.assertEqual(
  65. 0,
  66. gmock_test_utils.Subprocess(
  67. TEST_WITH_ON_CALL + ['--gmock_catch_leaked_mocks=0'], env=environ
  68. ).exit_code,
  69. )
  70. def testCatchesLeakedMockWhenEnabled(self):
  71. self.assertNotEqual(
  72. 0,
  73. gmock_test_utils.Subprocess(
  74. TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks'], env=environ
  75. ).exit_code,
  76. )
  77. self.assertNotEqual(
  78. 0,
  79. gmock_test_utils.Subprocess(
  80. TEST_WITH_ON_CALL + ['--gmock_catch_leaked_mocks'], env=environ
  81. ).exit_code,
  82. )
  83. def testCatchesLeakedMockWhenEnabledWithExplictFlagValue(self):
  84. self.assertNotEqual(
  85. 0,
  86. gmock_test_utils.Subprocess(
  87. TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks=1'],
  88. env=environ,
  89. ).exit_code,
  90. )
  91. def testCatchesMultipleLeakedMocks(self):
  92. self.assertNotEqual(
  93. 0,
  94. gmock_test_utils.Subprocess(
  95. TEST_MULTIPLE_LEAKS + ['--gmock_catch_leaked_mocks'], env=environ
  96. ).exit_code,
  97. )
  98. if __name__ == '__main__':
  99. gmock_test_utils.Main()