• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2010 Google Inc. All rights reserved.
2# Copyright (C) 2009 Torch Mobile Inc.
3# Copyright (C) 2009 Apple Inc. All rights reserved.
4# Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@gmail.com)
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
32import webkitpy.thirdparty.unittest2 as unittest
33
34from webkitpy.common.system.filesystem_mock import MockFileSystem
35from webkitpy.style.patchreader import PatchReader
36
37
38class PatchReaderTest(unittest.TestCase):
39
40    """Test the PatchReader class."""
41
42    class MockTextFileReader(object):
43
44        def __init__(self):
45            self.passed_to_process_file = []
46            """A list of (file_path, line_numbers) pairs."""
47            self.delete_only_file_count = 0
48            """A number of times count_delete_only_file() called"""
49
50        def process_file(self, file_path, line_numbers):
51            self.passed_to_process_file.append((file_path, line_numbers))
52
53        def count_delete_only_file(self):
54            self.delete_only_file_count += 1
55
56    def setUp(self):
57        file_reader = self.MockTextFileReader()
58        self._file_reader = file_reader
59        self._patch_checker = PatchReader(file_reader)
60
61    def _call_check_patch(self, patch_string):
62        self._patch_checker.check(patch_string)
63
64    def _assert_checked(self, passed_to_process_file, delete_only_file_count):
65        self.assertEqual(self._file_reader.passed_to_process_file,
66                          passed_to_process_file)
67        self.assertEqual(self._file_reader.delete_only_file_count,
68                          delete_only_file_count)
69
70    def test_check_patch(self):
71        # The modified line_numbers array for this patch is: [2].
72        self._call_check_patch("""diff --git a/__init__.py b/__init__.py
73index ef65bee..e3db70e 100644
74--- a/__init__.py
75+++ b/__init__.py
76@@ -1,1 +1,2 @@
77 # Required for Python to search this directory for module files
78+# New line
79""")
80        self._assert_checked([("__init__.py", [2])], 0)
81
82    def test_check_patch_with_deletion(self):
83        self._call_check_patch("""Index: __init__.py
84===================================================================
85--- __init__.py  (revision 3593)
86+++ __init__.py  (working copy)
87@@ -1 +0,0 @@
88-foobar
89""")
90        # _mock_check_file should not be called for the deletion patch.
91        self._assert_checked([], 1)
92
93    def test_check_patch_with_png_deletion(self):
94        fs = MockFileSystem()
95        diff_text = """Index: LayoutTests/platform/mac/foo-expected.png
96===================================================================
97Cannot display: file marked as a binary type.
98svn:mime-type = image/png
99"""
100        self._patch_checker.check(diff_text, fs)
101        self._assert_checked([], 1)
102