inputhooks
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
"""Tests for pylab tools module.
|
||||
"""
|
||||
#-----------------------------------------------------------------------------
|
||||
# Copyright (c) 2011, the yap_ipython Development Team.
|
||||
#
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
#
|
||||
# The full license is in the file COPYING.txt, distributed with this software.
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Imports
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
# Stdlib imports
|
||||
import time
|
||||
|
||||
# Third-party imports
|
||||
import nose.tools as nt
|
||||
|
||||
# Our own imports
|
||||
from yap_ipython.lib import backgroundjobs as bg
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Globals and constants
|
||||
#-----------------------------------------------------------------------------
|
||||
t_short = 0.0001 # very short interval to wait on jobs
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Local utilities
|
||||
#-----------------------------------------------------------------------------
|
||||
def sleeper(interval=t_short, *a, **kw):
|
||||
args = dict(interval=interval,
|
||||
other_args=a,
|
||||
kw_args=kw)
|
||||
time.sleep(interval)
|
||||
return args
|
||||
|
||||
def crasher(interval=t_short, *a, **kw):
|
||||
time.sleep(interval)
|
||||
raise Exception("Dead job with interval %s" % interval)
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Classes and functions
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
def test_result():
|
||||
"""Test job submission and result retrieval"""
|
||||
jobs = bg.BackgroundJobManager()
|
||||
j = jobs.new(sleeper)
|
||||
j.join()
|
||||
nt.assert_equal(j.result['interval'], t_short)
|
||||
|
||||
|
||||
def test_flush():
|
||||
"""Test job control"""
|
||||
jobs = bg.BackgroundJobManager()
|
||||
j = jobs.new(sleeper)
|
||||
j.join()
|
||||
nt.assert_equal(len(jobs.completed), 1)
|
||||
nt.assert_equal(len(jobs.dead), 0)
|
||||
jobs.flush()
|
||||
nt.assert_equal(len(jobs.completed), 0)
|
||||
|
||||
|
||||
def test_dead():
|
||||
"""Test control of dead jobs"""
|
||||
jobs = bg.BackgroundJobManager()
|
||||
j = jobs.new(crasher)
|
||||
j.join()
|
||||
nt.assert_equal(len(jobs.completed), 0)
|
||||
nt.assert_equal(len(jobs.dead), 1)
|
||||
jobs.flush()
|
||||
nt.assert_equal(len(jobs.dead), 0)
|
||||
|
||||
|
||||
def test_longer():
|
||||
"""Test control of longer-running jobs"""
|
||||
jobs = bg.BackgroundJobManager()
|
||||
# Sleep for long enough for the following two checks to still report the
|
||||
# job as running, but not so long that it makes the test suite noticeably
|
||||
# slower.
|
||||
j = jobs.new(sleeper, 0.1)
|
||||
nt.assert_equal(len(jobs.running), 1)
|
||||
nt.assert_equal(len(jobs.completed), 0)
|
||||
j.join()
|
||||
nt.assert_equal(len(jobs.running), 0)
|
||||
nt.assert_equal(len(jobs.completed), 1)
|
@@ -0,0 +1,21 @@
|
||||
import nose.tools as nt
|
||||
|
||||
from yap_ipython.core.error import TryNext
|
||||
from yap_ipython.lib.clipboard import ClipboardEmpty
|
||||
from yap_ipython.testing.decorators import skip_if_no_x11
|
||||
|
||||
@skip_if_no_x11
|
||||
def test_clipboard_get():
|
||||
# Smoketest for clipboard access - we can't easily guarantee that the
|
||||
# clipboard is accessible and has something on it, but this tries to
|
||||
# exercise the relevant code anyway.
|
||||
try:
|
||||
a = get_ipython().hooks.clipboard_get()
|
||||
except ClipboardEmpty:
|
||||
# Nothing in clipboard to get
|
||||
pass
|
||||
except TryNext:
|
||||
# No clipboard access API available
|
||||
pass
|
||||
else:
|
||||
nt.assert_is_instance(a, str)
|
@@ -0,0 +1,34 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Test suite for the deepreload module."""
|
||||
|
||||
# Copyright (c) yap_ipython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
import os
|
||||
|
||||
import nose.tools as nt
|
||||
|
||||
from yap_ipython.utils.syspathcontext import prepended_to_syspath
|
||||
from yap_ipython.utils.tempdir import TemporaryDirectory
|
||||
from yap_ipython.lib.deepreload import reload as dreload
|
||||
|
||||
def test_deepreload():
|
||||
"Test that dreload does deep reloads and skips excluded modules."
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
with prepended_to_syspath(tmpdir):
|
||||
with open(os.path.join(tmpdir, 'A.py'), 'w') as f:
|
||||
f.write("class Object(object):\n pass\n")
|
||||
with open(os.path.join(tmpdir, 'B.py'), 'w') as f:
|
||||
f.write("import A\n")
|
||||
import A
|
||||
import B
|
||||
|
||||
# Test that A is not reloaded.
|
||||
obj = A.Object()
|
||||
dreload(B, exclude=['A'])
|
||||
nt.assert_true(isinstance(obj, A.Object))
|
||||
|
||||
# Test that A is reloaded.
|
||||
obj = A.Object()
|
||||
dreload(B)
|
||||
nt.assert_false(isinstance(obj, A.Object))
|
177
packages/python/yap_kernel/yap_ipython/lib/tests/test_display.py
Normal file
177
packages/python/yap_kernel/yap_ipython/lib/tests/test_display.py
Normal file
@@ -0,0 +1,177 @@
|
||||
"""Tests for yap_ipython.lib.display.
|
||||
|
||||
"""
|
||||
#-----------------------------------------------------------------------------
|
||||
# Copyright (c) 2012, the yap_ipython Development Team.
|
||||
#
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
#
|
||||
# The full license is in the file COPYING.txt, distributed with this software.
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Imports
|
||||
#-----------------------------------------------------------------------------
|
||||
from tempfile import NamedTemporaryFile, mkdtemp
|
||||
from os.path import split, join as pjoin, dirname
|
||||
|
||||
# Third-party imports
|
||||
import nose.tools as nt
|
||||
|
||||
# Our own imports
|
||||
from yap_ipython.lib import display
|
||||
from yap_ipython.testing.decorators import skipif_not_numpy
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Classes and functions
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
#--------------------------
|
||||
# FileLink tests
|
||||
#--------------------------
|
||||
|
||||
def test_instantiation_FileLink():
|
||||
"""FileLink: Test class can be instantiated"""
|
||||
fl = display.FileLink('example.txt')
|
||||
|
||||
def test_warning_on_non_existant_path_FileLink():
|
||||
"""FileLink: Calling _repr_html_ on non-existant files returns a warning
|
||||
"""
|
||||
fl = display.FileLink('example.txt')
|
||||
nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)'))
|
||||
|
||||
def test_existing_path_FileLink():
|
||||
"""FileLink: Calling _repr_html_ functions as expected on existing filepath
|
||||
"""
|
||||
tf = NamedTemporaryFile()
|
||||
fl = display.FileLink(tf.name)
|
||||
actual = fl._repr_html_()
|
||||
expected = "<a href='%s' target='_blank'>%s</a><br>" % (tf.name,tf.name)
|
||||
nt.assert_equal(actual,expected)
|
||||
|
||||
def test_existing_path_FileLink_repr():
|
||||
"""FileLink: Calling repr() functions as expected on existing filepath
|
||||
"""
|
||||
tf = NamedTemporaryFile()
|
||||
fl = display.FileLink(tf.name)
|
||||
actual = repr(fl)
|
||||
expected = tf.name
|
||||
nt.assert_equal(actual,expected)
|
||||
|
||||
def test_error_on_directory_to_FileLink():
|
||||
"""FileLink: Raises error when passed directory
|
||||
"""
|
||||
td = mkdtemp()
|
||||
nt.assert_raises(ValueError,display.FileLink,td)
|
||||
|
||||
#--------------------------
|
||||
# FileLinks tests
|
||||
#--------------------------
|
||||
|
||||
def test_instantiation_FileLinks():
|
||||
"""FileLinks: Test class can be instantiated
|
||||
"""
|
||||
fls = display.FileLinks('example')
|
||||
|
||||
def test_warning_on_non_existant_path_FileLinks():
|
||||
"""FileLinks: Calling _repr_html_ on non-existant files returns a warning
|
||||
"""
|
||||
fls = display.FileLinks('example')
|
||||
nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)'))
|
||||
|
||||
def test_existing_path_FileLinks():
|
||||
"""FileLinks: Calling _repr_html_ functions as expected on existing dir
|
||||
"""
|
||||
td = mkdtemp()
|
||||
tf1 = NamedTemporaryFile(dir=td)
|
||||
tf2 = NamedTemporaryFile(dir=td)
|
||||
fl = display.FileLinks(td)
|
||||
actual = fl._repr_html_()
|
||||
actual = actual.split('\n')
|
||||
actual.sort()
|
||||
# the links should always have forward slashes, even on windows, so replace
|
||||
# backslashes with forward slashes here
|
||||
expected = ["%s/<br>" % td,
|
||||
" <a href='%s' target='_blank'>%s</a><br>" %\
|
||||
(tf2.name.replace("\\","/"),split(tf2.name)[1]),
|
||||
" <a href='%s' target='_blank'>%s</a><br>" %\
|
||||
(tf1.name.replace("\\","/"),split(tf1.name)[1])]
|
||||
expected.sort()
|
||||
# We compare the sorted list of links here as that's more reliable
|
||||
nt.assert_equal(actual,expected)
|
||||
|
||||
def test_existing_path_FileLinks_alt_formatter():
|
||||
"""FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter
|
||||
"""
|
||||
td = mkdtemp()
|
||||
tf1 = NamedTemporaryFile(dir=td)
|
||||
tf2 = NamedTemporaryFile(dir=td)
|
||||
def fake_formatter(dirname,fnames,included_suffixes):
|
||||
return ["hello","world"]
|
||||
fl = display.FileLinks(td,notebook_display_formatter=fake_formatter)
|
||||
actual = fl._repr_html_()
|
||||
actual = actual.split('\n')
|
||||
actual.sort()
|
||||
expected = ["hello","world"]
|
||||
expected.sort()
|
||||
# We compare the sorted list of links here as that's more reliable
|
||||
nt.assert_equal(actual,expected)
|
||||
|
||||
def test_existing_path_FileLinks_repr():
|
||||
"""FileLinks: Calling repr() functions as expected on existing directory """
|
||||
td = mkdtemp()
|
||||
tf1 = NamedTemporaryFile(dir=td)
|
||||
tf2 = NamedTemporaryFile(dir=td)
|
||||
fl = display.FileLinks(td)
|
||||
actual = repr(fl)
|
||||
actual = actual.split('\n')
|
||||
actual.sort()
|
||||
expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]]
|
||||
expected.sort()
|
||||
# We compare the sorted list of links here as that's more reliable
|
||||
nt.assert_equal(actual,expected)
|
||||
|
||||
def test_existing_path_FileLinks_repr_alt_formatter():
|
||||
"""FileLinks: Calling repr() functions as expected w/ alt formatter
|
||||
"""
|
||||
td = mkdtemp()
|
||||
tf1 = NamedTemporaryFile(dir=td)
|
||||
tf2 = NamedTemporaryFile(dir=td)
|
||||
def fake_formatter(dirname,fnames,included_suffixes):
|
||||
return ["hello","world"]
|
||||
fl = display.FileLinks(td,terminal_display_formatter=fake_formatter)
|
||||
actual = repr(fl)
|
||||
actual = actual.split('\n')
|
||||
actual.sort()
|
||||
expected = ["hello","world"]
|
||||
expected.sort()
|
||||
# We compare the sorted list of links here as that's more reliable
|
||||
nt.assert_equal(actual,expected)
|
||||
|
||||
def test_error_on_file_to_FileLinks():
|
||||
"""FileLinks: Raises error when passed file
|
||||
"""
|
||||
td = mkdtemp()
|
||||
tf1 = NamedTemporaryFile(dir=td)
|
||||
nt.assert_raises(ValueError,display.FileLinks,tf1.name)
|
||||
|
||||
def test_recursive_FileLinks():
|
||||
"""FileLinks: Does not recurse when recursive=False
|
||||
"""
|
||||
td = mkdtemp()
|
||||
tf = NamedTemporaryFile(dir=td)
|
||||
subtd = mkdtemp(dir=td)
|
||||
subtf = NamedTemporaryFile(dir=subtd)
|
||||
fl = display.FileLinks(td)
|
||||
actual = str(fl)
|
||||
actual = actual.split('\n')
|
||||
nt.assert_equal(len(actual), 4, actual)
|
||||
fl = display.FileLinks(td, recursive=False)
|
||||
actual = str(fl)
|
||||
actual = actual.split('\n')
|
||||
nt.assert_equal(len(actual), 2, actual)
|
||||
|
||||
@skipif_not_numpy
|
||||
def test_audio_from_file():
|
||||
path = pjoin(dirname(__file__), 'test.wav')
|
||||
display.Audio(filename=path)
|
@@ -0,0 +1,34 @@
|
||||
"""Test installing editor hooks"""
|
||||
import sys
|
||||
from unittest import mock
|
||||
|
||||
import nose.tools as nt
|
||||
|
||||
from yap_ipython import get_ipython
|
||||
from yap_ipython.lib import editorhooks
|
||||
|
||||
def test_install_editor():
|
||||
called = []
|
||||
def fake_popen(*args, **kwargs):
|
||||
called.append({
|
||||
'args': args,
|
||||
'kwargs': kwargs,
|
||||
})
|
||||
return mock.MagicMock(**{'wait.return_value': 0})
|
||||
editorhooks.install_editor('foo -l {line} -f {filename}', wait=False)
|
||||
|
||||
with mock.patch('subprocess.Popen', fake_popen):
|
||||
get_ipython().hooks.editor('the file', 64)
|
||||
|
||||
nt.assert_equal(len(called), 1)
|
||||
args = called[0]['args']
|
||||
kwargs = called[0]['kwargs']
|
||||
|
||||
nt.assert_equal(kwargs, {'shell': True})
|
||||
|
||||
if sys.platform.startswith('win'):
|
||||
expected = ['foo', '-l', '64', '-f', 'the file']
|
||||
else:
|
||||
expected = "foo -l 64 -f 'the file'"
|
||||
cmd = args[0]
|
||||
nt.assert_equal(cmd, expected)
|
@@ -0,0 +1,11 @@
|
||||
# encoding: utf-8
|
||||
from yap_ipython.testing import decorators as dec
|
||||
|
||||
def test_import_backgroundjobs():
|
||||
from yap_ipython.lib import backgroundjobs
|
||||
|
||||
def test_import_deepreload():
|
||||
from yap_ipython.lib import deepreload
|
||||
|
||||
def test_import_demo():
|
||||
from yap_ipython.lib import demo
|
@@ -0,0 +1,134 @@
|
||||
# encoding: utf-8
|
||||
"""Tests for yap_ipython.utils.path.py"""
|
||||
|
||||
# Copyright (c) yap_ipython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
from unittest.mock import patch
|
||||
import nose.tools as nt
|
||||
|
||||
from yap_ipython.lib import latextools
|
||||
from yap_ipython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib
|
||||
from yap_ipython.utils.process import FindCmdError
|
||||
|
||||
|
||||
def test_latex_to_png_dvipng_fails_when_no_cmd():
|
||||
"""
|
||||
`latex_to_png_dvipng` should return None when there is no required command
|
||||
"""
|
||||
for command in ['latex', 'dvipng']:
|
||||
yield (check_latex_to_png_dvipng_fails_when_no_cmd, command)
|
||||
|
||||
|
||||
def check_latex_to_png_dvipng_fails_when_no_cmd(command):
|
||||
def mock_find_cmd(arg):
|
||||
if arg == command:
|
||||
raise FindCmdError
|
||||
|
||||
with patch.object(latextools, "find_cmd", mock_find_cmd):
|
||||
nt.assert_equal(latextools.latex_to_png_dvipng("whatever", True),
|
||||
None)
|
||||
|
||||
|
||||
@onlyif_cmds_exist('latex', 'dvipng')
|
||||
def test_latex_to_png_dvipng_runs():
|
||||
"""
|
||||
Test that latex_to_png_dvipng just runs without error.
|
||||
"""
|
||||
def mock_kpsewhich(filename):
|
||||
nt.assert_equal(filename, "breqn.sty")
|
||||
return None
|
||||
|
||||
for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]:
|
||||
yield (latextools.latex_to_png_dvipng, s, wrap)
|
||||
|
||||
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
|
||||
yield (latextools.latex_to_png_dvipng, s, wrap)
|
||||
|
||||
@skipif_not_matplotlib
|
||||
def test_latex_to_png_mpl_runs():
|
||||
"""
|
||||
Test that latex_to_png_mpl just runs without error.
|
||||
"""
|
||||
def mock_kpsewhich(filename):
|
||||
nt.assert_equal(filename, "breqn.sty")
|
||||
return None
|
||||
|
||||
for (s, wrap) in [("$x^2$", False), ("x^2", True)]:
|
||||
yield (latextools.latex_to_png_mpl, s, wrap)
|
||||
|
||||
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
|
||||
yield (latextools.latex_to_png_mpl, s, wrap)
|
||||
|
||||
@skipif_not_matplotlib
|
||||
def test_latex_to_html():
|
||||
img = latextools.latex_to_html("$x^2$")
|
||||
nt.assert_in("data:image/png;base64,iVBOR", img)
|
||||
|
||||
|
||||
def test_genelatex_no_wrap():
|
||||
"""
|
||||
Test genelatex with wrap=False.
|
||||
"""
|
||||
def mock_kpsewhich(filename):
|
||||
assert False, ("kpsewhich should not be called "
|
||||
"(called with {0})".format(filename))
|
||||
|
||||
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
|
||||
nt.assert_equal(
|
||||
'\n'.join(latextools.genelatex("body text", False)),
|
||||
r'''\documentclass{article}
|
||||
\usepackage{amsmath}
|
||||
\usepackage{amsthm}
|
||||
\usepackage{amssymb}
|
||||
\usepackage{bm}
|
||||
\pagestyle{empty}
|
||||
\begin{document}
|
||||
body text
|
||||
\end{document}''')
|
||||
|
||||
|
||||
def test_genelatex_wrap_with_breqn():
|
||||
"""
|
||||
Test genelatex with wrap=True for the case breqn.sty is installed.
|
||||
"""
|
||||
def mock_kpsewhich(filename):
|
||||
nt.assert_equal(filename, "breqn.sty")
|
||||
return "path/to/breqn.sty"
|
||||
|
||||
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
|
||||
nt.assert_equal(
|
||||
'\n'.join(latextools.genelatex("x^2", True)),
|
||||
r'''\documentclass{article}
|
||||
\usepackage{amsmath}
|
||||
\usepackage{amsthm}
|
||||
\usepackage{amssymb}
|
||||
\usepackage{bm}
|
||||
\usepackage{breqn}
|
||||
\pagestyle{empty}
|
||||
\begin{document}
|
||||
\begin{dmath*}
|
||||
x^2
|
||||
\end{dmath*}
|
||||
\end{document}''')
|
||||
|
||||
|
||||
def test_genelatex_wrap_without_breqn():
|
||||
"""
|
||||
Test genelatex with wrap=True for the case breqn.sty is not installed.
|
||||
"""
|
||||
def mock_kpsewhich(filename):
|
||||
nt.assert_equal(filename, "breqn.sty")
|
||||
return None
|
||||
|
||||
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
|
||||
nt.assert_equal(
|
||||
'\n'.join(latextools.genelatex("x^2", True)),
|
||||
r'''\documentclass{article}
|
||||
\usepackage{amsmath}
|
||||
\usepackage{amsthm}
|
||||
\usepackage{amssymb}
|
||||
\usepackage{bm}
|
||||
\pagestyle{empty}
|
||||
\begin{document}
|
||||
$$x^2$$
|
||||
\end{document}''')
|
129
packages/python/yap_kernel/yap_ipython/lib/tests/test_lexers.py
Normal file
129
packages/python/yap_kernel/yap_ipython/lib/tests/test_lexers.py
Normal file
@@ -0,0 +1,129 @@
|
||||
"""Test lexers module"""
|
||||
|
||||
# Copyright (c) yap_ipython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
from unittest import TestCase
|
||||
from pygments.token import Token
|
||||
from pygments.lexers import BashLexer
|
||||
|
||||
from .. import lexers
|
||||
|
||||
|
||||
class TestLexers(TestCase):
|
||||
"""Collection of lexers tests"""
|
||||
def setUp(self):
|
||||
self.lexer = lexers.IPythonLexer()
|
||||
self.bash_lexer = BashLexer()
|
||||
|
||||
def testIPythonLexer(self):
|
||||
fragment = '!echo $HOME\n'
|
||||
tokens = [
|
||||
(Token.Operator, '!'),
|
||||
]
|
||||
tokens.extend(self.bash_lexer.get_tokens(fragment[1:]))
|
||||
self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))
|
||||
|
||||
fragment_2 = '!' + fragment
|
||||
tokens_2 = [
|
||||
(Token.Operator, '!!'),
|
||||
] + tokens[1:]
|
||||
self.assertEqual(tokens_2, list(self.lexer.get_tokens(fragment_2)))
|
||||
|
||||
fragment_2 = '\t %%!\n' + fragment[1:]
|
||||
tokens_2 = [
|
||||
(Token.Text, '\t '),
|
||||
(Token.Operator, '%%!'),
|
||||
(Token.Text, '\n'),
|
||||
] + tokens[1:]
|
||||
self.assertEqual(tokens_2, list(self.lexer.get_tokens(fragment_2)))
|
||||
|
||||
fragment_2 = 'x = ' + fragment
|
||||
tokens_2 = [
|
||||
(Token.Name, 'x'),
|
||||
(Token.Text, ' '),
|
||||
(Token.Operator, '='),
|
||||
(Token.Text, ' '),
|
||||
] + tokens
|
||||
self.assertEqual(tokens_2, list(self.lexer.get_tokens(fragment_2)))
|
||||
|
||||
fragment_2 = 'x, = ' + fragment
|
||||
tokens_2 = [
|
||||
(Token.Name, 'x'),
|
||||
(Token.Punctuation, ','),
|
||||
(Token.Text, ' '),
|
||||
(Token.Operator, '='),
|
||||
(Token.Text, ' '),
|
||||
] + tokens
|
||||
self.assertEqual(tokens_2, list(self.lexer.get_tokens(fragment_2)))
|
||||
|
||||
fragment_2 = 'x, = %sx ' + fragment[1:]
|
||||
tokens_2 = [
|
||||
(Token.Name, 'x'),
|
||||
(Token.Punctuation, ','),
|
||||
(Token.Text, ' '),
|
||||
(Token.Operator, '='),
|
||||
(Token.Text, ' '),
|
||||
(Token.Operator, '%'),
|
||||
(Token.Keyword, 'sx'),
|
||||
(Token.Text, ' '),
|
||||
] + tokens[1:]
|
||||
self.assertEqual(tokens_2, list(self.lexer.get_tokens(fragment_2)))
|
||||
|
||||
fragment_2 = 'f = %R function () {}\n'
|
||||
tokens_2 = [
|
||||
(Token.Name, 'f'),
|
||||
(Token.Text, ' '),
|
||||
(Token.Operator, '='),
|
||||
(Token.Text, ' '),
|
||||
(Token.Operator, '%'),
|
||||
(Token.Keyword, 'R'),
|
||||
(Token.Text, ' function () {}\n'),
|
||||
]
|
||||
self.assertEqual(tokens_2, list(self.lexer.get_tokens(fragment_2)))
|
||||
|
||||
fragment_2 = '\t%%xyz\n$foo\n'
|
||||
tokens_2 = [
|
||||
(Token.Text, '\t'),
|
||||
(Token.Operator, '%%'),
|
||||
(Token.Keyword, 'xyz'),
|
||||
(Token.Text, '\n$foo\n'),
|
||||
]
|
||||
self.assertEqual(tokens_2, list(self.lexer.get_tokens(fragment_2)))
|
||||
|
||||
fragment_2 = '%system?\n'
|
||||
tokens_2 = [
|
||||
(Token.Operator, '%'),
|
||||
(Token.Keyword, 'system'),
|
||||
(Token.Operator, '?'),
|
||||
(Token.Text, '\n'),
|
||||
]
|
||||
self.assertEqual(tokens_2, list(self.lexer.get_tokens(fragment_2)))
|
||||
|
||||
fragment_2 = 'x != y\n'
|
||||
tokens_2 = [
|
||||
(Token.Name, 'x'),
|
||||
(Token.Text, ' '),
|
||||
(Token.Operator, '!='),
|
||||
(Token.Text, ' '),
|
||||
(Token.Name, 'y'),
|
||||
(Token.Text, '\n'),
|
||||
]
|
||||
self.assertEqual(tokens_2, list(self.lexer.get_tokens(fragment_2)))
|
||||
|
||||
fragment_2 = ' ?math.sin\n'
|
||||
tokens_2 = [
|
||||
(Token.Text, ' '),
|
||||
(Token.Operator, '?'),
|
||||
(Token.Text, 'math.sin'),
|
||||
(Token.Text, '\n'),
|
||||
]
|
||||
self.assertEqual(tokens_2, list(self.lexer.get_tokens(fragment_2)))
|
||||
|
||||
fragment = ' *int*?\n'
|
||||
tokens = [
|
||||
(Token.Text, ' *int*'),
|
||||
(Token.Operator, '?'),
|
||||
(Token.Text, '\n'),
|
||||
]
|
||||
self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))
|
423
packages/python/yap_kernel/yap_ipython/lib/tests/test_pretty.py
Normal file
423
packages/python/yap_kernel/yap_ipython/lib/tests/test_pretty.py
Normal file
@@ -0,0 +1,423 @@
|
||||
# coding: utf-8
|
||||
"""Tests for yap_ipython.lib.pretty."""
|
||||
|
||||
# Copyright (c) yap_ipython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
|
||||
from collections import Counter, defaultdict, deque, OrderedDict
|
||||
import types
|
||||
import string
|
||||
import unittest
|
||||
|
||||
import nose.tools as nt
|
||||
|
||||
from yap_ipython.lib import pretty
|
||||
from yap_ipython.testing.decorators import skip_without
|
||||
|
||||
from io import StringIO
|
||||
|
||||
|
||||
class MyList(object):
|
||||
def __init__(self, content):
|
||||
self.content = content
|
||||
def _repr_pretty_(self, p, cycle):
|
||||
if cycle:
|
||||
p.text("MyList(...)")
|
||||
else:
|
||||
with p.group(3, "MyList(", ")"):
|
||||
for (i, child) in enumerate(self.content):
|
||||
if i:
|
||||
p.text(",")
|
||||
p.breakable()
|
||||
else:
|
||||
p.breakable("")
|
||||
p.pretty(child)
|
||||
|
||||
|
||||
class MyDict(dict):
|
||||
def _repr_pretty_(self, p, cycle):
|
||||
p.text("MyDict(...)")
|
||||
|
||||
class MyObj(object):
|
||||
def somemethod(self):
|
||||
pass
|
||||
|
||||
|
||||
class Dummy1(object):
|
||||
def _repr_pretty_(self, p, cycle):
|
||||
p.text("Dummy1(...)")
|
||||
|
||||
class Dummy2(Dummy1):
|
||||
_repr_pretty_ = None
|
||||
|
||||
class NoModule(object):
|
||||
pass
|
||||
|
||||
NoModule.__module__ = None
|
||||
|
||||
class Breaking(object):
|
||||
def _repr_pretty_(self, p, cycle):
|
||||
with p.group(4,"TG: ",":"):
|
||||
p.text("Breaking(")
|
||||
p.break_()
|
||||
p.text(")")
|
||||
|
||||
class BreakingRepr(object):
|
||||
def __repr__(self):
|
||||
return "Breaking(\n)"
|
||||
|
||||
class BreakingReprParent(object):
|
||||
def _repr_pretty_(self, p, cycle):
|
||||
with p.group(4,"TG: ",":"):
|
||||
p.pretty(BreakingRepr())
|
||||
|
||||
class BadRepr(object):
|
||||
|
||||
def __repr__(self):
|
||||
return 1/0
|
||||
|
||||
|
||||
def test_indentation():
|
||||
"""Test correct indentation in groups"""
|
||||
count = 40
|
||||
gotoutput = pretty.pretty(MyList(range(count)))
|
||||
expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")"
|
||||
|
||||
nt.assert_equal(gotoutput, expectedoutput)
|
||||
|
||||
|
||||
def test_dispatch():
|
||||
"""
|
||||
Test correct dispatching: The _repr_pretty_ method for MyDict
|
||||
must be found before the registered printer for dict.
|
||||
"""
|
||||
gotoutput = pretty.pretty(MyDict())
|
||||
expectedoutput = "MyDict(...)"
|
||||
|
||||
nt.assert_equal(gotoutput, expectedoutput)
|
||||
|
||||
|
||||
def test_callability_checking():
|
||||
"""
|
||||
Test that the _repr_pretty_ method is tested for callability and skipped if
|
||||
not.
|
||||
"""
|
||||
gotoutput = pretty.pretty(Dummy2())
|
||||
expectedoutput = "Dummy1(...)"
|
||||
|
||||
nt.assert_equal(gotoutput, expectedoutput)
|
||||
|
||||
|
||||
def test_sets():
|
||||
"""
|
||||
Test that set and frozenset use Python 3 formatting.
|
||||
"""
|
||||
objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]),
|
||||
frozenset([1, 2]), set([-1, -2, -3])]
|
||||
expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}',
|
||||
'frozenset({1, 2})', '{-3, -2, -1}']
|
||||
for obj, expected_output in zip(objects, expected):
|
||||
got_output = pretty.pretty(obj)
|
||||
yield nt.assert_equal, got_output, expected_output
|
||||
|
||||
|
||||
@skip_without('xxlimited')
|
||||
def test_pprint_heap_allocated_type():
|
||||
"""
|
||||
Test that pprint works for heap allocated types.
|
||||
"""
|
||||
import xxlimited
|
||||
output = pretty.pretty(xxlimited.Null)
|
||||
nt.assert_equal(output, 'xxlimited.Null')
|
||||
|
||||
def test_pprint_nomod():
|
||||
"""
|
||||
Test that pprint works for classes with no __module__.
|
||||
"""
|
||||
output = pretty.pretty(NoModule)
|
||||
nt.assert_equal(output, 'NoModule')
|
||||
|
||||
def test_pprint_break():
|
||||
"""
|
||||
Test that p.break_ produces expected output
|
||||
"""
|
||||
output = pretty.pretty(Breaking())
|
||||
expected = "TG: Breaking(\n ):"
|
||||
nt.assert_equal(output, expected)
|
||||
|
||||
def test_pprint_break_repr():
|
||||
"""
|
||||
Test that p.break_ is used in repr
|
||||
"""
|
||||
output = pretty.pretty(BreakingReprParent())
|
||||
expected = "TG: Breaking(\n ):"
|
||||
nt.assert_equal(output, expected)
|
||||
|
||||
def test_bad_repr():
|
||||
"""Don't catch bad repr errors"""
|
||||
with nt.assert_raises(ZeroDivisionError):
|
||||
pretty.pretty(BadRepr())
|
||||
|
||||
class BadException(Exception):
|
||||
def __str__(self):
|
||||
return -1
|
||||
|
||||
class ReallyBadRepr(object):
|
||||
__module__ = 1
|
||||
@property
|
||||
def __class__(self):
|
||||
raise ValueError("I am horrible")
|
||||
|
||||
def __repr__(self):
|
||||
raise BadException()
|
||||
|
||||
def test_really_bad_repr():
|
||||
with nt.assert_raises(BadException):
|
||||
pretty.pretty(ReallyBadRepr())
|
||||
|
||||
|
||||
class SA(object):
|
||||
pass
|
||||
|
||||
class SB(SA):
|
||||
pass
|
||||
|
||||
class TestsPretty(unittest.TestCase):
|
||||
|
||||
def test_super_repr(self):
|
||||
# "<super: module_name.SA, None>"
|
||||
output = pretty.pretty(super(SA))
|
||||
self.assertRegex(output, r"<super: \S+.SA, None>")
|
||||
|
||||
# "<super: module_name.SA, <module_name.SB at 0x...>>"
|
||||
sb = SB()
|
||||
output = pretty.pretty(super(SA, sb))
|
||||
self.assertRegex(output, r"<super: \S+.SA,\s+<\S+.SB at 0x\S+>>")
|
||||
|
||||
|
||||
def test_long_list(self):
|
||||
lis = list(range(10000))
|
||||
p = pretty.pretty(lis)
|
||||
last2 = p.rsplit('\n', 2)[-2:]
|
||||
self.assertEqual(last2, [' 999,', ' ...]'])
|
||||
|
||||
def test_long_set(self):
|
||||
s = set(range(10000))
|
||||
p = pretty.pretty(s)
|
||||
last2 = p.rsplit('\n', 2)[-2:]
|
||||
self.assertEqual(last2, [' 999,', ' ...}'])
|
||||
|
||||
def test_long_tuple(self):
|
||||
tup = tuple(range(10000))
|
||||
p = pretty.pretty(tup)
|
||||
last2 = p.rsplit('\n', 2)[-2:]
|
||||
self.assertEqual(last2, [' 999,', ' ...)'])
|
||||
|
||||
def test_long_dict(self):
|
||||
d = { n:n for n in range(10000) }
|
||||
p = pretty.pretty(d)
|
||||
last2 = p.rsplit('\n', 2)[-2:]
|
||||
self.assertEqual(last2, [' 999: 999,', ' ...}'])
|
||||
|
||||
def test_unbound_method(self):
|
||||
output = pretty.pretty(MyObj.somemethod)
|
||||
self.assertIn('MyObj.somemethod', output)
|
||||
|
||||
|
||||
class MetaClass(type):
|
||||
def __new__(cls, name):
|
||||
return type.__new__(cls, name, (object,), {'name': name})
|
||||
|
||||
def __repr__(self):
|
||||
return "[CUSTOM REPR FOR CLASS %s]" % self.name
|
||||
|
||||
|
||||
ClassWithMeta = MetaClass('ClassWithMeta')
|
||||
|
||||
|
||||
def test_metaclass_repr():
|
||||
output = pretty.pretty(ClassWithMeta)
|
||||
nt.assert_equal(output, "[CUSTOM REPR FOR CLASS ClassWithMeta]")
|
||||
|
||||
|
||||
def test_unicode_repr():
|
||||
u = u"üniçodé"
|
||||
ustr = u
|
||||
|
||||
class C(object):
|
||||
def __repr__(self):
|
||||
return ustr
|
||||
|
||||
c = C()
|
||||
p = pretty.pretty(c)
|
||||
nt.assert_equal(p, u)
|
||||
p = pretty.pretty([c])
|
||||
nt.assert_equal(p, u'[%s]' % u)
|
||||
|
||||
|
||||
def test_basic_class():
|
||||
def type_pprint_wrapper(obj, p, cycle):
|
||||
if obj is MyObj:
|
||||
type_pprint_wrapper.called = True
|
||||
return pretty._type_pprint(obj, p, cycle)
|
||||
type_pprint_wrapper.called = False
|
||||
|
||||
stream = StringIO()
|
||||
printer = pretty.RepresentationPrinter(stream)
|
||||
printer.type_pprinters[type] = type_pprint_wrapper
|
||||
printer.pretty(MyObj)
|
||||
printer.flush()
|
||||
output = stream.getvalue()
|
||||
|
||||
nt.assert_equal(output, '%s.MyObj' % __name__)
|
||||
nt.assert_true(type_pprint_wrapper.called)
|
||||
|
||||
|
||||
def test_collections_defaultdict():
|
||||
# Create defaultdicts with cycles
|
||||
a = defaultdict()
|
||||
a.default_factory = a
|
||||
b = defaultdict(list)
|
||||
b['key'] = b
|
||||
|
||||
# Dictionary order cannot be relied on, test against single keys.
|
||||
cases = [
|
||||
(defaultdict(list), 'defaultdict(list, {})'),
|
||||
(defaultdict(list, {'key': '-' * 50}),
|
||||
"defaultdict(list,\n"
|
||||
" {'key': '--------------------------------------------------'})"),
|
||||
(a, 'defaultdict(defaultdict(...), {})'),
|
||||
(b, "defaultdict(list, {'key': defaultdict(...)})"),
|
||||
]
|
||||
for obj, expected in cases:
|
||||
nt.assert_equal(pretty.pretty(obj), expected)
|
||||
|
||||
|
||||
def test_collections_ordereddict():
|
||||
# Create OrderedDict with cycle
|
||||
a = OrderedDict()
|
||||
a['key'] = a
|
||||
|
||||
cases = [
|
||||
(OrderedDict(), 'OrderedDict()'),
|
||||
(OrderedDict((i, i) for i in range(1000, 1010)),
|
||||
'OrderedDict([(1000, 1000),\n'
|
||||
' (1001, 1001),\n'
|
||||
' (1002, 1002),\n'
|
||||
' (1003, 1003),\n'
|
||||
' (1004, 1004),\n'
|
||||
' (1005, 1005),\n'
|
||||
' (1006, 1006),\n'
|
||||
' (1007, 1007),\n'
|
||||
' (1008, 1008),\n'
|
||||
' (1009, 1009)])'),
|
||||
(a, "OrderedDict([('key', OrderedDict(...))])"),
|
||||
]
|
||||
for obj, expected in cases:
|
||||
nt.assert_equal(pretty.pretty(obj), expected)
|
||||
|
||||
|
||||
def test_collections_deque():
|
||||
# Create deque with cycle
|
||||
a = deque()
|
||||
a.append(a)
|
||||
|
||||
cases = [
|
||||
(deque(), 'deque([])'),
|
||||
(deque(i for i in range(1000, 1020)),
|
||||
'deque([1000,\n'
|
||||
' 1001,\n'
|
||||
' 1002,\n'
|
||||
' 1003,\n'
|
||||
' 1004,\n'
|
||||
' 1005,\n'
|
||||
' 1006,\n'
|
||||
' 1007,\n'
|
||||
' 1008,\n'
|
||||
' 1009,\n'
|
||||
' 1010,\n'
|
||||
' 1011,\n'
|
||||
' 1012,\n'
|
||||
' 1013,\n'
|
||||
' 1014,\n'
|
||||
' 1015,\n'
|
||||
' 1016,\n'
|
||||
' 1017,\n'
|
||||
' 1018,\n'
|
||||
' 1019])'),
|
||||
(a, 'deque([deque(...)])'),
|
||||
]
|
||||
for obj, expected in cases:
|
||||
nt.assert_equal(pretty.pretty(obj), expected)
|
||||
|
||||
def test_collections_counter():
|
||||
class MyCounter(Counter):
|
||||
pass
|
||||
cases = [
|
||||
(Counter(), 'Counter()'),
|
||||
(Counter(a=1), "Counter({'a': 1})"),
|
||||
(MyCounter(a=1), "MyCounter({'a': 1})"),
|
||||
]
|
||||
for obj, expected in cases:
|
||||
nt.assert_equal(pretty.pretty(obj), expected)
|
||||
|
||||
def test_mappingproxy():
|
||||
MP = types.MappingProxyType
|
||||
underlying_dict = {}
|
||||
mp_recursive = MP(underlying_dict)
|
||||
underlying_dict[2] = mp_recursive
|
||||
underlying_dict[3] = underlying_dict
|
||||
|
||||
cases = [
|
||||
(MP({}), "mappingproxy({})"),
|
||||
(MP({None: MP({})}), "mappingproxy({None: mappingproxy({})})"),
|
||||
(MP({k: k.upper() for k in string.ascii_lowercase}),
|
||||
"mappingproxy({'a': 'A',\n"
|
||||
" 'b': 'B',\n"
|
||||
" 'c': 'C',\n"
|
||||
" 'd': 'D',\n"
|
||||
" 'e': 'E',\n"
|
||||
" 'f': 'F',\n"
|
||||
" 'g': 'G',\n"
|
||||
" 'h': 'H',\n"
|
||||
" 'i': 'I',\n"
|
||||
" 'j': 'J',\n"
|
||||
" 'k': 'K',\n"
|
||||
" 'l': 'L',\n"
|
||||
" 'm': 'M',\n"
|
||||
" 'n': 'N',\n"
|
||||
" 'o': 'O',\n"
|
||||
" 'p': 'P',\n"
|
||||
" 'q': 'Q',\n"
|
||||
" 'r': 'R',\n"
|
||||
" 's': 'S',\n"
|
||||
" 't': 'T',\n"
|
||||
" 'u': 'U',\n"
|
||||
" 'v': 'V',\n"
|
||||
" 'w': 'W',\n"
|
||||
" 'x': 'X',\n"
|
||||
" 'y': 'Y',\n"
|
||||
" 'z': 'Z'})"),
|
||||
(mp_recursive, "mappingproxy({2: {...}, 3: {2: {...}, 3: {...}}})"),
|
||||
(underlying_dict,
|
||||
"{2: mappingproxy({2: {...}, 3: {...}}), 3: {...}}"),
|
||||
]
|
||||
for obj, expected in cases:
|
||||
nt.assert_equal(pretty.pretty(obj), expected)
|
||||
|
||||
def test_function_pretty():
|
||||
"Test pretty print of function"
|
||||
# posixpath is a pure python module, its interface is consistent
|
||||
# across Python distributions
|
||||
import posixpath
|
||||
nt.assert_equal(pretty.pretty(posixpath.join), '<function posixpath.join(a, *p)>')
|
||||
|
||||
# custom function
|
||||
def meaning_of_life(question=None):
|
||||
if question:
|
||||
return 42
|
||||
return "Don't panic"
|
||||
|
||||
nt.assert_in('meaning_of_life(question=None)', pretty.pretty(meaning_of_life))
|
||||
|
@@ -0,0 +1,26 @@
|
||||
# coding: utf-8
|
||||
from yap_ipython.lib import passwd
|
||||
from yap_ipython.lib.security import passwd_check, salt_len
|
||||
import nose.tools as nt
|
||||
|
||||
def test_passwd_structure():
|
||||
p = passwd('passphrase')
|
||||
algorithm, salt, hashed = p.split(':')
|
||||
nt.assert_equal(algorithm, 'sha1')
|
||||
nt.assert_equal(len(salt), salt_len)
|
||||
nt.assert_equal(len(hashed), 40)
|
||||
|
||||
def test_roundtrip():
|
||||
p = passwd('passphrase')
|
||||
nt.assert_equal(passwd_check(p, 'passphrase'), True)
|
||||
|
||||
def test_bad():
|
||||
p = passwd('passphrase')
|
||||
nt.assert_equal(passwd_check(p, p), False)
|
||||
nt.assert_equal(passwd_check(p, 'a:b:c:d'), False)
|
||||
nt.assert_equal(passwd_check(p, 'a:b'), False)
|
||||
|
||||
def test_passwd_check_unicode():
|
||||
# GH issue #4524
|
||||
phash = u'sha1:23862bc21dd3:7a415a95ae4580582e314072143d9c382c491e4f'
|
||||
assert passwd_check(phash, u"łe¶ŧ←↓→")
|
Reference in New Issue
Block a user