3 import encodings
.aliases
4 from modulegraph
import util
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
29 if i
[1][0] is not None:
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")
53 self
.assertEqual(util
.guess_encoding(fp
), "utf-8")
58 if __name__
== "__main__":