1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org)
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
6 # 1. Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer.
8 # 2. Redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution.
12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
13 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
16 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 """Unit tests for common.py."""
27 from common import CarriageReturnChecker
28 from common import TabChecker
30 # FIXME: The unit tests for the cpp, text, and common checkers should
31 # share supporting test code. This can include, for example, the
32 # mock style error handling code and the code to check that all
33 # of a checker's categories are covered by the unit tests.
34 # Such shared code can be located in a shared test file, perhaps
36 class CarriageReturnCheckerTest(unittest.TestCase):
38 """Tests check_no_carriage_return()."""
40 _category = "whitespace/carriage_return"
42 _expected_message = ("One or more unexpected \\r (^M) found; "
43 "better to use only a \\n")
46 self._style_errors = [] # The list of accumulated style errors.
48 def _mock_style_error_handler(self, line_number, category, confidence,
50 """Append the error information to the list of style errors."""
51 error = (line_number, category, confidence, message)
52 self._style_errors.append(error)
54 def assert_carriage_return(self, input_lines, expected_lines, error_lines):
55 """Process the given line and assert that the result is correct."""
56 handle_style_error = self._mock_style_error_handler
58 checker = CarriageReturnChecker(handle_style_error)
59 output_lines = checker.check(input_lines)
61 # Check both the return value and error messages.
62 self.assertEqual(output_lines, expected_lines)
64 expected_errors = [(line_number, self._category, self._confidence,
65 self._expected_message)
66 for line_number in error_lines]
67 self.assertEqual(self._style_errors, expected_errors)
69 def test_ends_with_carriage(self):
70 self.assert_carriage_return(["carriage return\r"],
74 def test_ends_with_nothing(self):
75 self.assert_carriage_return(["no carriage return"],
76 ["no carriage return"],
79 def test_ends_with_newline(self):
80 self.assert_carriage_return(["no carriage return\n"],
81 ["no carriage return\n"],
84 def test_carriage_in_middle(self):
85 # The CarriageReturnChecker checks only the final character
87 self.assert_carriage_return(["carriage\r in a string"],
88 ["carriage\r in a string"],
91 def test_multiple_errors(self):
92 self.assert_carriage_return(["line1", "line2\r", "line3\r"],
93 ["line1", "line2", "line3"],
97 class TabCheckerTest(unittest.TestCase):
99 """Tests for TabChecker."""
101 def assert_tab(self, input_lines, error_lines):
102 """Assert when the given lines contain tabs."""
103 self._error_lines = []
105 def style_error_handler(line_number, category, confidence, message):
106 self.assertEqual(category, 'whitespace/tab')
107 self.assertEqual(confidence, 5)
108 self.assertEqual(message, 'Line contains tab character.')
109 self._error_lines.append(line_number)
111 checker = TabChecker('', style_error_handler)
112 checker.check(input_lines)
113 self.assertEqual(self._error_lines, error_lines)
115 def test_notab(self):
116 self.assert_tab([''], [])
117 self.assert_tab(['foo', 'bar'], [])
120 self.assert_tab(['\tfoo'], [1])
121 self.assert_tab(['line1', '\tline2', 'line3\t'], [2, 3])