Show More
@@ -1,65 +1,65 | |||||
1 | from IPython.utils.capture import capture_output |
|
1 | from IPython.utils.capture import capture_output | |
2 |
|
2 | |||
3 | import nose.tools as nt |
|
3 | import pytest | |
4 |
|
4 | |||
5 | def test_alias_lifecycle(): |
|
5 | def test_alias_lifecycle(): | |
6 | name = 'test_alias1' |
|
6 | name = 'test_alias1' | |
7 | cmd = 'echo "Hello"' |
|
7 | cmd = 'echo "Hello"' | |
8 | am = _ip.alias_manager |
|
8 | am = _ip.alias_manager | |
9 | am.clear_aliases() |
|
9 | am.clear_aliases() | |
10 | am.define_alias(name, cmd) |
|
10 | am.define_alias(name, cmd) | |
11 | assert am.is_alias(name) |
|
11 | assert am.is_alias(name) | |
12 |
|
|
12 | assert am.retrieve_alias(name) == cmd | |
13 |
|
|
13 | assert (name, cmd) in am.aliases | |
14 |
|
14 | |||
15 | # Test running the alias |
|
15 | # Test running the alias | |
16 | orig_system = _ip.system |
|
16 | orig_system = _ip.system | |
17 | result = [] |
|
17 | result = [] | |
18 | _ip.system = result.append |
|
18 | _ip.system = result.append | |
19 | try: |
|
19 | try: | |
20 | _ip.run_cell('%{}'.format(name)) |
|
20 | _ip.run_cell('%{}'.format(name)) | |
21 | result = [c.strip() for c in result] |
|
21 | result = [c.strip() for c in result] | |
22 |
|
|
22 | assert result == [cmd] | |
23 | finally: |
|
23 | finally: | |
24 | _ip.system = orig_system |
|
24 | _ip.system = orig_system | |
25 |
|
25 | |||
26 | # Test removing the alias |
|
26 | # Test removing the alias | |
27 | am.undefine_alias(name) |
|
27 | am.undefine_alias(name) | |
28 | assert not am.is_alias(name) |
|
28 | assert not am.is_alias(name) | |
29 |
with |
|
29 | with pytest.raises(ValueError): | |
30 | am.retrieve_alias(name) |
|
30 | am.retrieve_alias(name) | |
31 |
|
|
31 | assert (name, cmd) not in am.aliases | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | def test_alias_args_error(): |
|
34 | def test_alias_args_error(): | |
35 | """Error expanding with wrong number of arguments""" |
|
35 | """Error expanding with wrong number of arguments""" | |
36 | _ip.alias_manager.define_alias('parts', 'echo first %s second %s') |
|
36 | _ip.alias_manager.define_alias('parts', 'echo first %s second %s') | |
37 | # capture stderr: |
|
37 | # capture stderr: | |
38 | with capture_output() as cap: |
|
38 | with capture_output() as cap: | |
39 | _ip.run_cell('parts 1') |
|
39 | _ip.run_cell('parts 1') | |
40 |
|
40 | |||
41 |
|
|
41 | assert cap.stderr.split(':')[0] == 'UsageError' | |
42 |
|
42 | |||
43 | def test_alias_args_commented(): |
|
43 | def test_alias_args_commented(): | |
44 | """Check that alias correctly ignores 'commented out' args""" |
|
44 | """Check that alias correctly ignores 'commented out' args""" | |
45 | _ip.magic('alias commetarg echo this is %%s a commented out arg') |
|
45 | _ip.magic('alias commetarg echo this is %%s a commented out arg') | |
46 |
|
46 | |||
47 | with capture_output() as cap: |
|
47 | with capture_output() as cap: | |
48 | _ip.run_cell('commetarg') |
|
48 | _ip.run_cell('commetarg') | |
49 |
|
49 | |||
50 | # strip() is for pytest compat; testing via iptest patch IPython shell |
|
50 | # strip() is for pytest compat; testing via iptest patch IPython shell | |
51 | # in testin.globalipapp and replace the system call which messed up the |
|
51 | # in testin.globalipapp and replace the system call which messed up the | |
52 | # \r\n |
|
52 | # \r\n | |
53 | assert cap.stdout.strip() == 'this is %s a commented out arg' |
|
53 | assert cap.stdout.strip() == 'this is %s a commented out arg' | |
54 |
|
54 | |||
55 | def test_alias_args_commented_nargs(): |
|
55 | def test_alias_args_commented_nargs(): | |
56 | """Check that alias correctly counts args, excluding those commented out""" |
|
56 | """Check that alias correctly counts args, excluding those commented out""" | |
57 | am = _ip.alias_manager |
|
57 | am = _ip.alias_manager | |
58 | alias_name = 'comargcount' |
|
58 | alias_name = 'comargcount' | |
59 | cmd = 'echo this is %%s a commented out arg and this is not %s' |
|
59 | cmd = 'echo this is %%s a commented out arg and this is not %s' | |
60 |
|
60 | |||
61 | am.define_alias(alias_name, cmd) |
|
61 | am.define_alias(alias_name, cmd) | |
62 | assert am.is_alias(alias_name) |
|
62 | assert am.is_alias(alias_name) | |
63 |
|
63 | |||
64 | thealias = am.get_alias(alias_name) |
|
64 | thealias = am.get_alias(alias_name) | |
65 |
|
|
65 | assert thealias.nargs == 1 |
@@ -1,73 +1,73 | |||||
1 | # coding: utf-8 |
|
1 | # coding: utf-8 | |
2 | """Tests for the compilerop module. |
|
2 | """Tests for the compilerop module. | |
3 | """ |
|
3 | """ | |
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2010-2011 The IPython Development Team. |
|
5 | # Copyright (C) 2010-2011 The IPython Development Team. | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the BSD License. |
|
7 | # Distributed under the terms of the BSD License. | |
8 | # |
|
8 | # | |
9 | # 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. | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Imports |
|
13 | # Imports | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
16 | # Stdlib imports |
|
16 | # Stdlib imports | |
17 | import linecache |
|
17 | import linecache | |
18 | import sys |
|
18 | import sys | |
19 |
|
19 | |||
20 | # Third-party imports |
|
20 | # Third-party imports | |
21 | import nose.tools as nt |
|
21 | import pytest | |
22 |
|
22 | |||
23 | # Our own imports |
|
23 | # Our own imports | |
24 | from IPython.core import compilerop |
|
24 | from IPython.core import compilerop | |
25 |
|
25 | |||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | # Test functions |
|
27 | # Test functions | |
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 |
|
29 | |||
30 | def test_code_name(): |
|
30 | def test_code_name(): | |
31 | code = 'x=1' |
|
31 | code = 'x=1' | |
32 | name = compilerop.code_name(code) |
|
32 | name = compilerop.code_name(code) | |
33 |
|
|
33 | assert name.startswith('<ipython-input-0') | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | def test_code_name2(): |
|
36 | def test_code_name2(): | |
37 | code = 'x=1' |
|
37 | code = 'x=1' | |
38 | name = compilerop.code_name(code, 9) |
|
38 | name = compilerop.code_name(code, 9) | |
39 |
|
|
39 | assert name.startswith('<ipython-input-9') | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | def test_cache(): |
|
42 | def test_cache(): | |
43 | """Test the compiler correctly compiles and caches inputs |
|
43 | """Test the compiler correctly compiles and caches inputs | |
44 | """ |
|
44 | """ | |
45 | cp = compilerop.CachingCompiler() |
|
45 | cp = compilerop.CachingCompiler() | |
46 | ncache = len(linecache.cache) |
|
46 | ncache = len(linecache.cache) | |
47 | cp.cache('x=1') |
|
47 | cp.cache('x=1') | |
48 |
|
|
48 | assert len(linecache.cache) > ncache | |
49 |
|
49 | |||
50 | def test_proper_default_encoding(): |
|
50 | def test_proper_default_encoding(): | |
51 | # Check we're in a proper Python 2 environment (some imports, such |
|
51 | # Check we're in a proper Python 2 environment (some imports, such | |
52 | # as GTK, can change the default encoding, which can hide bugs.) |
|
52 | # as GTK, can change the default encoding, which can hide bugs.) | |
53 |
|
|
53 | assert sys.getdefaultencoding() == "utf-8" | |
54 |
|
54 | |||
55 | def test_cache_unicode(): |
|
55 | def test_cache_unicode(): | |
56 | cp = compilerop.CachingCompiler() |
|
56 | cp = compilerop.CachingCompiler() | |
57 | ncache = len(linecache.cache) |
|
57 | ncache = len(linecache.cache) | |
58 | cp.cache(u"t = 'žćčšđ'") |
|
58 | cp.cache(u"t = 'žćčšđ'") | |
59 |
|
|
59 | assert len(linecache.cache) > ncache | |
60 |
|
60 | |||
61 | def test_compiler_check_cache(): |
|
61 | def test_compiler_check_cache(): | |
62 | """Test the compiler properly manages the cache. |
|
62 | """Test the compiler properly manages the cache. | |
63 | """ |
|
63 | """ | |
64 | # Rather simple-minded tests that just exercise the API |
|
64 | # Rather simple-minded tests that just exercise the API | |
65 | cp = compilerop.CachingCompiler() |
|
65 | cp = compilerop.CachingCompiler() | |
66 | cp.cache('x=1', 99) |
|
66 | cp.cache('x=1', 99) | |
67 | # Ensure now that after clearing the cache, our entries survive |
|
67 | # Ensure now that after clearing the cache, our entries survive | |
68 | linecache.checkcache() |
|
68 | linecache.checkcache() | |
69 | for k in linecache.cache: |
|
69 | for k in linecache.cache: | |
70 | if k.startswith('<ipython-input-99'): |
|
70 | if k.startswith('<ipython-input-99'): | |
71 | break |
|
71 | break | |
72 | else: |
|
72 | else: | |
73 | raise AssertionError('Entry for input-99 missing from linecache') |
|
73 | raise AssertionError('Entry for input-99 missing from linecache') |
@@ -1,166 +1,165 | |||||
1 | """Tests for the line-based transformers in IPython.core.inputtransformer2 |
|
1 | """Tests for the line-based transformers in IPython.core.inputtransformer2 | |
2 |
|
2 | |||
3 | Line-based transformers are the simpler ones; token-based transformers are |
|
3 | Line-based transformers are the simpler ones; token-based transformers are | |
4 | more complex. See test_inputtransformer2 for tests for token-based transformers. |
|
4 | more complex. See test_inputtransformer2 for tests for token-based transformers. | |
5 | """ |
|
5 | """ | |
6 | import nose.tools as nt |
|
6 | import pytest | |
7 |
|
7 | |||
8 | from IPython.core import inputtransformer2 as ipt2 |
|
8 | from IPython.core import inputtransformer2 as ipt2 | |
9 |
|
9 | |||
10 | CELL_MAGIC = ("""\ |
|
10 | CELL_MAGIC = ("""\ | |
11 | %%foo arg |
|
11 | %%foo arg | |
12 | body 1 |
|
12 | body 1 | |
13 | body 2 |
|
13 | body 2 | |
14 | """, """\ |
|
14 | """, """\ | |
15 | get_ipython().run_cell_magic('foo', 'arg', 'body 1\\nbody 2\\n') |
|
15 | get_ipython().run_cell_magic('foo', 'arg', 'body 1\\nbody 2\\n') | |
16 | """) |
|
16 | """) | |
17 |
|
17 | |||
18 | def test_cell_magic(): |
|
18 | def test_cell_magic(): | |
19 | for sample, expected in [CELL_MAGIC]: |
|
19 | for sample, expected in [CELL_MAGIC]: | |
20 |
|
|
20 | assert (ipt2.cell_magic(sample.splitlines(keepends=True)) == | |
21 | expected.splitlines(keepends=True)) |
|
21 | expected.splitlines(keepends=True)) | |
22 |
|
22 | |||
23 | CLASSIC_PROMPT = ("""\ |
|
23 | CLASSIC_PROMPT = ("""\ | |
24 | >>> for a in range(5): |
|
24 | >>> for a in range(5): | |
25 | ... print(a) |
|
25 | ... print(a) | |
26 | """, """\ |
|
26 | """, """\ | |
27 | for a in range(5): |
|
27 | for a in range(5): | |
28 | print(a) |
|
28 | print(a) | |
29 | """) |
|
29 | """) | |
30 |
|
30 | |||
31 | CLASSIC_PROMPT_L2 = ("""\ |
|
31 | CLASSIC_PROMPT_L2 = ("""\ | |
32 | for a in range(5): |
|
32 | for a in range(5): | |
33 | ... print(a) |
|
33 | ... print(a) | |
34 | ... print(a ** 2) |
|
34 | ... print(a ** 2) | |
35 | """, """\ |
|
35 | """, """\ | |
36 | for a in range(5): |
|
36 | for a in range(5): | |
37 | print(a) |
|
37 | print(a) | |
38 | print(a ** 2) |
|
38 | print(a ** 2) | |
39 | """) |
|
39 | """) | |
40 |
|
40 | |||
41 | def test_classic_prompt(): |
|
41 | def test_classic_prompt(): | |
42 | for sample, expected in [CLASSIC_PROMPT, CLASSIC_PROMPT_L2]: |
|
42 | for sample, expected in [CLASSIC_PROMPT, CLASSIC_PROMPT_L2]: | |
43 |
|
|
43 | assert (ipt2.classic_prompt(sample.splitlines(keepends=True)) == | |
44 | expected.splitlines(keepends=True)) |
|
44 | expected.splitlines(keepends=True)) | |
45 |
|
45 | |||
46 | IPYTHON_PROMPT = ("""\ |
|
46 | IPYTHON_PROMPT = ("""\ | |
47 | In [1]: for a in range(5): |
|
47 | In [1]: for a in range(5): | |
48 | ...: print(a) |
|
48 | ...: print(a) | |
49 | """, """\ |
|
49 | """, """\ | |
50 | for a in range(5): |
|
50 | for a in range(5): | |
51 | print(a) |
|
51 | print(a) | |
52 | """) |
|
52 | """) | |
53 |
|
53 | |||
54 | IPYTHON_PROMPT_L2 = ("""\ |
|
54 | IPYTHON_PROMPT_L2 = ("""\ | |
55 | for a in range(5): |
|
55 | for a in range(5): | |
56 | ...: print(a) |
|
56 | ...: print(a) | |
57 | ...: print(a ** 2) |
|
57 | ...: print(a ** 2) | |
58 | """, """\ |
|
58 | """, """\ | |
59 | for a in range(5): |
|
59 | for a in range(5): | |
60 | print(a) |
|
60 | print(a) | |
61 | print(a ** 2) |
|
61 | print(a ** 2) | |
62 | """) |
|
62 | """) | |
63 |
|
63 | |||
64 |
|
64 | |||
65 | IPYTHON_PROMPT_VI_INS = ( |
|
65 | IPYTHON_PROMPT_VI_INS = ( | |
66 | """\ |
|
66 | """\ | |
67 | [ins] In [11]: def a(): |
|
67 | [ins] In [11]: def a(): | |
68 | ...: 123 |
|
68 | ...: 123 | |
69 | ...: |
|
69 | ...: | |
70 | ...: 123 |
|
70 | ...: 123 | |
71 | """, |
|
71 | """, | |
72 | """\ |
|
72 | """\ | |
73 | def a(): |
|
73 | def a(): | |
74 | 123 |
|
74 | 123 | |
75 |
|
75 | |||
76 | 123 |
|
76 | 123 | |
77 | """, |
|
77 | """, | |
78 | ) |
|
78 | ) | |
79 |
|
79 | |||
80 | IPYTHON_PROMPT_VI_NAV = ( |
|
80 | IPYTHON_PROMPT_VI_NAV = ( | |
81 | """\ |
|
81 | """\ | |
82 | [nav] In [11]: def a(): |
|
82 | [nav] In [11]: def a(): | |
83 | ...: 123 |
|
83 | ...: 123 | |
84 | ...: |
|
84 | ...: | |
85 | ...: 123 |
|
85 | ...: 123 | |
86 | """, |
|
86 | """, | |
87 | """\ |
|
87 | """\ | |
88 | def a(): |
|
88 | def a(): | |
89 | 123 |
|
89 | 123 | |
90 |
|
90 | |||
91 | 123 |
|
91 | 123 | |
92 | """, |
|
92 | """, | |
93 | ) |
|
93 | ) | |
94 |
|
94 | |||
95 |
|
95 | |||
96 | def test_ipython_prompt(): |
|
96 | def test_ipython_prompt(): | |
97 | for sample, expected in [ |
|
97 | for sample, expected in [ | |
98 | IPYTHON_PROMPT, |
|
98 | IPYTHON_PROMPT, | |
99 | IPYTHON_PROMPT_L2, |
|
99 | IPYTHON_PROMPT_L2, | |
100 | IPYTHON_PROMPT_VI_INS, |
|
100 | IPYTHON_PROMPT_VI_INS, | |
101 | IPYTHON_PROMPT_VI_NAV, |
|
101 | IPYTHON_PROMPT_VI_NAV, | |
102 | ]: |
|
102 | ]: | |
103 |
|
|
103 | assert ( | |
104 |
ipt2.ipython_prompt(sample.splitlines(keepends=True)) |
|
104 | ipt2.ipython_prompt(sample.splitlines(keepends=True)) == | |
105 |
expected.splitlines(keepends=True) |
|
105 | expected.splitlines(keepends=True)) | |
106 | ) |
|
|||
107 |
|
106 | |||
108 |
|
107 | |||
109 | INDENT_SPACES = ("""\ |
|
108 | INDENT_SPACES = ("""\ | |
110 | if True: |
|
109 | if True: | |
111 | a = 3 |
|
110 | a = 3 | |
112 | """, """\ |
|
111 | """, """\ | |
113 | if True: |
|
112 | if True: | |
114 | a = 3 |
|
113 | a = 3 | |
115 | """) |
|
114 | """) | |
116 |
|
115 | |||
117 | INDENT_TABS = ("""\ |
|
116 | INDENT_TABS = ("""\ | |
118 | \tif True: |
|
117 | \tif True: | |
119 | \t\tb = 4 |
|
118 | \t\tb = 4 | |
120 | """, """\ |
|
119 | """, """\ | |
121 | if True: |
|
120 | if True: | |
122 | \tb = 4 |
|
121 | \tb = 4 | |
123 | """) |
|
122 | """) | |
124 |
|
123 | |||
125 | def test_leading_indent(): |
|
124 | def test_leading_indent(): | |
126 | for sample, expected in [INDENT_SPACES, INDENT_TABS]: |
|
125 | for sample, expected in [INDENT_SPACES, INDENT_TABS]: | |
127 |
|
|
126 | assert (ipt2.leading_indent(sample.splitlines(keepends=True)) == | |
128 | expected.splitlines(keepends=True)) |
|
127 | expected.splitlines(keepends=True)) | |
129 |
|
128 | |||
130 | LEADING_EMPTY_LINES = ("""\ |
|
129 | LEADING_EMPTY_LINES = ("""\ | |
131 | \t |
|
130 | \t | |
132 |
|
131 | |||
133 | if True: |
|
132 | if True: | |
134 | a = 3 |
|
133 | a = 3 | |
135 |
|
134 | |||
136 | b = 4 |
|
135 | b = 4 | |
137 | """, """\ |
|
136 | """, """\ | |
138 | if True: |
|
137 | if True: | |
139 | a = 3 |
|
138 | a = 3 | |
140 |
|
139 | |||
141 | b = 4 |
|
140 | b = 4 | |
142 | """) |
|
141 | """) | |
143 |
|
142 | |||
144 | ONLY_EMPTY_LINES = ("""\ |
|
143 | ONLY_EMPTY_LINES = ("""\ | |
145 | \t |
|
144 | \t | |
146 |
|
145 | |||
147 | """, """\ |
|
146 | """, """\ | |
148 | \t |
|
147 | \t | |
149 |
|
148 | |||
150 | """) |
|
149 | """) | |
151 |
|
150 | |||
152 | def test_leading_empty_lines(): |
|
151 | def test_leading_empty_lines(): | |
153 | for sample, expected in [LEADING_EMPTY_LINES, ONLY_EMPTY_LINES]: |
|
152 | for sample, expected in [LEADING_EMPTY_LINES, ONLY_EMPTY_LINES]: | |
154 |
|
|
153 | assert ( | |
155 |
ipt2.leading_empty_lines(sample.splitlines(keepends=True)) |
|
154 | ipt2.leading_empty_lines(sample.splitlines(keepends=True)) == | |
156 | expected.splitlines(keepends=True)) |
|
155 | expected.splitlines(keepends=True)) | |
157 |
|
156 | |||
158 | CRLF_MAGIC = ([ |
|
157 | CRLF_MAGIC = ([ | |
159 | "%%ls\r\n" |
|
158 | "%%ls\r\n" | |
160 | ], [ |
|
159 | ], [ | |
161 | "get_ipython().run_cell_magic('ls', '', '')\n" |
|
160 | "get_ipython().run_cell_magic('ls', '', '')\n" | |
162 | ]) |
|
161 | ]) | |
163 |
|
162 | |||
164 | def test_crlf_magic(): |
|
163 | def test_crlf_magic(): | |
165 | for sample, expected in [CRLF_MAGIC]: |
|
164 | for sample, expected in [CRLF_MAGIC]: | |
166 |
|
|
165 | assert ipt2.cell_magic(sample) == expected |
@@ -1,238 +1,238 | |||||
1 | """Tests for the key interactiveshell module, where the main ipython class is defined. |
|
1 | """Tests for the key interactiveshell module, where the main ipython class is defined. | |
2 | """ |
|
2 | """ | |
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Module imports |
|
4 | # Module imports | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 |
|
6 | |||
7 | # third party |
|
7 | # third party | |
8 | import nose.tools as nt |
|
8 | import pytest | |
9 |
|
9 | |||
10 | # our own packages |
|
10 | # our own packages | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Test functions |
|
13 | # Test functions | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
16 | def test_reset(): |
|
16 | def test_reset(): | |
17 | """reset must clear most namespaces.""" |
|
17 | """reset must clear most namespaces.""" | |
18 |
|
18 | |||
19 | # Check that reset runs without error |
|
19 | # Check that reset runs without error | |
20 | ip.reset() |
|
20 | ip.reset() | |
21 |
|
21 | |||
22 | # Once we've reset it (to clear of any junk that might have been there from |
|
22 | # Once we've reset it (to clear of any junk that might have been there from | |
23 | # other tests, we can count how many variables are in the user's namespace |
|
23 | # other tests, we can count how many variables are in the user's namespace | |
24 | nvars_user_ns = len(ip.user_ns) |
|
24 | nvars_user_ns = len(ip.user_ns) | |
25 | nvars_hidden = len(ip.user_ns_hidden) |
|
25 | nvars_hidden = len(ip.user_ns_hidden) | |
26 |
|
26 | |||
27 | # Now add a few variables to user_ns, and check that reset clears them |
|
27 | # Now add a few variables to user_ns, and check that reset clears them | |
28 | ip.user_ns['x'] = 1 |
|
28 | ip.user_ns['x'] = 1 | |
29 | ip.user_ns['y'] = 1 |
|
29 | ip.user_ns['y'] = 1 | |
30 | ip.reset() |
|
30 | ip.reset() | |
31 |
|
31 | |||
32 | # Finally, check that all namespaces have only as many variables as we |
|
32 | # Finally, check that all namespaces have only as many variables as we | |
33 | # expect to find in them: |
|
33 | # expect to find in them: | |
34 |
|
|
34 | assert len(ip.user_ns) == nvars_user_ns | |
35 |
|
|
35 | assert len(ip.user_ns_hidden) == nvars_hidden | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | # Tests for reporting of exceptions in various modes, handling of SystemExit, |
|
38 | # Tests for reporting of exceptions in various modes, handling of SystemExit, | |
39 | # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell. |
|
39 | # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell. | |
40 |
|
40 | |||
41 | def doctest_tb_plain(): |
|
41 | def doctest_tb_plain(): | |
42 | """ |
|
42 | """ | |
43 | In [18]: xmode plain |
|
43 | In [18]: xmode plain | |
44 | Exception reporting mode: Plain |
|
44 | Exception reporting mode: Plain | |
45 |
|
45 | |||
46 | In [19]: run simpleerr.py |
|
46 | In [19]: run simpleerr.py | |
47 | Traceback (most recent call last): |
|
47 | Traceback (most recent call last): | |
48 | ...line 32, in <module> |
|
48 | ...line 32, in <module> | |
49 | bar(mode) |
|
49 | bar(mode) | |
50 | ...line 16, in bar |
|
50 | ...line 16, in bar | |
51 | div0() |
|
51 | div0() | |
52 | ...line 8, in div0 |
|
52 | ...line 8, in div0 | |
53 | x/y |
|
53 | x/y | |
54 | ZeroDivisionError: ... |
|
54 | ZeroDivisionError: ... | |
55 | """ |
|
55 | """ | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | def doctest_tb_context(): |
|
58 | def doctest_tb_context(): | |
59 | """ |
|
59 | """ | |
60 | In [3]: xmode context |
|
60 | In [3]: xmode context | |
61 | Exception reporting mode: Context |
|
61 | Exception reporting mode: Context | |
62 |
|
62 | |||
63 | In [4]: run simpleerr.py |
|
63 | In [4]: run simpleerr.py | |
64 | --------------------------------------------------------------------------- |
|
64 | --------------------------------------------------------------------------- | |
65 | ZeroDivisionError Traceback (most recent call last) |
|
65 | ZeroDivisionError Traceback (most recent call last) | |
66 | <BLANKLINE> |
|
66 | <BLANKLINE> | |
67 | ... in <module> |
|
67 | ... in <module> | |
68 | 29 except IndexError: |
|
68 | 29 except IndexError: | |
69 | 30 mode = 'div' |
|
69 | 30 mode = 'div' | |
70 | ---> 32 bar(mode) |
|
70 | ---> 32 bar(mode) | |
71 | <BLANKLINE> |
|
71 | <BLANKLINE> | |
72 | ... in bar(mode) |
|
72 | ... in bar(mode) | |
73 | 14 "bar" |
|
73 | 14 "bar" | |
74 | 15 if mode=='div': |
|
74 | 15 if mode=='div': | |
75 | ---> 16 div0() |
|
75 | ---> 16 div0() | |
76 | 17 elif mode=='exit': |
|
76 | 17 elif mode=='exit': | |
77 | 18 try: |
|
77 | 18 try: | |
78 | <BLANKLINE> |
|
78 | <BLANKLINE> | |
79 | ... in div0() |
|
79 | ... in div0() | |
80 | 6 x = 1 |
|
80 | 6 x = 1 | |
81 | 7 y = 0 |
|
81 | 7 y = 0 | |
82 | ----> 8 x/y |
|
82 | ----> 8 x/y | |
83 | <BLANKLINE> |
|
83 | <BLANKLINE> | |
84 | ZeroDivisionError: ... |
|
84 | ZeroDivisionError: ... | |
85 | """ |
|
85 | """ | |
86 |
|
86 | |||
87 |
|
87 | |||
88 | def doctest_tb_verbose(): |
|
88 | def doctest_tb_verbose(): | |
89 | """ |
|
89 | """ | |
90 | In [5]: xmode verbose |
|
90 | In [5]: xmode verbose | |
91 | Exception reporting mode: Verbose |
|
91 | Exception reporting mode: Verbose | |
92 |
|
92 | |||
93 | In [6]: run simpleerr.py |
|
93 | In [6]: run simpleerr.py | |
94 | --------------------------------------------------------------------------- |
|
94 | --------------------------------------------------------------------------- | |
95 | ZeroDivisionError Traceback (most recent call last) |
|
95 | ZeroDivisionError Traceback (most recent call last) | |
96 | <BLANKLINE> |
|
96 | <BLANKLINE> | |
97 | ... in <module> |
|
97 | ... in <module> | |
98 | 29 except IndexError: |
|
98 | 29 except IndexError: | |
99 | 30 mode = 'div' |
|
99 | 30 mode = 'div' | |
100 | ---> 32 bar(mode) |
|
100 | ---> 32 bar(mode) | |
101 | mode = 'div' |
|
101 | mode = 'div' | |
102 | <BLANKLINE> |
|
102 | <BLANKLINE> | |
103 | ... in bar(mode='div') |
|
103 | ... in bar(mode='div') | |
104 | 14 "bar" |
|
104 | 14 "bar" | |
105 | 15 if mode=='div': |
|
105 | 15 if mode=='div': | |
106 | ---> 16 div0() |
|
106 | ---> 16 div0() | |
107 | 17 elif mode=='exit': |
|
107 | 17 elif mode=='exit': | |
108 | 18 try: |
|
108 | 18 try: | |
109 | <BLANKLINE> |
|
109 | <BLANKLINE> | |
110 | ... in div0() |
|
110 | ... in div0() | |
111 | 6 x = 1 |
|
111 | 6 x = 1 | |
112 | 7 y = 0 |
|
112 | 7 y = 0 | |
113 | ----> 8 x/y |
|
113 | ----> 8 x/y | |
114 | x = 1 |
|
114 | x = 1 | |
115 | y = 0 |
|
115 | y = 0 | |
116 | <BLANKLINE> |
|
116 | <BLANKLINE> | |
117 | ZeroDivisionError: ... |
|
117 | ZeroDivisionError: ... | |
118 | """ |
|
118 | """ | |
119 |
|
119 | |||
120 | # TODO : Marc 2021 – this seem to fail due |
|
120 | # TODO : Marc 2021 – this seem to fail due | |
121 | # to upstream changes in CI for whatever reason. |
|
121 | # to upstream changes in CI for whatever reason. | |
122 | # Commenting for now, to revive someday (maybe?) |
|
122 | # Commenting for now, to revive someday (maybe?) | |
123 | # nose won't work in 3.10 anyway and we'll have to disable iptest. |
|
123 | # nose won't work in 3.10 anyway and we'll have to disable iptest. | |
124 | # thus this likely need to bemigrated to pytest. |
|
124 | # thus this likely need to bemigrated to pytest. | |
125 |
|
125 | |||
126 |
|
126 | |||
127 | # def doctest_tb_sysexit(): |
|
127 | # def doctest_tb_sysexit(): | |
128 | # """ |
|
128 | # """ | |
129 | # In [17]: %xmode plain |
|
129 | # In [17]: %xmode plain | |
130 | # Exception reporting mode: Plain |
|
130 | # Exception reporting mode: Plain | |
131 | # |
|
131 | # | |
132 | # In [18]: %run simpleerr.py exit |
|
132 | # In [18]: %run simpleerr.py exit | |
133 | # An exception has occurred, use %tb to see the full traceback. |
|
133 | # An exception has occurred, use %tb to see the full traceback. | |
134 | # SystemExit: (1, 'Mode = exit') |
|
134 | # SystemExit: (1, 'Mode = exit') | |
135 | # |
|
135 | # | |
136 | # In [19]: %run simpleerr.py exit 2 |
|
136 | # In [19]: %run simpleerr.py exit 2 | |
137 | # An exception has occurred, use %tb to see the full traceback. |
|
137 | # An exception has occurred, use %tb to see the full traceback. | |
138 | # SystemExit: (2, 'Mode = exit') |
|
138 | # SystemExit: (2, 'Mode = exit') | |
139 | # |
|
139 | # | |
140 | # In [20]: %tb |
|
140 | # In [20]: %tb | |
141 | # Traceback (most recent call last): |
|
141 | # Traceback (most recent call last): | |
142 | # File ... in <module> |
|
142 | # File ... in <module> | |
143 | # bar(mode) |
|
143 | # bar(mode) | |
144 | # File ... line 22, in bar |
|
144 | # File ... line 22, in bar | |
145 | # sysexit(stat, mode) |
|
145 | # sysexit(stat, mode) | |
146 | # File ... line 11, in sysexit |
|
146 | # File ... line 11, in sysexit | |
147 | # raise SystemExit(stat, 'Mode = %s' % mode) |
|
147 | # raise SystemExit(stat, 'Mode = %s' % mode) | |
148 | # SystemExit: (2, 'Mode = exit') |
|
148 | # SystemExit: (2, 'Mode = exit') | |
149 | # |
|
149 | # | |
150 | # In [21]: %xmode context |
|
150 | # In [21]: %xmode context | |
151 | # Exception reporting mode: Context |
|
151 | # Exception reporting mode: Context | |
152 | # |
|
152 | # | |
153 | # In [22]: %tb |
|
153 | # In [22]: %tb | |
154 | # --------------------------------------------------------------------------- |
|
154 | # --------------------------------------------------------------------------- | |
155 | # SystemExit Traceback (most recent call last) |
|
155 | # SystemExit Traceback (most recent call last) | |
156 | # <BLANKLINE> |
|
156 | # <BLANKLINE> | |
157 | # ...<module> |
|
157 | # ...<module> | |
158 | # 29 except IndexError: |
|
158 | # 29 except IndexError: | |
159 | # 30 mode = 'div' |
|
159 | # 30 mode = 'div' | |
160 | # ---> 32 bar(mode) |
|
160 | # ---> 32 bar(mode) | |
161 | # <BLANKLINE> |
|
161 | # <BLANKLINE> | |
162 | # ...bar(mode) |
|
162 | # ...bar(mode) | |
163 | # 20 except: |
|
163 | # 20 except: | |
164 | # 21 stat = 1 |
|
164 | # 21 stat = 1 | |
165 | # ---> 22 sysexit(stat, mode) |
|
165 | # ---> 22 sysexit(stat, mode) | |
166 | # 23 else: |
|
166 | # 23 else: | |
167 | # 24 raise ValueError('Unknown mode') |
|
167 | # 24 raise ValueError('Unknown mode') | |
168 | # <BLANKLINE> |
|
168 | # <BLANKLINE> | |
169 | # ...sysexit(stat, mode) |
|
169 | # ...sysexit(stat, mode) | |
170 | # 10 def sysexit(stat, mode): |
|
170 | # 10 def sysexit(stat, mode): | |
171 | # ---> 11 raise SystemExit(stat, 'Mode = %s' % mode) |
|
171 | # ---> 11 raise SystemExit(stat, 'Mode = %s' % mode) | |
172 | # <BLANKLINE> |
|
172 | # <BLANKLINE> | |
173 | # SystemExit: (2, 'Mode = exit') |
|
173 | # SystemExit: (2, 'Mode = exit') | |
174 | # |
|
174 | # | |
175 | # In [23]: %xmode verbose |
|
175 | # In [23]: %xmode verbose | |
176 | # Exception reporting mode: Verbose |
|
176 | # Exception reporting mode: Verbose | |
177 | # |
|
177 | # | |
178 | # In [24]: %tb |
|
178 | # In [24]: %tb | |
179 | # --------------------------------------------------------------------------- |
|
179 | # --------------------------------------------------------------------------- | |
180 | # SystemExit Traceback (most recent call last) |
|
180 | # SystemExit Traceback (most recent call last) | |
181 | # <BLANKLINE> |
|
181 | # <BLANKLINE> | |
182 | # ... in <module> |
|
182 | # ... in <module> | |
183 | # 29 except IndexError: |
|
183 | # 29 except IndexError: | |
184 | # 30 mode = 'div' |
|
184 | # 30 mode = 'div' | |
185 | # ---> 32 bar(mode) |
|
185 | # ---> 32 bar(mode) | |
186 | # mode = 'exit' |
|
186 | # mode = 'exit' | |
187 | # <BLANKLINE> |
|
187 | # <BLANKLINE> | |
188 | # ... in bar(mode='exit') |
|
188 | # ... in bar(mode='exit') | |
189 | # 20 except: |
|
189 | # 20 except: | |
190 | # 21 stat = 1 |
|
190 | # 21 stat = 1 | |
191 | # ---> 22 sysexit(stat, mode) |
|
191 | # ---> 22 sysexit(stat, mode) | |
192 | # mode = 'exit' |
|
192 | # mode = 'exit' | |
193 | # stat = 2 |
|
193 | # stat = 2 | |
194 | # 23 else: |
|
194 | # 23 else: | |
195 | # 24 raise ValueError('Unknown mode') |
|
195 | # 24 raise ValueError('Unknown mode') | |
196 | # <BLANKLINE> |
|
196 | # <BLANKLINE> | |
197 | # ... in sysexit(stat=2, mode='exit') |
|
197 | # ... in sysexit(stat=2, mode='exit') | |
198 | # 10 def sysexit(stat, mode): |
|
198 | # 10 def sysexit(stat, mode): | |
199 | # ---> 11 raise SystemExit(stat, 'Mode = %s' % mode) |
|
199 | # ---> 11 raise SystemExit(stat, 'Mode = %s' % mode) | |
200 | # stat = 2 |
|
200 | # stat = 2 | |
201 | # mode = 'exit' |
|
201 | # mode = 'exit' | |
202 | # <BLANKLINE> |
|
202 | # <BLANKLINE> | |
203 | # SystemExit: (2, 'Mode = exit') |
|
203 | # SystemExit: (2, 'Mode = exit') | |
204 | # """ |
|
204 | # """ | |
205 |
|
205 | |||
206 |
|
206 | |||
207 | def test_run_cell(): |
|
207 | def test_run_cell(): | |
208 | import textwrap |
|
208 | import textwrap | |
209 | ip.run_cell('a = 10\na+=1') |
|
209 | ip.run_cell('a = 10\na+=1') | |
210 | ip.run_cell('assert a == 11\nassert 1') |
|
210 | ip.run_cell('assert a == 11\nassert 1') | |
211 |
|
211 | |||
212 |
|
|
212 | assert ip.user_ns['a'] == 11 | |
213 | complex = textwrap.dedent(""" |
|
213 | complex = textwrap.dedent(""" | |
214 | if 1: |
|
214 | if 1: | |
215 | print "hello" |
|
215 | print "hello" | |
216 | if 1: |
|
216 | if 1: | |
217 | print "world" |
|
217 | print "world" | |
218 |
|
218 | |||
219 | if 2: |
|
219 | if 2: | |
220 | print "foo" |
|
220 | print "foo" | |
221 |
|
221 | |||
222 | if 3: |
|
222 | if 3: | |
223 | print "bar" |
|
223 | print "bar" | |
224 |
|
224 | |||
225 | if 4: |
|
225 | if 4: | |
226 | print "bar" |
|
226 | print "bar" | |
227 |
|
227 | |||
228 | """) |
|
228 | """) | |
229 | # Simply verifies that this kind of input is run |
|
229 | # Simply verifies that this kind of input is run | |
230 | ip.run_cell(complex) |
|
230 | ip.run_cell(complex) | |
231 |
|
231 | |||
232 |
|
232 | |||
233 | def test_db(): |
|
233 | def test_db(): | |
234 | """Test the internal database used for variable persistence.""" |
|
234 | """Test the internal database used for variable persistence.""" | |
235 | ip.db['__unittest_'] = 12 |
|
235 | ip.db['__unittest_'] = 12 | |
236 |
|
|
236 | assert ip.db['__unittest_'] == 12 | |
237 | del ip.db['__unittest_'] |
|
237 | del ip.db['__unittest_'] | |
238 | assert '__unittest_' not in ip.db |
|
238 | assert '__unittest_' not in ip.db |
@@ -1,30 +1,30 | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Test IPython.core.logger""" |
|
2 | """Test IPython.core.logger""" | |
3 |
|
3 | |||
4 | import os.path |
|
4 | import os.path | |
|
5 | import pytest | |||
5 |
|
6 | |||
6 | import nose.tools as nt |
|
|||
7 | from IPython.utils.tempdir import TemporaryDirectory |
|
7 | from IPython.utils.tempdir import TemporaryDirectory | |
8 |
|
8 | |||
9 | def test_logstart_inaccessible_file(): |
|
9 | def test_logstart_inaccessible_file(): | |
10 | try: |
|
10 | try: | |
11 | _ip.logger.logstart(logfname="/") # Opening that filename will fail. |
|
11 | _ip.logger.logstart(logfname="/") # Opening that filename will fail. | |
12 | except IOError: |
|
12 | except IOError: | |
13 | pass |
|
13 | pass | |
14 | else: |
|
14 | else: | |
15 |
|
|
15 | assert False # The try block should never pass. | |
16 |
|
16 | |||
17 | try: |
|
17 | try: | |
18 | _ip.run_cell("a=1") # Check it doesn't try to log this |
|
18 | _ip.run_cell("a=1") # Check it doesn't try to log this | |
19 | finally: |
|
19 | finally: | |
20 | _ip.logger.log_active = False # If this fails, don't let later tests fail |
|
20 | _ip.logger.log_active = False # If this fails, don't let later tests fail | |
21 |
|
21 | |||
22 | def test_logstart_unicode(): |
|
22 | def test_logstart_unicode(): | |
23 | with TemporaryDirectory() as tdir: |
|
23 | with TemporaryDirectory() as tdir: | |
24 | logfname = os.path.join(tdir, "test_unicode.log") |
|
24 | logfname = os.path.join(tdir, "test_unicode.log") | |
25 | _ip.run_cell("'abc€'") |
|
25 | _ip.run_cell("'abc€'") | |
26 | try: |
|
26 | try: | |
27 | _ip.magic("logstart -to %s" % logfname) |
|
27 | _ip.magic("logstart -to %s" % logfname) | |
28 | _ip.run_cell("'abc€'") |
|
28 | _ip.run_cell("'abc€'") | |
29 | finally: |
|
29 | finally: | |
30 | _ip.logger.logstop() |
|
30 | _ip.logger.logstop() |
@@ -1,118 +1,118 | |||||
1 | #----------------------------------------------------------------------------- |
|
1 | #----------------------------------------------------------------------------- | |
2 | # Copyright (C) 2010-2011, IPython Development Team. |
|
2 | # Copyright (C) 2010-2011, IPython Development Team. | |
3 | # |
|
3 | # | |
4 | # Distributed under the terms of the Modified BSD License. |
|
4 | # Distributed under the terms of the Modified BSD License. | |
5 | # |
|
5 | # | |
6 | # The full license is in the file COPYING.txt, distributed with this software. |
|
6 | # The full license is in the file COPYING.txt, distributed with this software. | |
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 |
|
8 | |||
9 | import argparse |
|
9 | import argparse | |
10 | from nose.tools import assert_equal |
|
10 | import pytest | |
11 |
|
11 | |||
12 | from IPython.core.magic_arguments import (argument, argument_group, kwds, |
|
12 | from IPython.core.magic_arguments import (argument, argument_group, kwds, | |
13 | magic_arguments, parse_argstring, real_name) |
|
13 | magic_arguments, parse_argstring, real_name) | |
14 |
|
14 | |||
15 |
|
15 | |||
16 | @magic_arguments() |
|
16 | @magic_arguments() | |
17 | @argument('-f', '--foo', help="an argument") |
|
17 | @argument('-f', '--foo', help="an argument") | |
18 | def magic_foo1(self, args): |
|
18 | def magic_foo1(self, args): | |
19 | """ A docstring. |
|
19 | """ A docstring. | |
20 | """ |
|
20 | """ | |
21 | return parse_argstring(magic_foo1, args) |
|
21 | return parse_argstring(magic_foo1, args) | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | @magic_arguments() |
|
24 | @magic_arguments() | |
25 | def magic_foo2(self, args): |
|
25 | def magic_foo2(self, args): | |
26 | """ A docstring. |
|
26 | """ A docstring. | |
27 | """ |
|
27 | """ | |
28 | return parse_argstring(magic_foo2, args) |
|
28 | return parse_argstring(magic_foo2, args) | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | @magic_arguments() |
|
31 | @magic_arguments() | |
32 | @argument('-f', '--foo', help="an argument") |
|
32 | @argument('-f', '--foo', help="an argument") | |
33 | @argument_group('Group') |
|
33 | @argument_group('Group') | |
34 | @argument('-b', '--bar', help="a grouped argument") |
|
34 | @argument('-b', '--bar', help="a grouped argument") | |
35 | @argument_group('Second Group') |
|
35 | @argument_group('Second Group') | |
36 | @argument('-z', '--baz', help="another grouped argument") |
|
36 | @argument('-z', '--baz', help="another grouped argument") | |
37 | def magic_foo3(self, args): |
|
37 | def magic_foo3(self, args): | |
38 | """ A docstring. |
|
38 | """ A docstring. | |
39 | """ |
|
39 | """ | |
40 | return parse_argstring(magic_foo3, args) |
|
40 | return parse_argstring(magic_foo3, args) | |
41 |
|
41 | |||
42 |
|
42 | |||
43 | @magic_arguments() |
|
43 | @magic_arguments() | |
44 | @kwds(argument_default=argparse.SUPPRESS) |
|
44 | @kwds(argument_default=argparse.SUPPRESS) | |
45 | @argument('-f', '--foo', help="an argument") |
|
45 | @argument('-f', '--foo', help="an argument") | |
46 | def magic_foo4(self, args): |
|
46 | def magic_foo4(self, args): | |
47 | """ A docstring. |
|
47 | """ A docstring. | |
48 | """ |
|
48 | """ | |
49 | return parse_argstring(magic_foo4, args) |
|
49 | return parse_argstring(magic_foo4, args) | |
50 |
|
50 | |||
51 |
|
51 | |||
52 | @magic_arguments('frobnicate') |
|
52 | @magic_arguments('frobnicate') | |
53 | @argument('-f', '--foo', help="an argument") |
|
53 | @argument('-f', '--foo', help="an argument") | |
54 | def magic_foo5(self, args): |
|
54 | def magic_foo5(self, args): | |
55 | """ A docstring. |
|
55 | """ A docstring. | |
56 | """ |
|
56 | """ | |
57 | return parse_argstring(magic_foo5, args) |
|
57 | return parse_argstring(magic_foo5, args) | |
58 |
|
58 | |||
59 |
|
59 | |||
60 | @magic_arguments() |
|
60 | @magic_arguments() | |
61 | @argument('-f', '--foo', help="an argument") |
|
61 | @argument('-f', '--foo', help="an argument") | |
62 | def magic_magic_foo(self, args): |
|
62 | def magic_magic_foo(self, args): | |
63 | """ A docstring. |
|
63 | """ A docstring. | |
64 | """ |
|
64 | """ | |
65 | return parse_argstring(magic_magic_foo, args) |
|
65 | return parse_argstring(magic_magic_foo, args) | |
66 |
|
66 | |||
67 |
|
67 | |||
68 | @magic_arguments() |
|
68 | @magic_arguments() | |
69 | @argument('-f', '--foo', help="an argument") |
|
69 | @argument('-f', '--foo', help="an argument") | |
70 | def foo(self, args): |
|
70 | def foo(self, args): | |
71 | """ A docstring. |
|
71 | """ A docstring. | |
72 | """ |
|
72 | """ | |
73 | return parse_argstring(foo, args) |
|
73 | return parse_argstring(foo, args) | |
74 |
|
74 | |||
75 |
|
75 | |||
76 | def test_magic_arguments(): |
|
76 | def test_magic_arguments(): | |
77 |
assert |
|
77 | assert magic_foo1.__doc__ == '::\n\n %foo1 [-f FOO]\n\n A docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n' | |
78 |
assert |
|
78 | assert getattr(magic_foo1, 'argcmd_name', None) == None | |
79 |
assert |
|
79 | assert real_name(magic_foo1) == 'foo1' | |
80 |
assert |
|
80 | assert magic_foo1(None, '') == argparse.Namespace(foo=None) | |
81 | assert hasattr(magic_foo1, 'has_arguments') |
|
81 | assert hasattr(magic_foo1, 'has_arguments') | |
82 |
|
82 | |||
83 |
assert |
|
83 | assert magic_foo2.__doc__ == '::\n\n %foo2\n\n A docstring.\n' | |
84 |
assert |
|
84 | assert getattr(magic_foo2, 'argcmd_name', None) == None | |
85 |
assert |
|
85 | assert real_name(magic_foo2) == 'foo2' | |
86 |
assert |
|
86 | assert magic_foo2(None, '') == argparse.Namespace() | |
87 | assert hasattr(magic_foo2, 'has_arguments') |
|
87 | assert hasattr(magic_foo2, 'has_arguments') | |
88 |
|
88 | |||
89 |
assert |
|
89 | assert magic_foo3.__doc__ == '::\n\n %foo3 [-f FOO] [-b BAR] [-z BAZ]\n\n A docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n\nGroup:\n -b BAR, --bar BAR a grouped argument\n\nSecond Group:\n -z BAZ, --baz BAZ another grouped argument\n' | |
90 |
assert |
|
90 | assert getattr(magic_foo3, 'argcmd_name', None) == None | |
91 |
assert |
|
91 | assert real_name(magic_foo3) == 'foo3' | |
92 |
assert |
|
92 | assert (magic_foo3(None, '') == | |
93 | argparse.Namespace(bar=None, baz=None, foo=None)) |
|
93 | argparse.Namespace(bar=None, baz=None, foo=None)) | |
94 | assert hasattr(magic_foo3, 'has_arguments') |
|
94 | assert hasattr(magic_foo3, 'has_arguments') | |
95 |
|
95 | |||
96 |
assert |
|
96 | assert magic_foo4.__doc__ == '::\n\n %foo4 [-f FOO]\n\n A docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n' | |
97 |
assert |
|
97 | assert getattr(magic_foo4, 'argcmd_name', None) == None | |
98 |
assert |
|
98 | assert real_name(magic_foo4) == 'foo4' | |
99 |
assert |
|
99 | assert magic_foo4(None, '') == argparse.Namespace() | |
100 | assert hasattr(magic_foo4, 'has_arguments') |
|
100 | assert hasattr(magic_foo4, 'has_arguments') | |
101 |
|
101 | |||
102 |
assert |
|
102 | assert magic_foo5.__doc__ == '::\n\n %frobnicate [-f FOO]\n\n A docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n' | |
103 |
assert |
|
103 | assert getattr(magic_foo5, 'argcmd_name', None) == 'frobnicate' | |
104 |
assert |
|
104 | assert real_name(magic_foo5) == 'frobnicate' | |
105 |
assert |
|
105 | assert magic_foo5(None, '') == argparse.Namespace(foo=None) | |
106 | assert hasattr(magic_foo5, 'has_arguments') |
|
106 | assert hasattr(magic_foo5, 'has_arguments') | |
107 |
|
107 | |||
108 |
assert |
|
108 | assert magic_magic_foo.__doc__ == '::\n\n %magic_foo [-f FOO]\n\n A docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n' | |
109 |
assert |
|
109 | assert getattr(magic_magic_foo, 'argcmd_name', None) == None | |
110 |
assert |
|
110 | assert real_name(magic_magic_foo) == 'magic_foo' | |
111 |
assert |
|
111 | assert magic_magic_foo(None, '') == argparse.Namespace(foo=None) | |
112 | assert hasattr(magic_magic_foo, 'has_arguments') |
|
112 | assert hasattr(magic_magic_foo, 'has_arguments') | |
113 |
|
113 | |||
114 |
assert |
|
114 | assert foo.__doc__ == '::\n\n %foo [-f FOO]\n\n A docstring.\n\noptional arguments:\n -f FOO, --foo FOO an argument\n' | |
115 |
assert |
|
115 | assert getattr(foo, 'argcmd_name', None) == None | |
116 |
assert |
|
116 | assert real_name(foo) == 'foo' | |
117 |
assert |
|
117 | assert foo(None, '') == argparse.Namespace(foo=None) | |
118 | assert hasattr(foo, 'has_arguments') |
|
118 | assert hasattr(foo, 'has_arguments') |
@@ -1,127 +1,127 | |||||
1 | """Tests for input manipulation machinery.""" |
|
1 | """Tests for input manipulation machinery.""" | |
2 |
|
2 | |||
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Imports |
|
4 | # Imports | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | import nose.tools as nt |
|
6 | import pytest | |
7 |
|
7 | |||
8 | from IPython.core.prefilter import AutocallChecker |
|
8 | from IPython.core.prefilter import AutocallChecker | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Tests |
|
11 | # Tests | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | def test_prefilter(): |
|
14 | def test_prefilter(): | |
15 | """Test user input conversions""" |
|
15 | """Test user input conversions""" | |
16 |
|
16 | |||
17 | # pairs of (raw, expected correct) input |
|
17 | # pairs of (raw, expected correct) input | |
18 | pairs = [ ('2+2','2+2'), |
|
18 | pairs = [ ('2+2','2+2'), | |
19 | ] |
|
19 | ] | |
20 |
|
20 | |||
21 | for raw, correct in pairs: |
|
21 | for raw, correct in pairs: | |
22 |
|
|
22 | assert ip.prefilter(raw) == correct | |
23 |
|
23 | |||
24 | def test_prefilter_shadowed(): |
|
24 | def test_prefilter_shadowed(): | |
25 | def dummy_magic(line): pass |
|
25 | def dummy_magic(line): pass | |
26 |
|
26 | |||
27 | prev_automagic_state = ip.automagic |
|
27 | prev_automagic_state = ip.automagic | |
28 | ip.automagic = True |
|
28 | ip.automagic = True | |
29 | ip.autocall = 0 |
|
29 | ip.autocall = 0 | |
30 |
|
30 | |||
31 | try: |
|
31 | try: | |
32 | # These should not be transformed - they are shadowed by other names |
|
32 | # These should not be transformed - they are shadowed by other names | |
33 | for name in ['if', 'zip', 'get_ipython']: # keyword, builtin, global |
|
33 | for name in ['if', 'zip', 'get_ipython']: # keyword, builtin, global | |
34 | ip.register_magic_function(dummy_magic, magic_name=name) |
|
34 | ip.register_magic_function(dummy_magic, magic_name=name) | |
35 | res = ip.prefilter(name+' foo') |
|
35 | res = ip.prefilter(name+' foo') | |
36 |
|
|
36 | assert res == name+' foo' | |
37 | del ip.magics_manager.magics['line'][name] |
|
37 | del ip.magics_manager.magics['line'][name] | |
38 |
|
38 | |||
39 | # These should be transformed |
|
39 | # These should be transformed | |
40 | for name in ['fi', 'piz', 'nohtypi_teg']: |
|
40 | for name in ['fi', 'piz', 'nohtypi_teg']: | |
41 | ip.register_magic_function(dummy_magic, magic_name=name) |
|
41 | ip.register_magic_function(dummy_magic, magic_name=name) | |
42 | res = ip.prefilter(name+' foo') |
|
42 | res = ip.prefilter(name+' foo') | |
43 |
|
|
43 | assert res != name+' foo' | |
44 | del ip.magics_manager.magics['line'][name] |
|
44 | del ip.magics_manager.magics['line'][name] | |
45 |
|
45 | |||
46 | finally: |
|
46 | finally: | |
47 | ip.automagic = prev_automagic_state |
|
47 | ip.automagic = prev_automagic_state | |
48 |
|
48 | |||
49 | def test_autocall_binops(): |
|
49 | def test_autocall_binops(): | |
50 | """See https://github.com/ipython/ipython/issues/81""" |
|
50 | """See https://github.com/ipython/ipython/issues/81""" | |
51 | ip.magic('autocall 2') |
|
51 | ip.magic('autocall 2') | |
52 | f = lambda x: x |
|
52 | f = lambda x: x | |
53 | ip.user_ns['f'] = f |
|
53 | ip.user_ns['f'] = f | |
54 | try: |
|
54 | try: | |
55 |
|
|
55 | assert ip.prefilter('f 1') =='f(1)' | |
56 | for t in ['f +1', 'f -1']: |
|
56 | for t in ['f +1', 'f -1']: | |
57 |
|
|
57 | assert ip.prefilter(t) == t | |
58 |
|
58 | |||
59 | # Run tests again with a more permissive exclude_regexp, which will |
|
59 | # Run tests again with a more permissive exclude_regexp, which will | |
60 | # allow transformation of binary operations ('f -1' -> 'f(-1)'). |
|
60 | # allow transformation of binary operations ('f -1' -> 'f(-1)'). | |
61 | pm = ip.prefilter_manager |
|
61 | pm = ip.prefilter_manager | |
62 | ac = AutocallChecker(shell=pm.shell, prefilter_manager=pm, |
|
62 | ac = AutocallChecker(shell=pm.shell, prefilter_manager=pm, | |
63 | config=pm.config) |
|
63 | config=pm.config) | |
64 | try: |
|
64 | try: | |
65 | ac.priority = 1 |
|
65 | ac.priority = 1 | |
66 | ac.exclude_regexp = r'^[,&^\|\*/]|^is |^not |^in |^and |^or ' |
|
66 | ac.exclude_regexp = r'^[,&^\|\*/]|^is |^not |^in |^and |^or ' | |
67 | pm.sort_checkers() |
|
67 | pm.sort_checkers() | |
68 |
|
68 | |||
69 |
|
|
69 | assert ip.prefilter('f -1') == 'f(-1)' | |
70 |
|
|
70 | assert ip.prefilter('f +1') == 'f(+1)' | |
71 | finally: |
|
71 | finally: | |
72 | pm.unregister_checker(ac) |
|
72 | pm.unregister_checker(ac) | |
73 | finally: |
|
73 | finally: | |
74 | ip.magic('autocall 0') |
|
74 | ip.magic('autocall 0') | |
75 | del ip.user_ns['f'] |
|
75 | del ip.user_ns['f'] | |
76 |
|
76 | |||
77 |
|
77 | |||
78 | def test_issue_114(): |
|
78 | def test_issue_114(): | |
79 | """Check that multiline string literals don't expand as magic |
|
79 | """Check that multiline string literals don't expand as magic | |
80 | see http://github.com/ipython/ipython/issues/114""" |
|
80 | see http://github.com/ipython/ipython/issues/114""" | |
81 |
|
81 | |||
82 | template = '"""\n%s\n"""' |
|
82 | template = '"""\n%s\n"""' | |
83 | # Store the current value of multi_line_specials and turn it off before |
|
83 | # Store the current value of multi_line_specials and turn it off before | |
84 | # running test, since it could be true (case in which the test doesn't make |
|
84 | # running test, since it could be true (case in which the test doesn't make | |
85 | # sense, as multiline string literals *will* expand as magic in that case). |
|
85 | # sense, as multiline string literals *will* expand as magic in that case). | |
86 | msp = ip.prefilter_manager.multi_line_specials |
|
86 | msp = ip.prefilter_manager.multi_line_specials | |
87 | ip.prefilter_manager.multi_line_specials = False |
|
87 | ip.prefilter_manager.multi_line_specials = False | |
88 | try: |
|
88 | try: | |
89 | for mgk in ip.magics_manager.lsmagic()['line']: |
|
89 | for mgk in ip.magics_manager.lsmagic()['line']: | |
90 | raw = template % mgk |
|
90 | raw = template % mgk | |
91 |
|
|
91 | assert ip.prefilter(raw) == raw | |
92 | finally: |
|
92 | finally: | |
93 | ip.prefilter_manager.multi_line_specials = msp |
|
93 | ip.prefilter_manager.multi_line_specials = msp | |
94 |
|
94 | |||
95 |
|
95 | |||
96 | def test_prefilter_attribute_errors(): |
|
96 | def test_prefilter_attribute_errors(): | |
97 | """Capture exceptions thrown by user objects on attribute access. |
|
97 | """Capture exceptions thrown by user objects on attribute access. | |
98 |
|
98 | |||
99 | See http://github.com/ipython/ipython/issues/988.""" |
|
99 | See http://github.com/ipython/ipython/issues/988.""" | |
100 |
|
100 | |||
101 | class X(object): |
|
101 | class X(object): | |
102 | def __getattr__(self, k): |
|
102 | def __getattr__(self, k): | |
103 | raise ValueError('broken object') |
|
103 | raise ValueError('broken object') | |
104 | def __call__(self, x): |
|
104 | def __call__(self, x): | |
105 | return x |
|
105 | return x | |
106 |
|
106 | |||
107 | # Create a callable broken object |
|
107 | # Create a callable broken object | |
108 | ip.user_ns['x'] = X() |
|
108 | ip.user_ns['x'] = X() | |
109 | ip.magic('autocall 2') |
|
109 | ip.magic('autocall 2') | |
110 | try: |
|
110 | try: | |
111 | # Even if x throws an attribute error when looking at its rewrite |
|
111 | # Even if x throws an attribute error when looking at its rewrite | |
112 | # attribute, we should not crash. So the test here is simply making |
|
112 | # attribute, we should not crash. So the test here is simply making | |
113 | # the prefilter call and not having an exception. |
|
113 | # the prefilter call and not having an exception. | |
114 | ip.prefilter('x 1') |
|
114 | ip.prefilter('x 1') | |
115 | finally: |
|
115 | finally: | |
116 | del ip.user_ns['x'] |
|
116 | del ip.user_ns['x'] | |
117 | ip.magic('autocall 0') |
|
117 | ip.magic('autocall 0') | |
118 |
|
118 | |||
119 |
|
119 | |||
120 | def test_autocall_should_support_unicode(): |
|
120 | def test_autocall_should_support_unicode(): | |
121 | ip.magic('autocall 2') |
|
121 | ip.magic('autocall 2') | |
122 | ip.user_ns['π'] = lambda x: x |
|
122 | ip.user_ns['π'] = lambda x: x | |
123 | try: |
|
123 | try: | |
124 |
|
|
124 | assert ip.prefilter('π 3') =='π(3)' | |
125 | finally: |
|
125 | finally: | |
126 | ip.magic('autocall 0') |
|
126 | ip.magic('autocall 0') | |
127 | del ip.user_ns['π'] |
|
127 | del ip.user_ns['π'] |
General Comments 0
You need to be logged in to leave comments.
Login now