1# Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org)
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions
5# are met:
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.
11#
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.
22
23"""Unit tests for common.py."""
24
25import unittest
26
27from common import CarriageReturnChecker
28from common import TabChecker
29
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
35#        even this file.
36class CarriageReturnCheckerTest(unittest.TestCase):
37
38    """Tests check_no_carriage_return()."""
39
40    _category = "whitespace/carriage_return"
41    _confidence = 1
42    _expected_message = ("One or more unexpected \\r (^M) found; "
43                         "better to use only a \\n")
44
45    def setUp(self):
46        self._style_errors = [] # The list of accumulated style errors.
47
48    def _mock_style_error_handler(self, line_number, category, confidence,
49                                  message):
50        """Append the error information to the list of style errors."""
51        error = (line_number, category, confidence, message)
52        self._style_errors.append(error)
53
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
57
58        checker = CarriageReturnChecker(handle_style_error)
59        output_lines = checker.check(input_lines)
60
61        # Check both the return value and error messages.
62        self.assertEquals(output_lines, expected_lines)
63
64        expected_errors = [(line_number, self._category, self._confidence,
65                            self._expected_message)
66                           for line_number in error_lines]
67        self.assertEquals(self._style_errors, expected_errors)
68
69    def test_ends_with_carriage(self):
70        self.assert_carriage_return(["carriage return\r"],
71                                    ["carriage return"],
72                                    [1])
73
74    def test_ends_with_nothing(self):
75        self.assert_carriage_return(["no carriage return"],
76                                    ["no carriage return"],
77                                    [])
78
79    def test_ends_with_newline(self):
80        self.assert_carriage_return(["no carriage return\n"],
81                                    ["no carriage return\n"],
82                                    [])
83
84    def test_carriage_in_middle(self):
85        # The CarriageReturnChecker checks only the final character
86        # of each line.
87        self.assert_carriage_return(["carriage\r in a string"],
88                                    ["carriage\r in a string"],
89                                    [])
90
91    def test_multiple_errors(self):
92        self.assert_carriage_return(["line1", "line2\r", "line3\r"],
93                                    ["line1", "line2", "line3"],
94                                    [2, 3])
95
96
97class TabCheckerTest(unittest.TestCase):
98
99    """Tests for TabChecker."""
100
101    def assert_tab(self, input_lines, error_lines):
102        """Assert when the given lines contain tabs."""
103        self._error_lines = []
104
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)
110
111        checker = TabChecker('', style_error_handler)
112        checker.check(input_lines)
113        self.assertEquals(self._error_lines, error_lines)
114
115    def test_notab(self):
116        self.assert_tab([''], [])
117        self.assert_tab(['foo', 'bar'], [])
118
119    def test_tab(self):
120        self.assert_tab(['\tfoo'], [1])
121        self.assert_tab(['line1', '\tline2', 'line3\t'], [2, 3])
122
123if __name__ == '__main__':
124    unittest.main()
125