text_unittest.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright (C) 2009 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. """Unit test for text_style.py."""
  29. import unittest2 as unittest
  30. import text as text_style
  31. from text import TextChecker
  32. class TextStyleTestCase(unittest.TestCase):
  33. """TestCase for text_style.py"""
  34. def assertNoError(self, lines):
  35. """Asserts that the specified lines has no errors."""
  36. self.had_error = False
  37. def error_for_test(line_number, category, confidence, message):
  38. """Records if an error occurs."""
  39. self.had_error = True
  40. text_style.process_file_data('', lines, error_for_test)
  41. self.assertFalse(self.had_error, '%s should not have any errors.' % lines)
  42. def assertError(self, lines, expected_line_number):
  43. """Asserts that the specified lines has an error."""
  44. self.had_error = False
  45. def error_for_test(line_number, category, confidence, message):
  46. """Checks if the expected error occurs."""
  47. self.assertEqual(expected_line_number, line_number)
  48. self.assertEqual('whitespace/tab', category)
  49. self.had_error = True
  50. text_style.process_file_data('', lines, error_for_test)
  51. self.assertTrue(self.had_error, '%s should have an error [whitespace/tab].' % lines)
  52. def test_no_error(self):
  53. """Tests for no error cases."""
  54. self.assertNoError([''])
  55. self.assertNoError(['abc def', 'ggg'])
  56. def test_error(self):
  57. """Tests for error cases."""
  58. self.assertError(['2009-12-16\tKent Tamura\t<tkent@chromium.org>'], 1)
  59. self.assertError(['2009-12-16 Kent Tamura <tkent@chromium.org>',
  60. '',
  61. '\tReviewed by NOBODY.'], 3)
  62. class TextCheckerTest(unittest.TestCase):
  63. """Tests TextChecker class."""
  64. def mock_handle_style_error(self):
  65. pass
  66. def test_init(self):
  67. """Test __init__ constructor."""
  68. checker = TextChecker("foo.txt", self.mock_handle_style_error)
  69. self.assertEqual(checker.file_path, "foo.txt")
  70. self.assertEqual(checker.handle_style_error, self.mock_handle_style_error)