##// END OF EJS Templates
Merge remote-tracking branch 'public-upstream/master' into links-rebase...
Merge remote-tracking branch 'public-upstream/master' into links-rebase Conflicts: examples/Interactive Widgets/Widget Events.ipynb

File last commit:

r18427:7b2e741d
r19202:bab26ab3 merge
Show More
test_strings.py
165 lines | 6.2 KiB | text/x-python | PythonLexer
Jonathan Frederic
Add new filter tests
r11902 """
Module with tests for Strings
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the 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
#-----------------------------------------------------------------------------
Paul Ivanov
add missing import, properly use os.path.join
r11974 import os
Jonathan Frederic
Add new filter tests
r11902
from ...tests.base import TestsBase
Jonathan Frederic
Fixed line break syntax error
r11930 from ..strings import (wrap_text, html2text, add_anchor, strip_dollars,
MinRK
add posix_path filter...
r11972 strip_files_prefix, get_lines, comment_lines, ipython2python, posix_path,
Benjamin ABEL
Fix issue #5877 with tests...
r18427 add_prompts, prevent_list_blocks
MinRK
add posix_path filter...
r11972 )
Jonathan Frederic
Add new filter tests
r11902
#-----------------------------------------------------------------------------
# Class
#-----------------------------------------------------------------------------
class TestStrings(TestsBase):
def test_wrap_text(self):
Jonathan Frederic
Shrink header comments
r11934 """wrap_text test"""
Jonathan Frederic
Add new filter tests
r11902 test_text = """
Tush! never tell me; I take it much unkindly
That thou, Iago, who hast had my purse
As if the strings were thine, shouldst know of this.
"""
for length in [30,5,1]:
Thomas Kluyver
Remove ParametricTestCase from nbconvert tests
r12373 self._confirm_wrap_text(test_text, length)
Jonathan Frederic
Add new filter tests
r11902
Jonathan Frederic
Use decorator to enable test generation
r11935
Jonathan Frederic
Add new filter tests
r11902 def _confirm_wrap_text(self, text, length):
for line in wrap_text(text, length).split('\n'):
assert len(line) <= length
Jonathan Frederic
Use decorator to enable test generation
r11935
Jonathan Frederic
Add new filter tests
r11902 def test_html2text(self):
Jonathan Frederic
Shrink header comments
r11934 """html2text test"""
Jonathan Frederic
Add new filter tests
r11902 #TODO: More tests
Jonathan Frederic
Use IPython parameterized testing
r11936 self.assertEqual(html2text('<name>joe</name>'), 'joe')
Jonathan Frederic
Add new filter tests
r11902
def test_add_anchor(self):
Jonathan Frederic
Shrink header comments
r11934 """add_anchor test"""
Jonathan Frederic
Add new filter tests
r11902 #TODO: More tests
Jonathan Frederic
Moved add_anchor bytes-strings fix into add_anchor
r11927 results = add_anchor('<b>Hello World!</b>')
Jonathan Frederic
Fixed py3 / unicode compat
r11925 assert 'Hello World!' in results
assert 'id="' in results
assert 'class="anchor-link"' in results
assert '<b' in results
assert '</b>' in results
Jonathan Frederic
Add new filter tests
r11902
MinRK
don't do anything if add_anchor fails
r13844 def test_add_anchor_fail(self):
"""add_anchor does nothing when it fails"""
html = '<h1>Hello <br>World!</h1>'
results = add_anchor(html)
self.assertEqual(html, results)
Jonathan Frederic
Add new filter tests
r11902 def test_strip_dollars(self):
Jonathan Frederic
Shrink header comments
r11934 """strip_dollars test"""
Jonathan Frederic
Add new filter tests
r11902 tests = [
('', ''),
('$$', ''),
('$H$', 'H'),
('$He', 'He'),
('H$el', 'H$el'),
('Hell$', 'Hell'),
('Hello', 'Hello'),
('W$o$rld', 'W$o$rld')]
for test in tests:
Thomas Kluyver
Remove ParametricTestCase from nbconvert tests
r12373 self._try_strip_dollars(test[0], test[1])
Jonathan Frederic
Add new filter tests
r11902
def _try_strip_dollars(self, test, result):
Jonathan Frederic
Use IPython parameterized testing
r11936 self.assertEqual(strip_dollars(test), result)
Jonathan Frederic
Add new filter tests
r11902
def test_strip_files_prefix(self):
Jonathan Frederic
Shrink header comments
r11934 """strip_files_prefix test"""
Jonathan Frederic
Add new filter tests
r11902 tests = [
('', ''),
('/files', '/files'),
('test="/files"', 'test="/files"'),
('My files are in `files/`', 'My files are in `files/`'),
Peter Davis
`strip_files_prefix` now also strips markdown style links, `latex_base` updated to include filter
r13593 ('<a href="files/test.html">files/test.html</a>', '<a href="test.html">files/test.html</a>'),
MinRK
expand strip_files test coverage
r13866 ('<a href="/files/test.html">files/test.html</a>', '<a href="test.html">files/test.html</a>'),
("<a href='files/test.html'>files/test.html</a>", "<a href='test.html'>files/test.html</a>"),
('<img src="files/url/location.gif">', '<img src="url/location.gif">'),
('<img src="/files/url/location.gif">', '<img src="url/location.gif">'),
Peter Davis
`strip_files_prefix` now also strips markdown style links, `latex_base` updated to include filter
r13593 ('hello![caption]', 'hello![caption]'),
('hello![caption](/url/location.gif)', 'hello![caption](/url/location.gif)'),
('hello![caption](url/location.gif)', 'hello![caption](url/location.gif)'),
('hello![caption](url/location.gif)', 'hello![caption](url/location.gif)'),
('hello![caption](files/url/location.gif)', 'hello![caption](url/location.gif)'),
MinRK
expand strip_files test coverage
r13866 ('hello![caption](/files/url/location.gif)', 'hello![caption](url/location.gif)'),
('hello [text](/files/url/location.gif)', 'hello [text](url/location.gif)'),
('hello [text space](files/url/location.gif)', 'hello [text space](url/location.gif)'),
]
Jonathan Frederic
Add new filter tests
r11902 for test in tests:
Thomas Kluyver
Remove ParametricTestCase from nbconvert tests
r12373 self._try_files_prefix(test[0], test[1])
Jonathan Frederic
Add new filter tests
r11902
def _try_files_prefix(self, test, result):
Jonathan Frederic
Use IPython parameterized testing
r11936 self.assertEqual(strip_files_prefix(test), result)
Jonathan Frederic
Add new filter tests
r11902
def test_comment_lines(self):
Jonathan Frederic
Shrink header comments
r11934 """comment_lines test"""
Jonathan Frederic
Add new filter tests
r11902 for line in comment_lines('hello\nworld\n!').split('\n'):
assert line.startswith('# ')
for line in comment_lines('hello\nworld\n!', 'beep').split('\n'):
assert line.startswith('beep')
def test_get_lines(self):
Jonathan Frederic
Shrink header comments
r11934 """get_lines test"""
Jonathan Frederic
Add new filter tests
r11902 text = "hello\nworld\n!"
Jonathan Frederic
Use IPython parameterized testing
r11936 self.assertEqual(get_lines(text, start=1), "world\n!")
self.assertEqual(get_lines(text, end=2), "hello\nworld")
self.assertEqual(get_lines(text, start=2, end=5), "!")
self.assertEqual(get_lines(text, start=-2), "world\n!")
Jonathan Frederic
Add new filter tests
r11902
def test_ipython2python(self):
Jonathan Frederic
Shrink header comments
r11934 """ipython2python test"""
Jonathan Frederic
Add new filter tests
r11902 #TODO: More tests
Jonathan Frederic
Fixed py3 / unicode compat
r11925 results = ipython2python(u'%%pylab\nprint("Hello-World")').replace("u'", "'")
self.fuzzy_compare(results, u"get_ipython().run_cell_magic('pylab', '', 'print(\"Hello-World\")')",
Jonathan Frederic
More agressive fuzzy compare
r11923 ignore_spaces=True, ignore_newlines=True)
MinRK
add posix_path filter...
r11972
def test_posix_path(self):
Brian E. Granger
Adding tests for add_prompts....
r12706 """posix_path test"""
MinRK
add posix_path filter...
r11972 path_list = ['foo', 'bar']
expected = '/'.join(path_list)
Paul Ivanov
add missing import, properly use os.path.join
r11974 native = os.path.join(*path_list)
MinRK
add posix_path filter...
r11972 filtered = posix_path(native)
self.assertEqual(filtered, expected)
Brian E. Granger
Adding tests for add_prompts....
r12706
def test_add_prompts(self):
"""add_prompts test"""
text1 = """for i in range(10):\n i += 1\n print i"""
text2 = """>>> for i in range(10):\n... i += 1\n... print i"""
self.assertEqual(text2, add_prompts(text1))
Benjamin ABEL
Fix issue #5877 with tests...
r18427
def test_prevent_list_blocks(self):
"""prevent_list_blocks test"""
tests = [
('1. arabic point', '1\\. arabic point'),
('* bullet asterisk', '\\* bullet asterisk'),
('+ bullet Plus Sign', '\\+ bullet Plus Sign'),
('- bullet Hyphen-Minus', '\\- bullet Hyphen-Minus'),
(' 1. spaces + arabic point', ' 1\\. spaces + arabic point'),
]
for test in tests:
self.assertEqual(prevent_list_blocks(test[0]), test[1])