##// END OF EJS Templates
Make test robust against differences in line endings...
Thomas Kluyver -
Show More
@@ -1,39 +1,39 b''
1 import io
1 import io
2 import os.path
2 import os.path
3 import nose.tools as nt
3 import nose.tools as nt
4
4
5 from IPython.utils import openpy
5 from IPython.utils import openpy
6
6
7 mydir = os.path.dirname(__file__)
7 mydir = os.path.dirname(__file__)
8 nonascii_path = os.path.join(mydir, '../../core/tests/nonascii.py')
8 nonascii_path = os.path.join(mydir, '../../core/tests/nonascii.py')
9
9
10 def test_detect_encoding():
10 def test_detect_encoding():
11 f = open(nonascii_path, 'rb')
11 f = open(nonascii_path, 'rb')
12 enc, lines = openpy.detect_encoding(f.readline)
12 enc, lines = openpy.detect_encoding(f.readline)
13 nt.assert_equal(enc, 'iso-8859-5')
13 nt.assert_equal(enc, 'iso-8859-5')
14
14
15 def test_read_file():
15 def test_read_file():
16 read_specified_enc = io.open(nonascii_path, encoding='iso-8859-5').read()
16 read_specified_enc = io.open(nonascii_path, encoding='iso-8859-5').read()
17 read_detected_enc = openpy.read_py_file(nonascii_path, skip_encoding_cookie=False)
17 read_detected_enc = openpy.read_py_file(nonascii_path, skip_encoding_cookie=False)
18 nt.assert_equal(read_detected_enc, read_specified_enc)
18 nt.assert_equal(read_detected_enc, read_specified_enc)
19 assert u'coding: iso-8859-5' in read_detected_enc
19 assert u'coding: iso-8859-5' in read_detected_enc
20
20
21 read_strip_enc_cookie = openpy.read_py_file(nonascii_path, skip_encoding_cookie=True)
21 read_strip_enc_cookie = openpy.read_py_file(nonascii_path, skip_encoding_cookie=True)
22 assert u'coding: iso-8859-5' not in read_strip_enc_cookie
22 assert u'coding: iso-8859-5' not in read_strip_enc_cookie
23
23
24 def test_source_to_unicode():
24 def test_source_to_unicode():
25 with io.open(nonascii_path, 'rb') as f:
25 with io.open(nonascii_path, 'rb') as f:
26 source_bytes = f.read()
26 source_bytes = f.read()
27 nt.assert_equal(openpy.source_to_unicode(source_bytes, skip_encoding_cookie=False),
27 nt.assert_equal(openpy.source_to_unicode(source_bytes, skip_encoding_cookie=False).splitlines(),
28 source_bytes.decode('iso-8859-5'))
28 source_bytes.decode('iso-8859-5').splitlines())
29
29
30 source_no_cookie = openpy.source_to_unicode(source_bytes, skip_encoding_cookie=True)
30 source_no_cookie = openpy.source_to_unicode(source_bytes, skip_encoding_cookie=True)
31 nt.assert_not_in(u'coding: iso-8859-5', source_no_cookie)
31 nt.assert_not_in(u'coding: iso-8859-5', source_no_cookie)
32
32
33 def test_list_readline():
33 def test_list_readline():
34 l = ['a', 'b']
34 l = ['a', 'b']
35 readline = openpy._list_readline(l)
35 readline = openpy._list_readline(l)
36 nt.assert_equal(readline(), 'a')
36 nt.assert_equal(readline(), 'a')
37 nt.assert_equal(readline(), 'b')
37 nt.assert_equal(readline(), 'b')
38 with nt.assert_raises(StopIteration):
38 with nt.assert_raises(StopIteration):
39 readline() No newline at end of file
39 readline()
General Comments 0
You need to be logged in to leave comments. Login now