Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / tools / telemetry / third_party / modulegraph / modulegraph_tests / test_util.py
blobeafe43e9af0cf8e9814fed7e7bdee8e42f1ca6af
1 import unittest
2 import encodings
3 import encodings.aliases
4 from modulegraph import util
5 import sys
7 try:
8 from io import BytesIO
9 except ImportError:
10 from cStringIO import StringIO as BytesIO
12 class TestUtil (unittest.TestCase):
13 def test_imp_find_module(self):
14 fn = util.imp_find_module('encodings.aliases')[1]
15 self.assertTrue(encodings.aliases.__file__.startswith(fn))
17 def test_imp_walk(self):
18 imps = list(util.imp_walk('encodings.aliases'))
19 self.assertEqual(len(imps), 2)
21 self.assertEqual(imps[0][0], 'encodings')
22 self.assertTrue(encodings.__file__.startswith(imps[0][1][1]))
24 self.assertEqual(imps[1][0], 'aliases')
25 self.assertTrue(encodings.aliases.__file__.startswith(imps[1][1][1]))
27 # Close all files, avoid warning by unittest
28 for i in imps:
29 if i[1][0] is not None:
30 i[1][0].close()
33 def test_guess_encoding(self):
34 fp = BytesIO(b"# coding: utf-8")
35 self.assertEqual(util.guess_encoding(fp), "utf-8")
37 fp = BytesIO(b"\n# coding: utf-8")
38 self.assertEqual(util.guess_encoding(fp), "utf-8")
40 fp = BytesIO(b"# coding: latin-1")
41 self.assertEqual(util.guess_encoding(fp), "latin-1")
43 fp = BytesIO(b"\n# coding: latin-1")
44 self.assertEqual(util.guess_encoding(fp), "latin-1")
46 fp = BytesIO(b"#!/usr/bin/env/python\n# vim: set fileencoding=latin-1 :")
47 self.assertEqual(util.guess_encoding(fp), "latin-1")
49 fp = BytesIO(b"\n\n\n# coding: latin-1")
50 if sys.version_info[0] == 2:
51 self.assertEqual(util.guess_encoding(fp), "ascii")
52 else:
53 self.assertEqual(util.guess_encoding(fp), "utf-8")
55 del fp
58 if __name__ == "__main__":
59 unittest.main()