def test_macro_run(): """Test that we can run a multi-line macro successfully.""" ip = get_ipython() ip.history_manager.reset() cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"), "%macro test 2-3"] for cmd in cmds: ip.run_cell(cmd, store_history=True) nt.assert_equal(ip.user_ns["test"].value, py3compat.doctest_refactor_print("a+=1\nprint a\n")) with tt.AssertPrints("12"): ip.run_cell("test") with tt.AssertPrints("13"): ip.run_cell("test")
def init(self, startup_file, startup, test): # write startup python file with open(os.path.join(self.pd.startup_dir, startup_file), 'w') as f: f.write(startup) # write simple test file, to check that the startup file was run with open(self.fname, 'w') as f: f.write(py3compat.doctest_refactor_print(test))
def testPython(self): """Test the Python runner.""" runner = irunner.PythonRunner(out=self.out) source = doctest_refactor_print(""" print 'hello, this is python' # some more code x=1;y=2 x+y**2 from math import * cos(pi) for i in range(5): print i print "that's all folks!" """) output = doctest_refactor_print("""\ >>> print('hello, this is python') hello, this is python # some more code >>> x=1;y=2 >>> x+y**2 5 >>> from math import * >>> cos(pi) -1.0 >>> for i in range(5): ... print(i) ... 0 1 2 3 4 >>> print("that's all folks!") that's all folks! """) self._test_runner(runner,source,output)
def test_io_init(): """Test that io.stdin/out/err exist at startup""" for name in ("stdin", "stdout", "stderr"): cmd = doctest_refactor_print("from IPython.utils import io;print io.%s.__class__" % name) p = Popen([sys.executable, "-c", cmd], stdout=PIPE) p.wait() classname = p.stdout.read().strip().decode("ascii") # __class__ is a reference to the class object in Python 3, so we can't # just test for string equality. assert "IPython.utils.io.IOStream" in classname, classname
def test_io_init(): """Test that io.stdin/out/err exist at startup""" for name in ('stdin', 'stdout', 'stderr'): cmd = doctest_refactor_print( "from IPython.utils import io;print io.%s.__class__" % name) p = Popen([sys.executable, '-c', cmd], stdout=PIPE) p.wait() classname = p.stdout.read().strip().decode('ascii') # __class__ is a reference to the class object in Python 3, so we can't # just test for string equality. assert 'IPython.utils.io.IOStream' in classname, classname
def test_obj_del(self): """Test that object's __del__ methods are called on exit.""" if sys.platform == 'win32': try: import win32api except ImportError: raise SkipTest("Test requires pywin32") src = ("class A(object):\n" " def __del__(self):\n" " print 'object A deleted'\n" "a = A()\n") self.mktmp(py3compat.doctest_refactor_print(src)) tt.ipexec_validate(self.fname, 'object A deleted')
def test_startup_py(): # create profile dir pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, 'test') # write startup python file with open(os.path.join(pd.startup_dir, '00-start.py'), 'w') as f: f.write('zzz=123\n') # write simple test file, to check that the startup file was run fname = os.path.join(TMP_TEST_DIR, 'test.py') with open(fname, 'w') as f: f.write(py3compat.doctest_refactor_print('print zzz\n')) # validate output tt.ipexec_validate(fname, '123', '', options=['--ipython-dir', IP_TEST_DIR, '--profile', 'test'])
def test_obj_del(self): """Test that object's __del__ methods are called on exit.""" if sys.platform == "win32": try: import win32api except ImportError: raise SkipTest("Test requires pywin32") src = "class A(object):\n" " def __del__(self):\n" " print 'object A deleted'\n" "a = A()\n" self.mktmp(py3compat.doctest_refactor_print(src)) if dec.module_not_available("sqlite3"): err = "WARNING: IPython History requires SQLite, your history will not be saved\n" else: err = None tt.ipexec_validate(self.fname, "object A deleted", err)
def test_startup_py(): # create profile dir pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, 'test') # write startup python file with open(os.path.join(pd.startup_dir, '00-start.py'), 'w') as f: f.write('zzz=123\n') # write simple test file, to check that the startup file was run fname = os.path.join(TMP_TEST_DIR, 'test.py') with open(fname, 'w') as f: f.write(py3compat.doctest_refactor_print('print zzz\n')) # validate output tt.ipexec_validate( fname, '123', '', options=['--ipython-dir', IP_TEST_DIR, '--profile', 'test'])
def test_obj_del(self): """Test that object's __del__ methods are called on exit.""" if sys.platform == 'win32': try: import win32api except ImportError: raise SkipTest("Test requires pywin32") src = ("class A(object):\n" " def __del__(self):\n" " print 'object A deleted'\n" "a = A()\n") self.mktmp(py3compat.doctest_refactor_print(src)) if dec.module_not_available('sqlite3'): err = 'WARNING: IPython History requires SQLite, your history will not be saved\n' else: err = None tt.ipexec_validate(self.fname, 'object A deleted', err)
def test_aggressive_namespace_cleanup(self): """Test that namespace cleanup is not too aggressive GH-238 Returning from another run magic deletes the namespace""" # see ticket https://github.com/ipython/ipython/issues/238 class secondtmp(tt.TempFileMixin): pass empty = secondtmp() empty.mktmp('') src = ("ip = get_ipython()\n" "for i in range(5):\n" " try:\n" " ip.magic('run %s')\n" " except NameError, e:\n" " print i;break\n" % empty.fname) self.mktmp(py3compat.doctest_refactor_print(src)) _ip.magic('run %s' % self.fname) _ip.run_cell('ip == get_ipython()') tt.assert_equals(_ip.user_ns['i'], 5)
def testIPython(self): """Test the IPython runner.""" source = doctest_refactor_print(""" print 'hello, this is python' # some more code x=1;y=2 x+y**2 # An example of autocall functionality from math import * autocall 1 cos pi autocall 0 cos pi cos(pi) for i in range(5): print i print "that's all folks!" exit """) output = doctest_refactor_print("""\ In [1]: print 'hello, this is python' hello, this is python # some more code In [2]: x=1;y=2 In [3]: x+y**2 Out[3]: 5 # An example of autocall functionality In [4]: from math import * In [5]: autocall 1 Automatic calling is: Smart In [6]: cos pi ------> cos(pi) Out[6]: -1.0 In [7]: autocall 0 Automatic calling is: OFF In [8]: cos pi File "<ipython-input-8-6bd7313dd9a9>", line 1 cos pi ^ SyntaxError: invalid syntax In [9]: cos(pi) Out[9]: -1.0 In [10]: for i in range(5): ....: print i ....: 0 1 2 3 4 In [11]: print "that's all folks!" that's all folks! In [12]: exit """) runner = irunner.IPythonRunner(out=self.out) self._test_runner(runner,source,output)
def test_startup_py(self): self.init('00-start.py', 'zzz=123\n', py3compat.doctest_refactor_print('print zzz\n')) self.validate('123')
def test_startup_py(self): self.init("00-start.py", "zzz=123\n", py3compat.doctest_refactor_print("print zzz\n")) self.validate("123")