Blame googlemock/test/gmock_output_test.py

Packit bd1cd8
#!/usr/bin/env python
Packit bd1cd8
#
Packit bd1cd8
# Copyright 2008, Google Inc.
Packit bd1cd8
# All rights reserved.
Packit bd1cd8
#
Packit bd1cd8
# Redistribution and use in source and binary forms, with or without
Packit bd1cd8
# modification, are permitted provided that the following conditions are
Packit bd1cd8
# met:
Packit bd1cd8
#
Packit bd1cd8
#     * Redistributions of source code must retain the above copyright
Packit bd1cd8
# notice, this list of conditions and the following disclaimer.
Packit bd1cd8
#     * Redistributions in binary form must reproduce the above
Packit bd1cd8
# copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
# in the documentation and/or other materials provided with the
Packit bd1cd8
# distribution.
Packit bd1cd8
#     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
# contributors may be used to endorse or promote products derived from
Packit bd1cd8
# this software without specific prior written permission.
Packit bd1cd8
#
Packit bd1cd8
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
Packit bd1cd8
"""Tests the text output of Google C++ Mocking Framework.
Packit bd1cd8
Packit bd1cd8
SYNOPSIS
Packit bd1cd8
       gmock_output_test.py --build_dir=BUILD/DIR --gengolden
Packit bd1cd8
         # where BUILD/DIR contains the built gmock_output_test_ file.
Packit bd1cd8
       gmock_output_test.py --gengolden
Packit bd1cd8
       gmock_output_test.py
Packit bd1cd8
"""
Packit bd1cd8
Packit bd1cd8
__author__ = 'wan@google.com (Zhanyong Wan)'
Packit bd1cd8
Packit bd1cd8
import os
Packit bd1cd8
import re
Packit bd1cd8
import sys
Packit bd1cd8
Packit bd1cd8
import gmock_test_utils
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
# The flag for generating the golden file
Packit bd1cd8
GENGOLDEN_FLAG = '--gengolden'
Packit bd1cd8
Packit bd1cd8
PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_output_test_')
Packit bd1cd8
COMMAND = [PROGRAM_PATH, '--gtest_stack_trace_depth=0', '--gtest_print_time=0']
Packit bd1cd8
GOLDEN_NAME = 'gmock_output_test_golden.txt'
Packit bd1cd8
GOLDEN_PATH = os.path.join(gmock_test_utils.GetSourceDir(), GOLDEN_NAME)
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def ToUnixLineEnding(s):
Packit bd1cd8
  """Changes all Windows/Mac line endings in s to UNIX line endings."""
Packit bd1cd8
Packit bd1cd8
  return s.replace('\r\n', '\n').replace('\r', '\n')
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def RemoveReportHeaderAndFooter(output):
Packit bd1cd8
  """Removes Google Test result report's header and footer from the output."""
Packit bd1cd8
Packit bd1cd8
  output = re.sub(r'.*gtest_main.*\n', '', output)
Packit bd1cd8
  output = re.sub(r'\[.*\d+ tests.*\n', '', output)
Packit bd1cd8
  output = re.sub(r'\[.* test environment .*\n', '', output)
Packit bd1cd8
  output = re.sub(r'\[=+\] \d+ tests .* ran.*', '', output)
Packit bd1cd8
  output = re.sub(r'.* FAILED TESTS\n', '', output)
Packit bd1cd8
  return output
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def RemoveLocations(output):
Packit bd1cd8
  """Removes all file location info from a Google Test program's output.
Packit bd1cd8
Packit bd1cd8
  Args:
Packit bd1cd8
       output:  the output of a Google Test program.
Packit bd1cd8
Packit bd1cd8
  Returns:
Packit bd1cd8
       output with all file location info (in the form of
Packit bd1cd8
       'DIRECTORY/FILE_NAME:LINE_NUMBER: 'or
Packit bd1cd8
       'DIRECTORY\\FILE_NAME(LINE_NUMBER): ') replaced by
Packit bd1cd8
       'FILE:#: '.
Packit bd1cd8
  """
Packit bd1cd8
Packit bd1cd8
  return re.sub(r'.*[/\\](.+)(\:\d+|\(\d+\))\:', 'FILE:#:', output)
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def NormalizeErrorMarker(output):
Packit bd1cd8
  """Normalizes the error marker, which is different on Windows vs on Linux."""
Packit bd1cd8
Packit bd1cd8
  return re.sub(r' error: ', ' Failure\n', output)
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def RemoveMemoryAddresses(output):
Packit bd1cd8
  """Removes memory addresses from the test output."""
Packit bd1cd8
Packit bd1cd8
  return re.sub(r'@\w+', '@0x#', output)
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def RemoveTestNamesOfLeakedMocks(output):
Packit bd1cd8
  """Removes the test names of leaked mock objects from the test output."""
Packit bd1cd8
Packit bd1cd8
  return re.sub(r'\(used in test .+\) ', '', output)
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def GetLeakyTests(output):
Packit bd1cd8
  """Returns a list of test names that leak mock objects."""
Packit bd1cd8
Packit bd1cd8
  # findall() returns a list of all matches of the regex in output.
Packit bd1cd8
  # For example, if '(used in test FooTest.Bar)' is in output, the
Packit bd1cd8
  # list will contain 'FooTest.Bar'.
Packit bd1cd8
  return re.findall(r'\(used in test (.+)\)', output)
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def GetNormalizedOutputAndLeakyTests(output):
Packit bd1cd8
  """Normalizes the output of gmock_output_test_.
Packit bd1cd8
Packit bd1cd8
  Args:
Packit bd1cd8
    output: The test output.
Packit bd1cd8
Packit bd1cd8
  Returns:
Packit bd1cd8
    A tuple (the normalized test output, the list of test names that have
Packit bd1cd8
    leaked mocks).
Packit bd1cd8
  """
Packit bd1cd8
Packit bd1cd8
  output = ToUnixLineEnding(output)
Packit bd1cd8
  output = RemoveReportHeaderAndFooter(output)
Packit bd1cd8
  output = NormalizeErrorMarker(output)
Packit bd1cd8
  output = RemoveLocations(output)
Packit bd1cd8
  output = RemoveMemoryAddresses(output)
Packit bd1cd8
  return (RemoveTestNamesOfLeakedMocks(output), GetLeakyTests(output))
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def GetShellCommandOutput(cmd):
Packit bd1cd8
  """Runs a command in a sub-process, and returns its STDOUT in a string."""
Packit bd1cd8
Packit bd1cd8
  return gmock_test_utils.Subprocess(cmd, capture_stderr=False).output
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def GetNormalizedCommandOutputAndLeakyTests(cmd):
Packit bd1cd8
  """Runs a command and returns its normalized output and a list of leaky tests.
Packit bd1cd8
Packit bd1cd8
  Args:
Packit bd1cd8
    cmd:  the shell command.
Packit bd1cd8
  """
Packit bd1cd8
Packit bd1cd8
  # Disables exception pop-ups on Windows.
Packit bd1cd8
  os.environ['GTEST_CATCH_EXCEPTIONS'] = '1'
Packit bd1cd8
  return GetNormalizedOutputAndLeakyTests(GetShellCommandOutput(cmd))
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
class GMockOutputTest(gmock_test_utils.TestCase):
Packit bd1cd8
  def testOutput(self):
Packit bd1cd8
    (output, leaky_tests) = GetNormalizedCommandOutputAndLeakyTests(COMMAND)
Packit bd1cd8
    golden_file = open(GOLDEN_PATH, 'rb')
Packit bd1cd8
    golden = golden_file.read()
Packit bd1cd8
    golden_file.close()
Packit bd1cd8
Packit bd1cd8
    # The normalized output should match the golden file.
Packit bd1cd8
    self.assertEquals(golden, output)
Packit bd1cd8
Packit bd1cd8
    # The raw output should contain 2 leaked mock object errors for
Packit bd1cd8
    # test GMockOutputTest.CatchesLeakedMocks.
Packit bd1cd8
    self.assertEquals(['GMockOutputTest.CatchesLeakedMocks',
Packit bd1cd8
                       'GMockOutputTest.CatchesLeakedMocks'],
Packit bd1cd8
                      leaky_tests)
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
if __name__ == '__main__':
Packit bd1cd8
  if sys.argv[1:] == [GENGOLDEN_FLAG]:
Packit bd1cd8
    (output, _) = GetNormalizedCommandOutputAndLeakyTests(COMMAND)
Packit bd1cd8
    golden_file = open(GOLDEN_PATH, 'wb')
Packit bd1cd8
    golden_file.write(output)
Packit bd1cd8
    golden_file.close()
Packit bd1cd8
  else:
Packit bd1cd8
    gmock_test_utils.Main()