3 # Copyright (C) 2010 Google Inc. All rights reserved.
4 # Copyright (C) 2009 Torch Mobile Inc.
5 # Copyright (C) 2009 Apple Inc. All rights reserved.
6 # Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@gmail.com)
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following disclaimer
16 # in the documentation and/or other materials provided with the
18 # * Neither the name of Google Inc. nor the names of its
19 # contributors may be used to endorse or promote products derived from
20 # this software without specific prior written permission.
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 from webkitpy.style.patchreader import PatchReader
39 class PatchReaderTest(unittest.TestCase):
41 """Test the PatchReader class."""
43 class MockTextFileReader(object):
46 self.passed_to_process_file = []
47 """A list of (file_path, line_numbers) pairs."""
48 self.delete_only_file_count = 0
49 """A number of times count_delete_only_file() called"""
51 def process_file(self, file_path, line_numbers):
52 self.passed_to_process_file.append((file_path, line_numbers))
54 def count_delete_only_file(self):
55 self.delete_only_file_count += 1
58 file_reader = self.MockTextFileReader()
59 self._file_reader = file_reader
60 self._patch_checker = PatchReader(file_reader)
62 def _call_check_patch(self, patch_string):
63 self._patch_checker.check(patch_string)
65 def _assert_checked(self, passed_to_process_file, delete_only_file_count):
66 self.assertEquals(self._file_reader.passed_to_process_file,
67 passed_to_process_file)
68 self.assertEquals(self._file_reader.delete_only_file_count,
69 delete_only_file_count)
71 def test_check_patch(self):
72 # The modified line_numbers array for this patch is: [2].
73 self._call_check_patch("""diff --git a/__init__.py b/__init__.py
74 index ef65bee..e3db70e 100644
78 # Required for Python to search this directory for module files
81 self._assert_checked([("__init__.py", [2])], 0)
83 def test_check_patch_with_deletion(self):
84 self._call_check_patch("""Index: __init__.py
85 ===================================================================
86 --- __init__.py (revision 3593)
87 +++ __init__.py (working copy)
91 # _mock_check_file should not be called for the deletion patch.
92 self._assert_checked([], 1)