##// END OF EJS Templates
Add file encoding declarations to two tests.
Thomas Kluyver -
Show More
@@ -1,70 +1,71 b''
1 # coding: utf-8
1 """Tests for the compilerop module.
2 """Tests for the compilerop module.
2 """
3 """
3 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
4 # Copyright (C) 2010 The IPython Development Team.
5 # Copyright (C) 2010 The IPython Development Team.
5 #
6 #
6 # Distributed under the terms of the BSD License.
7 # Distributed under the terms of the BSD License.
7 #
8 #
8 # The full license is in the file COPYING.txt, distributed with this software.
9 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
10
11
11 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
12 # Imports
13 # Imports
13 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
14 from __future__ import print_function
15 from __future__ import print_function
15
16
16 # Stdlib imports
17 # Stdlib imports
17 import linecache
18 import linecache
18
19
19 # Third-party imports
20 # Third-party imports
20 import nose.tools as nt
21 import nose.tools as nt
21
22
22 # Our own imports
23 # Our own imports
23 from IPython.core import compilerop
24 from IPython.core import compilerop
24
25
25 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
26 # Test functions
27 # Test functions
27 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
28
29
29 def test_code_name():
30 def test_code_name():
30 code = 'x=1'
31 code = 'x=1'
31 name = compilerop.code_name(code)
32 name = compilerop.code_name(code)
32 nt.assert_true(name.startswith('<ipython-input-0'))
33 nt.assert_true(name.startswith('<ipython-input-0'))
33
34
34
35
35 def test_code_name2():
36 def test_code_name2():
36 code = 'x=1'
37 code = 'x=1'
37 name = compilerop.code_name(code, 9)
38 name = compilerop.code_name(code, 9)
38 nt.assert_true(name.startswith('<ipython-input-9'))
39 nt.assert_true(name.startswith('<ipython-input-9'))
39
40
40
41
41 def test_compiler():
42 def test_compiler():
42 """Test the compiler correctly compiles and caches inputs
43 """Test the compiler correctly compiles and caches inputs
43 """
44 """
44 cp = compilerop.CachingCompiler()
45 cp = compilerop.CachingCompiler()
45 ncache = len(linecache.cache)
46 ncache = len(linecache.cache)
46 cp('x=1', 'single')
47 cp('x=1', 'single')
47 nt.assert_true(len(linecache.cache) > ncache)
48 nt.assert_true(len(linecache.cache) > ncache)
48
49
49 def test_compiler_unicode():
50 def test_compiler_unicode():
50 import sys
51 import sys
51 nt.assert_equal(sys.getdefaultencoding(), "ascii")
52 nt.assert_equal(sys.getdefaultencoding(), "ascii")
52
53
53 cp = compilerop.CachingCompiler()
54 cp = compilerop.CachingCompiler()
54 ncache = len(linecache.cache)
55 ncache = len(linecache.cache)
55 cp(u"t = 'žćčőđ'", "single")
56 cp(u"t = 'žćčőđ'", "single")
56 nt.assert_true(len(linecache.cache) > ncache)
57 nt.assert_true(len(linecache.cache) > ncache)
57
58
58 def test_compiler_check_cache():
59 def test_compiler_check_cache():
59 """Test the compiler properly manages the cache.
60 """Test the compiler properly manages the cache.
60 """
61 """
61 # Rather simple-minded tests that just exercise the API
62 # Rather simple-minded tests that just exercise the API
62 cp = compilerop.CachingCompiler()
63 cp = compilerop.CachingCompiler()
63 cp('x=1', 'single', 99)
64 cp('x=1', 'single', 99)
64 # Ensure now that after clearing the cache, our entries survive
65 # Ensure now that after clearing the cache, our entries survive
65 cp.check_cache()
66 cp.check_cache()
66 for k in linecache.cache:
67 for k in linecache.cache:
67 if k.startswith('<ipython-input-99'):
68 if k.startswith('<ipython-input-99'):
68 break
69 break
69 else:
70 else:
70 raise AssertionError('Entry for input-99 missing from linecache')
71 raise AssertionError('Entry for input-99 missing from linecache')
@@ -1,115 +1,116 b''
1 # coding: utf-8
1 """Tests for the IPython tab-completion machinery.
2 """Tests for the IPython tab-completion machinery.
2 """
3 """
3 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
4 # Module imports
5 # Module imports
5 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
6
7
7 # stdlib
8 # stdlib
8 import os
9 import os
9 import sys
10 import sys
10 import unittest
11 import unittest
11
12
12 # third party
13 # third party
13 import nose.tools as nt
14 import nose.tools as nt
14
15
15 # our own packages
16 # our own packages
16 from IPython.utils.tempdir import TemporaryDirectory
17 from IPython.utils.tempdir import TemporaryDirectory
17 from IPython.core.history import HistoryManager, extract_hist_ranges
18 from IPython.core.history import HistoryManager, extract_hist_ranges
18
19
19 def test_history():
20 def test_history():
20 nt.assert_equal(sys.getdefaultencoding(), "ascii")
21 nt.assert_equal(sys.getdefaultencoding(), "ascii")
21 ip = get_ipython()
22 ip = get_ipython()
22 with TemporaryDirectory() as tmpdir:
23 with TemporaryDirectory() as tmpdir:
23 #tmpdir = '/software/temp'
24 #tmpdir = '/software/temp'
24 histfile = os.path.realpath(os.path.join(tmpdir, 'history.sqlite'))
25 histfile = os.path.realpath(os.path.join(tmpdir, 'history.sqlite'))
25 # Ensure that we restore the history management that we mess with in
26 # Ensure that we restore the history management that we mess with in
26 # this test doesn't affect the IPython instance used by the test suite
27 # this test doesn't affect the IPython instance used by the test suite
27 # beyond this test.
28 # beyond this test.
28 hist_manager_ori = ip.history_manager
29 hist_manager_ori = ip.history_manager
29 try:
30 try:
30 ip.history_manager = HistoryManager(shell=ip)
31 ip.history_manager = HistoryManager(shell=ip)
31 ip.history_manager.hist_file = histfile
32 ip.history_manager.hist_file = histfile
32 ip.history_manager.init_db() # Has to be called after changing file
33 ip.history_manager.init_db() # Has to be called after changing file
33 ip.history_manager.reset()
34 ip.history_manager.reset()
34 print 'test',histfile
35 print 'test',histfile
35 hist = ['a=1', 'def f():\n test = 1\n return test', u'b="žćčőđ"']
36 hist = ['a=1', 'def f():\n test = 1\n return test', u'b="žćčőđ"']
36 for i, h in enumerate(hist, start=1):
37 for i, h in enumerate(hist, start=1):
37 ip.history_manager.store_inputs(i, h)
38 ip.history_manager.store_inputs(i, h)
38
39
39 ip.history_manager.db_log_output = True
40 ip.history_manager.db_log_output = True
40 # Doesn't match the input, but we'll just check it's stored.
41 # Doesn't match the input, but we'll just check it's stored.
41 ip.history_manager.output_hist_reprs[3].append("spam")
42 ip.history_manager.output_hist_reprs[3].append("spam")
42 ip.history_manager.store_output(3)
43 ip.history_manager.store_output(3)
43
44
44 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
45 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
45
46
46 # Check lines were written to DB
47 # Check lines were written to DB
47 c = ip.history_manager.db.execute("SELECT source_raw FROM history")
48 c = ip.history_manager.db.execute("SELECT source_raw FROM history")
48 nt.assert_equal([x for x, in c], hist)
49 nt.assert_equal([x for x, in c], hist)
49
50
50 # New session
51 # New session
51 ip.history_manager.reset()
52 ip.history_manager.reset()
52 newcmds = ["z=5","class X(object):\n pass", "k='p'"]
53 newcmds = ["z=5","class X(object):\n pass", "k='p'"]
53 for i, cmd in enumerate(newcmds, start=1):
54 for i, cmd in enumerate(newcmds, start=1):
54 ip.history_manager.store_inputs(i, cmd)
55 ip.history_manager.store_inputs(i, cmd)
55 gothist = ip.history_manager.get_range(start=1, stop=4)
56 gothist = ip.history_manager.get_range(start=1, stop=4)
56 nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds))
57 nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds))
57 # Previous session:
58 # Previous session:
58 gothist = ip.history_manager.get_range(-1, 1, 4)
59 gothist = ip.history_manager.get_range(-1, 1, 4)
59 nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
60 nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
60
61
61 # Check get_hist_tail
62 # Check get_hist_tail
62 gothist = ip.history_manager.get_tail(4, output=True,
63 gothist = ip.history_manager.get_tail(4, output=True,
63 include_latest=True)
64 include_latest=True)
64 expected = [(1, 3, (hist[-1], ["spam"])),
65 expected = [(1, 3, (hist[-1], ["spam"])),
65 (2, 1, (newcmds[0], None)),
66 (2, 1, (newcmds[0], None)),
66 (2, 2, (newcmds[1], None)),
67 (2, 2, (newcmds[1], None)),
67 (2, 3, (newcmds[2], None)),]
68 (2, 3, (newcmds[2], None)),]
68 nt.assert_equal(list(gothist), expected)
69 nt.assert_equal(list(gothist), expected)
69
70
70 gothist = ip.history_manager.get_tail(2)
71 gothist = ip.history_manager.get_tail(2)
71 expected = [(2, 1, newcmds[0]),
72 expected = [(2, 1, newcmds[0]),
72 (2, 2, newcmds[1])]
73 (2, 2, newcmds[1])]
73 nt.assert_equal(list(gothist), expected)
74 nt.assert_equal(list(gothist), expected)
74
75
75 # Check get_hist_search
76 # Check get_hist_search
76 gothist = ip.history_manager.search("*test*")
77 gothist = ip.history_manager.search("*test*")
77 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
78 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
78 gothist = ip.history_manager.search("b*", output=True)
79 gothist = ip.history_manager.search("b*", output=True)
79 nt.assert_equal(list(gothist), [(1,3,(hist[2],["spam"]))] )
80 nt.assert_equal(list(gothist), [(1,3,(hist[2],["spam"]))] )
80
81
81 # Cross testing: check that magic %save can get previous session.
82 # Cross testing: check that magic %save can get previous session.
82 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
83 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
83 ip.magic_save(testfilename + " ~1/1-3")
84 ip.magic_save(testfilename + " ~1/1-3")
84 testfile = open(testfilename, "r")
85 testfile = open(testfilename, "r")
85 nt.assert_equal(testfile.read(), "\n".join(hist))
86 nt.assert_equal(testfile.read(), "\n".join(hist))
86
87
87 # Duplicate line numbers - check that it doesn't crash, and
88 # Duplicate line numbers - check that it doesn't crash, and
88 # gets a new session
89 # gets a new session
89 ip.history_manager.store_inputs(1, "rogue")
90 ip.history_manager.store_inputs(1, "rogue")
90 nt.assert_equal(ip.history_manager.session_number, 3)
91 nt.assert_equal(ip.history_manager.session_number, 3)
91 finally:
92 finally:
92 # Restore history manager
93 # Restore history manager
93 ip.history_manager = hist_manager_ori
94 ip.history_manager = hist_manager_ori
94
95
95
96
96 def test_extract_hist_ranges():
97 def test_extract_hist_ranges():
97 instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5"
98 instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5"
98 expected = [(0, 1, 2), # 0 == current session
99 expected = [(0, 1, 2), # 0 == current session
99 (2, 3, 4),
100 (2, 3, 4),
100 (-4, 5, 7),
101 (-4, 5, 7),
101 (-4, 7, 10),
102 (-4, 7, 10),
102 (-9, 2, None), # None == to end
103 (-9, 2, None), # None == to end
103 (-8, 1, None),
104 (-8, 1, None),
104 (-7, 1, 6)]
105 (-7, 1, 6)]
105 actual = list(extract_hist_ranges(instr))
106 actual = list(extract_hist_ranges(instr))
106 nt.assert_equal(actual, expected)
107 nt.assert_equal(actual, expected)
107
108
108 def test_magic_rerun():
109 def test_magic_rerun():
109 """Simple test for %rerun (no args -> rerun last line)"""
110 """Simple test for %rerun (no args -> rerun last line)"""
110 ip = get_ipython()
111 ip = get_ipython()
111 ip.run_cell("a = 10")
112 ip.run_cell("a = 10")
112 ip.run_cell("a += 1")
113 ip.run_cell("a += 1")
113 nt.assert_equal(ip.user_ns["a"], 11)
114 nt.assert_equal(ip.user_ns["a"], 11)
114 ip.run_cell("%rerun")
115 ip.run_cell("%rerun")
115 nt.assert_equal(ip.user_ns["a"], 12)
116 nt.assert_equal(ip.user_ns["a"], 12)
General Comments 0
You need to be logged in to leave comments. Login now