initial import
[vuplus_webkit] / Tools / Scripts / webkitpy / style / patchreader_unittest.py
1 #!/usr/bin/python
2 #
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)
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are
10 # met:
11 #
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
17 # distribution.
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.
21 #
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.
33
34 import unittest
35
36 from webkitpy.style.patchreader import PatchReader
37
38
39 class PatchReaderTest(unittest.TestCase):
40
41     """Test the PatchReader class."""
42
43     class MockTextFileReader(object):
44
45         def __init__(self):
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"""
50
51         def process_file(self, file_path, line_numbers):
52             self.passed_to_process_file.append((file_path, line_numbers))
53
54         def count_delete_only_file(self):
55             self.delete_only_file_count += 1
56
57     def setUp(self):
58         file_reader = self.MockTextFileReader()
59         self._file_reader = file_reader
60         self._patch_checker = PatchReader(file_reader)
61
62     def _call_check_patch(self, patch_string):
63         self._patch_checker.check(patch_string)
64
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)
70
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
75 --- a/__init__.py
76 +++ b/__init__.py
77 @@ -1,1 +1,2 @@
78  # Required for Python to search this directory for module files
79 +# New line
80 """)
81         self._assert_checked([("__init__.py", [2])], 0)
82
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)
88 @@ -1 +0,0 @@
89 -foobar
90 """)
91         # _mock_check_file should not be called for the deletion patch.
92         self._assert_checked([], 1)