common_unittest.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. """Unit tests for common.py."""
  23. import unittest2 as unittest
  24. from common import CarriageReturnChecker
  25. from common import TabChecker
  26. # FIXME: The unit tests for the cpp, text, and common checkers should
  27. # share supporting test code. This can include, for example, the
  28. # mock style error handling code and the code to check that all
  29. # of a checker's categories are covered by the unit tests.
  30. # Such shared code can be located in a shared test file, perhaps
  31. # even this file.
  32. class CarriageReturnCheckerTest(unittest.TestCase):
  33. """Tests check_no_carriage_return()."""
  34. _category = "whitespace/carriage_return"
  35. _confidence = 1
  36. _expected_message = ("One or more unexpected \\r (^M) found; "
  37. "better to use only a \\n")
  38. def setUp(self):
  39. self._style_errors = [] # The list of accumulated style errors.
  40. def _mock_style_error_handler(self, line_number, category, confidence,
  41. message):
  42. """Append the error information to the list of style errors."""
  43. error = (line_number, category, confidence, message)
  44. self._style_errors.append(error)
  45. def assert_carriage_return(self, input_lines, expected_lines, error_lines):
  46. """Process the given line and assert that the result is correct."""
  47. handle_style_error = self._mock_style_error_handler
  48. checker = CarriageReturnChecker(handle_style_error)
  49. output_lines = checker.check(input_lines)
  50. # Check both the return value and error messages.
  51. self.assertEqual(output_lines, expected_lines)
  52. expected_errors = [(line_number, self._category, self._confidence,
  53. self._expected_message)
  54. for line_number in error_lines]
  55. self.assertEqual(self._style_errors, expected_errors)
  56. def test_ends_with_carriage(self):
  57. self.assert_carriage_return(["carriage return\r"],
  58. ["carriage return"],
  59. [1])
  60. def test_ends_with_nothing(self):
  61. self.assert_carriage_return(["no carriage return"],
  62. ["no carriage return"],
  63. [])
  64. def test_ends_with_newline(self):
  65. self.assert_carriage_return(["no carriage return\n"],
  66. ["no carriage return\n"],
  67. [])
  68. def test_carriage_in_middle(self):
  69. # The CarriageReturnChecker checks only the final character
  70. # of each line.
  71. self.assert_carriage_return(["carriage\r in a string"],
  72. ["carriage\r in a string"],
  73. [])
  74. def test_multiple_errors(self):
  75. self.assert_carriage_return(["line1", "line2\r", "line3\r"],
  76. ["line1", "line2", "line3"],
  77. [2, 3])
  78. class TabCheckerTest(unittest.TestCase):
  79. """Tests for TabChecker."""
  80. def assert_tab(self, input_lines, error_lines):
  81. """Assert when the given lines contain tabs."""
  82. self._error_lines = []
  83. def style_error_handler(line_number, category, confidence, message):
  84. self.assertEqual(category, 'whitespace/tab')
  85. self.assertEqual(confidence, 5)
  86. self.assertEqual(message, 'Line contains tab character.')
  87. self._error_lines.append(line_number)
  88. checker = TabChecker('', style_error_handler)
  89. checker.check(input_lines)
  90. self.assertEqual(self._error_lines, error_lines)
  91. def test_notab(self):
  92. self.assert_tab([''], [])
  93. self.assert_tab(['foo', 'bar'], [])
  94. def test_tab(self):
  95. self.assert_tab(['\tfoo'], [1])
  96. self.assert_tab(['line1', '\tline2', 'line3\t'], [2, 3])