| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 '''Unit test that checks some of util functions. | |
| 7 ''' | |
| 8 | |
| 9 import os | |
| 10 import sys | |
| 11 if __name__ == '__main__': | |
| 12 sys.path.append(os.path.join(os.path.dirname(__file__), '..')) | |
| 13 | |
| 14 import unittest | |
| 15 | |
| 16 from grit import util | |
| 17 | |
| 18 | |
| 19 class UtilUnittest(unittest.TestCase): | |
| 20 ''' Tests functions from util | |
| 21 ''' | |
| 22 | |
| 23 def testNewClassInstance(self): | |
| 24 # Test short class name with no fully qualified package name | |
| 25 # Should fail, it is not supported by the function now (as documented) | |
| 26 cls = util.NewClassInstance('grit.util.TestClassToLoad', | |
| 27 TestBaseClassToLoad) | |
| 28 self.failUnless(cls == None) | |
| 29 | |
| 30 # Test non existent class name | |
| 31 cls = util.NewClassInstance('grit.util_unittest.NotExistingClass', | |
| 32 TestBaseClassToLoad) | |
| 33 self.failUnless(cls == None) | |
| 34 | |
| 35 # Test valid class name and valid base class | |
| 36 cls = util.NewClassInstance('grit.util_unittest.TestClassToLoad', | |
| 37 TestBaseClassToLoad) | |
| 38 self.failUnless(isinstance(cls, TestBaseClassToLoad)) | |
| 39 | |
| 40 # Test valid class name with wrong hierarchy | |
| 41 cls = util.NewClassInstance('grit.util_unittest.TestClassNoBase', | |
| 42 TestBaseClassToLoad) | |
| 43 self.failUnless(cls == None) | |
| 44 | |
| 45 def testCanonicalLanguage(self): | |
| 46 self.failUnless(util.CanonicalLanguage('en') == 'en') | |
| 47 self.failUnless(util.CanonicalLanguage('pt_br') == 'pt-BR') | |
| 48 self.failUnless(util.CanonicalLanguage('pt-br') == 'pt-BR') | |
| 49 self.failUnless(util.CanonicalLanguage('pt-BR') == 'pt-BR') | |
| 50 self.failUnless(util.CanonicalLanguage('pt/br') == 'pt-BR') | |
| 51 self.failUnless(util.CanonicalLanguage('pt/BR') == 'pt-BR') | |
| 52 self.failUnless(util.CanonicalLanguage('no_no_bokmal') == 'no-NO-BOKMAL') | |
| 53 | |
| 54 def testUnescapeHtml(self): | |
| 55 self.failUnless(util.UnescapeHtml('ϲ') == unichr(1010)) | |
| 56 self.failUnless(util.UnescapeHtml('ꯍ') == unichr(43981)) | |
| 57 | |
| 58 def testRelativePath(self): | |
| 59 """ Verify that MakeRelativePath works in some tricky cases.""" | |
| 60 | |
| 61 def TestRelativePathCombinations(base_path, other_path, expected_result): | |
| 62 """ Verify that the relative path function works for | |
| 63 the given paths regardless of whether or not they end with | |
| 64 a trailing slash.""" | |
| 65 for path1 in [base_path, base_path + os.path.sep]: | |
| 66 for path2 in [other_path, other_path + os.path.sep]: | |
| 67 result = util.MakeRelativePath(path1, path2) | |
| 68 self.failUnless(result == expected_result) | |
| 69 | |
| 70 # set-up variables | |
| 71 root_dir = 'c:%sa' % os.path.sep | |
| 72 result1 = '..%sabc' % os.path.sep | |
| 73 path1 = root_dir + 'bc' | |
| 74 result2 = 'bc' | |
| 75 path2 = '%s%s%s' % (root_dir, os.path.sep, result2) | |
| 76 # run the tests | |
| 77 TestRelativePathCombinations(root_dir, path1, result1) | |
| 78 TestRelativePathCombinations(root_dir, path2, result2) | |
| 79 | |
| 80 def testReadFile(self): | |
| 81 def Test(data, encoding, expected_result): | |
| 82 with open('testfile', 'wb') as f: | |
| 83 f.write(data) | |
| 84 if util.ReadFile('testfile', encoding) != expected_result: | |
| 85 print (util.ReadFile('testfile', encoding), expected_result) | |
| 86 self.failUnless(util.ReadFile('testfile', encoding) == expected_result) | |
| 87 | |
| 88 test_std_newline = '\xEF\xBB\xBFabc\ndef' # EF BB BF is UTF-8 BOM | |
| 89 newlines = ['\n', '\r\n', '\r'] | |
| 90 | |
| 91 with util.TempDir({}) as tmp_dir: | |
| 92 with tmp_dir.AsCurrentDir(): | |
| 93 for newline in newlines: | |
| 94 test = test_std_newline.replace('\n', newline) | |
| 95 Test(test, util.BINARY, test) | |
| 96 # RAW_TEXT uses universal newline mode | |
| 97 Test(test, util.RAW_TEXT, test_std_newline) | |
| 98 # utf-8 doesn't strip BOM | |
| 99 Test(test, 'utf-8', test_std_newline.decode('utf-8')) | |
| 100 # utf-8-sig strips BOM | |
| 101 Test(test, 'utf-8-sig', test_std_newline.decode('utf-8')[1:]) | |
| 102 # test another encoding | |
| 103 Test(test, 'cp1252', test_std_newline.decode('cp1252')) | |
| 104 self.assertRaises(UnicodeDecodeError, Test, '\x80', 'utf-8', None) | |
| 105 | |
| 106 | |
| 107 class TestBaseClassToLoad(object): | |
| 108 pass | |
| 109 | |
| 110 class TestClassToLoad(TestBaseClassToLoad): | |
| 111 pass | |
| 112 | |
| 113 class TestClassNoBase(object): | |
| 114 pass | |
| 115 | |
| 116 | |
| 117 if __name__ == '__main__': | |
| 118 unittest.main() | |
| 119 | |
| OLD | NEW |