Show More
@@ -21,8 +21,6 b' from unittest import mock' | |||||
21 |
|
21 | |||
22 | from os.path import join |
|
22 | from os.path import join | |
23 |
|
23 | |||
24 | import nose.tools as nt |
|
|||
25 |
|
||||
26 | from IPython.core.error import InputRejected |
|
24 | from IPython.core.error import InputRejected | |
27 | from IPython.core.inputtransformer import InputTransformer |
|
25 | from IPython.core.inputtransformer import InputTransformer | |
28 | from IPython.core import interactiveshell |
|
26 | from IPython.core import interactiveshell | |
@@ -220,32 +218,38 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
220 | def test_var_expand_local(self): |
|
218 | def test_var_expand_local(self): | |
221 | """Test local variable expansion in !system and %magic calls""" |
|
219 | """Test local variable expansion in !system and %magic calls""" | |
222 | # !system |
|
220 | # !system | |
223 |
ip.run_cell( |
|
221 | ip.run_cell( | |
|
222 | "def test():\n" | |||
224 |
|
|
223 | ' lvar = "ttt"\n' | |
225 |
|
|
224 | " ret = !echo {lvar}\n" | |
226 |
|
|
225 | " return ret[0]\n" | |
227 | res = ip.user_ns['test']() |
|
226 | ) | |
228 | nt.assert_in('ttt', res) |
|
227 | res = ip.user_ns["test"]() | |
|
228 | self.assertIn("ttt", res) | |||
229 |
|
229 | |||
230 | # %magic |
|
230 | # %magic | |
231 |
ip.run_cell( |
|
231 | ip.run_cell( | |
|
232 | "def makemacro():\n" | |||
232 |
|
|
233 | ' macroname = "macro_var_expand_locals"\n' | |
233 |
|
|
234 | " %macro {macroname} codestr\n" | |
234 | ip.user_ns['codestr'] = "str(12)" |
|
235 | ) | |
235 | ip.run_cell('makemacro()') |
|
236 | ip.user_ns["codestr"] = "str(12)" | |
236 | nt.assert_in('macro_var_expand_locals', ip.user_ns) |
|
237 | ip.run_cell("makemacro()") | |
|
238 | self.assertIn("macro_var_expand_locals", ip.user_ns) | |||
237 |
|
239 | |||
238 | def test_var_expand_self(self): |
|
240 | def test_var_expand_self(self): | |
239 | """Test variable expansion with the name 'self', which was failing. |
|
241 | """Test variable expansion with the name 'self', which was failing. | |
240 |
|
242 | |||
241 | See https://github.com/ipython/ipython/issues/1878#issuecomment-7698218 |
|
243 | See https://github.com/ipython/ipython/issues/1878#issuecomment-7698218 | |
242 | """ |
|
244 | """ | |
243 |
ip.run_cell( |
|
245 | ip.run_cell( | |
|
246 | "class cTest:\n" | |||
244 |
|
|
247 | ' classvar="see me"\n' | |
245 |
|
|
248 | " def test(self):\n" | |
246 |
|
|
249 | " res = !echo Variable: {self.classvar}\n" | |
247 |
|
|
250 | " return res[0]\n" | |
248 | nt.assert_in('see me', ip.user_ns['cTest']().test()) |
|
251 | ) | |
|
252 | self.assertIn("see me", ip.user_ns["cTest"]().test()) | |||
249 |
|
253 | |||
250 | def test_bad_var_expand(self): |
|
254 | def test_bad_var_expand(self): | |
251 | """var_expand on invalid formats shouldn't raise""" |
|
255 | """var_expand on invalid formats shouldn't raise""" | |
@@ -346,7 +350,7 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
346 | info = dict(found=True, isalias=False, ismagic=True, |
|
350 | info = dict(found=True, isalias=False, ismagic=True, | |
347 | namespace = 'IPython internal', obj= lmagic.__wrapped__, |
|
351 | namespace = 'IPython internal', obj= lmagic.__wrapped__, | |
348 | parent = None) |
|
352 | parent = None) | |
349 |
|
|
353 | self.assertEqual(lfind, info) | |
350 |
|
354 | |||
351 | def test_ofind_cell_magic(self): |
|
355 | def test_ofind_cell_magic(self): | |
352 | from IPython.core.magic import register_cell_magic |
|
356 | from IPython.core.magic import register_cell_magic | |
@@ -360,7 +364,7 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
360 | info = dict(found=True, isalias=False, ismagic=True, |
|
364 | info = dict(found=True, isalias=False, ismagic=True, | |
361 | namespace = 'IPython internal', obj= cmagic.__wrapped__, |
|
365 | namespace = 'IPython internal', obj= cmagic.__wrapped__, | |
362 | parent = None) |
|
366 | parent = None) | |
363 |
|
|
367 | self.assertEqual(find, info) | |
364 |
|
368 | |||
365 | def test_ofind_property_with_error(self): |
|
369 | def test_ofind_property_with_error(self): | |
366 | class A(object): |
|
370 | class A(object): | |
@@ -372,7 +376,7 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
372 | found = ip._ofind('a.foo', [('locals', locals())]) |
|
376 | found = ip._ofind('a.foo', [('locals', locals())]) | |
373 | info = dict(found=True, isalias=False, ismagic=False, |
|
377 | info = dict(found=True, isalias=False, ismagic=False, | |
374 | namespace='locals', obj=A.foo, parent=a) |
|
378 | namespace='locals', obj=A.foo, parent=a) | |
375 |
|
|
379 | self.assertEqual(found, info) | |
376 |
|
380 | |||
377 | def test_ofind_multiple_attribute_lookups(self): |
|
381 | def test_ofind_multiple_attribute_lookups(self): | |
378 | class A(object): |
|
382 | class A(object): | |
@@ -387,7 +391,7 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
387 | found = ip._ofind('a.a.a.foo', [('locals', locals())]) |
|
391 | found = ip._ofind('a.a.a.foo', [('locals', locals())]) | |
388 | info = dict(found=True, isalias=False, ismagic=False, |
|
392 | info = dict(found=True, isalias=False, ismagic=False, | |
389 | namespace='locals', obj=A.foo, parent=a.a.a) |
|
393 | namespace='locals', obj=A.foo, parent=a.a.a) | |
390 |
|
|
394 | self.assertEqual(found, info) | |
391 |
|
395 | |||
392 | def test_ofind_slotted_attributes(self): |
|
396 | def test_ofind_slotted_attributes(self): | |
393 | class A(object): |
|
397 | class A(object): | |
@@ -399,12 +403,12 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
399 | found = ip._ofind('a.foo', [('locals', locals())]) |
|
403 | found = ip._ofind('a.foo', [('locals', locals())]) | |
400 | info = dict(found=True, isalias=False, ismagic=False, |
|
404 | info = dict(found=True, isalias=False, ismagic=False, | |
401 | namespace='locals', obj=a.foo, parent=a) |
|
405 | namespace='locals', obj=a.foo, parent=a) | |
402 |
|
|
406 | self.assertEqual(found, info) | |
403 |
|
407 | |||
404 | found = ip._ofind('a.bar', [('locals', locals())]) |
|
408 | found = ip._ofind('a.bar', [('locals', locals())]) | |
405 | info = dict(found=False, isalias=False, ismagic=False, |
|
409 | info = dict(found=False, isalias=False, ismagic=False, | |
406 | namespace=None, obj=None, parent=a) |
|
410 | namespace=None, obj=None, parent=a) | |
407 |
|
|
411 | self.assertEqual(found, info) | |
408 |
|
412 | |||
409 | def test_ofind_prefers_property_to_instance_level_attribute(self): |
|
413 | def test_ofind_prefers_property_to_instance_level_attribute(self): | |
410 | class A(object): |
|
414 | class A(object): | |
@@ -412,10 +416,10 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
412 | def foo(self): |
|
416 | def foo(self): | |
413 | return 'bar' |
|
417 | return 'bar' | |
414 | a = A() |
|
418 | a = A() | |
415 |
a.__dict__[ |
|
419 | a.__dict__["foo"] = "baz" | |
416 |
|
|
420 | self.assertEqual(a.foo, "bar") | |
417 |
found = ip._ofind( |
|
421 | found = ip._ofind("a.foo", [("locals", locals())]) | |
418 |
|
|
422 | self.assertIs(found["obj"], A.foo) | |
419 |
|
423 | |||
420 | def test_custom_syntaxerror_exception(self): |
|
424 | def test_custom_syntaxerror_exception(self): | |
421 | called = [] |
|
425 | called = [] | |
@@ -819,7 +823,7 b' class TestAstTransformError(unittest.TestCase):' | |||||
819 | ip.run_cell("1 + 2") |
|
823 | ip.run_cell("1 + 2") | |
820 |
|
824 | |||
821 | # This should have been removed. |
|
825 | # This should have been removed. | |
822 |
|
|
826 | self.assertNotIn(err_transformer, ip.ast_transformers) | |
823 |
|
827 | |||
824 |
|
828 | |||
825 | class StringRejector(ast.NodeTransformer): |
|
829 | class StringRejector(ast.NodeTransformer): | |
@@ -889,20 +893,20 b' def test_user_variables():' | |||||
889 | keys = {'dummy', 'doesnotexist'} |
|
893 | keys = {'dummy', 'doesnotexist'} | |
890 | r = ip.user_expressions({ key:key for key in keys}) |
|
894 | r = ip.user_expressions({ key:key for key in keys}) | |
891 |
|
895 | |||
892 |
|
|
896 | assert keys == set(r.keys()) | |
893 |
dummy = r[ |
|
897 | dummy = r["dummy"] | |
894 |
|
|
898 | assert {"status", "data", "metadata"} == set(dummy.keys()) | |
895 |
|
|
899 | assert dummy["status"] == "ok" | |
896 |
data = dummy[ |
|
900 | data = dummy["data"] | |
897 |
metadata = dummy[ |
|
901 | metadata = dummy["metadata"] | |
898 |
|
|
902 | assert data.get("text/html") == d._repr_html_() | |
899 | js, jsmd = d._repr_javascript_() |
|
903 | js, jsmd = d._repr_javascript_() | |
900 |
|
|
904 | assert data.get("application/javascript") == js | |
901 |
|
|
905 | assert metadata.get("application/javascript") == jsmd | |
902 |
|
906 | |||
903 |
dne = r[ |
|
907 | dne = r["doesnotexist"] | |
904 |
|
|
908 | assert dne["status"] == "error" | |
905 |
|
|
909 | assert dne["ename"] == "NameError" | |
906 |
|
910 | |||
907 | # back to text only |
|
911 | # back to text only | |
908 | ip.display_formatter.active_types = ['text/plain'] |
|
912 | ip.display_formatter.active_types = ['text/plain'] | |
@@ -917,17 +921,17 b' def test_user_expression():' | |||||
917 | r = ip.user_expressions(query) |
|
921 | r = ip.user_expressions(query) | |
918 | import pprint |
|
922 | import pprint | |
919 | pprint.pprint(r) |
|
923 | pprint.pprint(r) | |
920 |
|
|
924 | assert set(r.keys()) == set(query.keys()) | |
921 |
a = r[ |
|
925 | a = r["a"] | |
922 |
|
|
926 | assert {"status", "data", "metadata"} == set(a.keys()) | |
923 |
|
|
927 | assert a["status"] == "ok" | |
924 |
data = a[ |
|
928 | data = a["data"] | |
925 |
metadata = a[ |
|
929 | metadata = a["metadata"] | |
926 |
|
|
930 | assert data.get("text/plain") == "3" | |
927 |
|
931 | |||
928 |
b = r[ |
|
932 | b = r["b"] | |
929 |
|
|
933 | assert b["status"] == "error" | |
930 |
|
|
934 | assert b["ename"] == "ZeroDivisionError" | |
931 |
|
935 | |||
932 | # back to text only |
|
936 | # back to text only | |
933 | ip.display_formatter.active_types = ['text/plain'] |
|
937 | ip.display_formatter.active_types = ['text/plain'] | |
@@ -1030,8 +1034,8 b' def test_custom_exc_count():' | |||||
1030 | ip.run_cell("def foo()", store_history=True) |
|
1034 | ip.run_cell("def foo()", store_history=True) | |
1031 | # restore default excepthook |
|
1035 | # restore default excepthook | |
1032 | ip.set_custom_exc((), None) |
|
1036 | ip.set_custom_exc((), None) | |
1033 |
|
|
1037 | assert hook.call_count == 1 | |
1034 |
|
|
1038 | assert ip.execution_count == before + 1 | |
1035 |
|
1039 | |||
1036 |
|
1040 | |||
1037 | def test_run_cell_async(): |
|
1041 | def test_run_cell_async(): |
General Comments 0
You need to be logged in to leave comments.
Login now