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