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 29 """Unit test for text_style.py.""" 30 31 import unittest 32 33 import text as text_style 34 from text import TextChecker 35 36 class TextStyleTestCase(unittest.TestCase): 37 """TestCase for text_style.py""" 38 39 def assertNoError(self, lines): 40 """Asserts that the specified lines has no errors.""" 41 self.had_error = False 42 43 def error_for_test(line_number, category, confidence, message): 44 """Records if an error occurs.""" 45 self.had_error = True 46 47 text_style.process_file_data('', lines, error_for_test) 48 self.assertFalse(self.had_error, '%s should not have any errors.' % lines) 49 50 def assertError(self, lines, expected_line_number): 51 """Asserts that the specified lines has an error.""" 52 self.had_error = False 53 54 def error_for_test(line_number, category, confidence, message): 55 """Checks if the expected error occurs.""" 56 self.assertEqual(expected_line_number, line_number) 57 self.assertEqual('whitespace/tab', category) 58 self.had_error = True 59 60 text_style.process_file_data('', lines, error_for_test) 61 self.assertTrue(self.had_error, '%s should have an error [whitespace/tab].' % lines) 62 63 64 def test_no_error(self): 65 """Tests for no error cases.""" 66 self.assertNoError(['']) 67 self.assertNoError(['abc def', 'ggg']) 68 69 70 def test_error(self): 71 """Tests for error cases.""" 72 self.assertError(['2009-12-16\tKent Tamura\t<tkent (at] chromium.org>'], 1) 73 self.assertError(['2009-12-16 Kent Tamura <tkent (at] chromium.org>', 74 '', 75 '\tReviewed by NOBODY.'], 3) 76 77 78 class TextCheckerTest(unittest.TestCase): 79 80 """Tests TextChecker class.""" 81 82 def mock_handle_style_error(self): 83 pass 84 85 def test_init(self): 86 """Test __init__ constructor.""" 87 checker = TextChecker("foo.txt", self.mock_handle_style_error) 88 self.assertEqual(checker.file_path, "foo.txt") 89 self.assertEqual(checker.handle_style_error, self.mock_handle_style_error) 90