##// END OF EJS Templates
Use decorator to enable test generation
Jonathan Frederic -
Show More
@@ -1,85 +1,88 b''
1 1 """
2 2 Module with tests for ansi filters
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 from IPython.testing import decorators as dec
17 18 from IPython.utils.coloransi import TermColors
18 19
19 20 from ...tests.base import TestsBase
20 21 from ..ansi import strip_ansi, ansi2html, ansi2latex
21 22
22 23
23 24 #-----------------------------------------------------------------------------
24 25 # Class
25 26 #-----------------------------------------------------------------------------
26 27
27 28 class TestAnsi(TestsBase):
28 29 """Contains test functions for ansi.py"""
29 30
30
31 @dec.parametric
31 32 def test_strip_ansi(self):
32 33 """strip_ansi test"""
33 34 correct_outputs = {
34 35 '%s%s%s' % (TermColors.Green, TermColors.White, TermColors.Red) : '',
35 36 'hello%s' % TermColors.Blue: 'hello',
36 37 'he%s%sllo' % (TermColors.Yellow, TermColors.Cyan) : 'hello',
37 38 '%shello' % TermColors.Blue : 'hello',
38 39 '{0}h{0}e{0}l{0}l{0}o{0}'.format(TermColors.Red) : 'hello',
39 40 'hel%slo' % TermColors.Green : 'hello',
40 41 'hello' : 'hello'}
41 42
42 43 for inval, outval in correct_outputs.items():
43 44 yield self._try_strip_ansi, inval, outval
44 45
45 46
46 47 def _try_strip_ansi(self, inval, outval):
47 48 self.assert_equal(outval, strip_ansi(inval))
48 49
49 50
51 @dec.parametric
50 52 def test_ansi2html(self):
51 53 """ansi2html test"""
52 54 correct_outputs = {
53 55 '%s' % (TermColors.Red) : '<span class="ansired"></span>',
54 56 'hello%s' % TermColors.Blue: 'hello<span class="ansiblue"></span>',
55 57 'he%s%sllo' % (TermColors.Green, TermColors.Cyan) : 'he<span class="ansigreen"></span><span class="ansicyan">llo</span>',
56 58 '%shello' % TermColors.Yellow : '<span class="ansiyellow">hello</span>',
57 59 '{0}h{0}e{0}l{0}l{0}o{0}'.format(TermColors.White) : '<span class="ansigrey">h</span><span class="ansigrey">e</span><span class="ansigrey">l</span><span class="ansigrey">l</span><span class="ansigrey">o</span><span class="ansigrey"></span>',
58 60 'hel%slo' % TermColors.Green : 'hel<span class="ansigreen">lo</span>',
59 61 'hello' : 'hello'}
60 62
61 63 for inval, outval in correct_outputs.items():
62 64 yield self._try_ansi2html, inval, outval
63 65
64 66
65 67 def _try_ansi2html(self, inval, outval):
66 68 self.fuzzy_compare(outval, ansi2html(inval))
67 69
68 70
71 @dec.parametric
69 72 def test_ansi2latex(self):
70 73 """ansi2latex test"""
71 74 correct_outputs = {
72 75 '%s' % (TermColors.Red) : r'\red{}',
73 76 'hello%s' % TermColors.Blue: r'hello\blue{}',
74 77 'he%s%sllo' % (TermColors.Green, TermColors.Cyan) : r'he\green{}\cyan{llo}',
75 78 '%shello' % TermColors.Yellow : r'\yellow{hello}',
76 79 '{0}h{0}e{0}l{0}l{0}o{0}'.format(TermColors.White) : r'\white{h}\white{e}\white{l}\white{l}\white{o}\white{}',
77 80 'hel%slo' % TermColors.Green : r'hel\green{lo}',
78 81 'hello' : 'hello'}
79 82
80 83 for inval, outval in correct_outputs.items():
81 84 yield self._try_ansi2latex, inval, outval
82 85
83 86
84 87 def _try_ansi2latex(self, inval, outval):
85 88 self.fuzzy_compare(outval, ansi2latex(inval), case_sensitive=True)
@@ -1,66 +1,69 b''
1 1 """
2 2 Module with tests for Highlight
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 from IPython.testing import decorators as dec
17 18
18 19 from ...tests.base import TestsBase
19 20 from ..highlight import highlight2html, highlight2latex
20 21
21 22
22 23 #-----------------------------------------------------------------------------
23 24 # Class
24 25 #-----------------------------------------------------------------------------
25 26
26 27 class TestHighlight(TestsBase):
27 28 """Contains test functions for highlight.py"""
28 29
29 30 #Hello world test, magics test, blank string test
30 31 tests = [
31 32 """
32 33 #Hello World Example
33 34
34 35 def say(text):
35 36 print(text)
36 37
37 38 say('Hello World!')
38 39 """,
39 40 """
40 41 %%pylab
41 42 plot(x,y, 'r')
42 43 """
43 44 ]
44 45
45 46 tokens = [
46 47 ['Hello World Example', 'say', 'text', 'print', 'def'],
47 48 ['pylab', 'plot']]
48 49
49 50
51 @dec.parametric
50 52 def test_highlight2html(self):
51 53 """highlight2html test"""
52 54 for index, test in enumerate(self.tests):
53 55 yield self._try_highlight, highlight2html, test, self.tokens[index]
54 56
55 57
58 @dec.parametric
56 59 def test_highlight2latex(self):
57 60 """highlight2latex test"""
58 61 for index, test in enumerate(self.tests):
59 62 yield self._try_highlight, highlight2latex, test, self.tokens[index]
60 63
61 64
62 65 def _try_highlight(self, method, test, tokens):
63 66 """Try highlighting source, look for key tokens"""
64 67 results = method(test)
65 68 for token in tokens:
66 69 assert token in results
@@ -1,66 +1,69 b''
1 1 """
2 2 Module with tests for Latex
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 from IPython.testing import decorators as dec
17 18
18 19 from ...tests.base import TestsBase
19 20 from ..latex import escape_latex, strip_math_space
20 21
21 22
22 23 #-----------------------------------------------------------------------------
23 24 # Class
24 25 #-----------------------------------------------------------------------------
25 26
26 27 class TestLatex(TestsBase):
27 28
28 29
30 @dec.parametric
29 31 def test_escape_latex(self):
30 32 """escape_latex test"""
31 33 tests = [
32 34 (r'How are \you doing today?', r'How are \textbackslashyou doing today?'),
33 35 (r'\escapechar=`\A\catcode`\|=0 |string|foo', r'\textbackslashescapechar=`\textbackslashA\textbackslashcatcode`\textbackslash|=0 |string|foo'),
34 36 (r'# $ % & ~ _ ^ \ { }',r'\# \$ \% \& \~{} \_ \^{} \textbackslash \{ \}'),
35 37 ('','')]
36 38
37 39 for test in tests:
38 40 yield self._try_escape_latex, test[0], test[1]
39 41
40 42
41 43 def _try_escape_latex(self, test, result):
42 44 """Try to remove latex from string"""
43 45 self.assert_equal(escape_latex(test), result)
44 46
45 47
48 @dec.parametric
46 49 def test_strip_math_space(self):
47 50 """strip_math_space test"""
48 51 tests = [
49 52 ('$e$','$e$'),
50 53 ('$ e $','$e$'),
51 54 ('xxx$e^i$yyy','xxx$e^i$yyy'),
52 55 ('xxx$ e^i $yyy','xxx$e^i$yyy'),
53 56 ('xxx$e^i $yyy','xxx$e^i$yyy'),
54 57 ('xxx$ e^i$yyy','xxx$e^i$yyy'),
55 58 ('\$ e $ e $','\$ e $e$'),
56 59 ('','')]
57 60
58 61 for test in tests:
59 62 yield self._try_strip_math_space, test[0], test[1]
60 63
61 64
62 65 def _try_strip_math_space(self, test, result):
63 66 """
64 67 Try to remove spaces between dollar symbols and contents correctly
65 68 """
66 69 self.assert_equal(strip_math_space(test), result)
@@ -1,93 +1,96 b''
1 1
2 2 """
3 3 Module with tests for Markdown
4 4 """
5 5
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (c) 2013, the IPython Development Team.
8 8 #
9 9 # Distributed under the terms of the Modified BSD License.
10 10 #
11 11 # The full license is in the file COPYING.txt, distributed with this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17
18 18 from copy import copy
19 19
20 from IPython.testing.decorators import onlyif_cmds_exist
21 20 from IPython.utils.py3compat import string_types
21 from IPython.testing import decorators as dec
22 22
23 23 from ...tests.base import TestsBase
24 24 from ..markdown import markdown2latex, markdown2html, markdown2rst
25 25
26 26
27 27 #-----------------------------------------------------------------------------
28 28 # Class
29 29 #-----------------------------------------------------------------------------
30 30
31 31 class TestMarkdown(TestsBase):
32 32
33 33 tests = [
34 34 '*test',
35 35 '**test',
36 36 '*test*',
37 37 '_test_',
38 38 '__test__',
39 39 '__*test*__',
40 40 '**test**',
41 41 '#test',
42 42 '##test',
43 43 'test\n----',
44 44 'test [link](https://google.com/)']
45 45
46 46 tokens = [
47 47 '*test',
48 48 '**test',
49 49 'test',
50 50 'test',
51 51 'test',
52 52 'test',
53 53 'test',
54 54 'test',
55 55 'test',
56 56 'test',
57 57 ('test', 'https://google.com/')]
58 58
59 59
60 @onlyif_cmds_exist('pandoc')
60 @dec.onlyif_cmds_exist('pandoc')
61 @dec.parametric
61 62 def test_markdown2latex(self):
62 63 """markdown2latex test"""
63 64 for index, test in enumerate(self.tests):
64 65 yield self._try_markdown, markdown2latex, test, self.tokens[index]
65 66
66 67
67 @onlyif_cmds_exist('pandoc')
68 @dec.onlyif_cmds_exist('pandoc')
69 @dec.parametric
68 70 def test_markdown2html(self):
69 71 """markdown2html test"""
70 72 for index, test in enumerate(self.tests):
71 73 yield self._try_markdown, markdown2html, test, self.tokens[index]
72 74
73 75
74 @onlyif_cmds_exist('pandoc')
76 @dec.onlyif_cmds_exist('pandoc')
77 @dec.parametric
75 78 def test_markdown2rst(self):
76 79 """markdown2rst test"""
77 80
78 81 #Modify token array for rst, escape asterik
79 82 tokens = copy(self.tokens)
80 83 tokens[0] = r'\*test'
81 84 tokens[1] = r'\*\*test'
82 85
83 86 for index, test in enumerate(self.tests):
84 87 yield self._try_markdown, markdown2rst, test, tokens[index]
85 88
86 89
87 90 def _try_markdown(self, method, test, tokens):
88 91 results = method(test)
89 92 if isinstance(tokens, string_types):
90 93 assert tokens in results
91 94 else:
92 95 for token in tokens:
93 96 assert token in results
@@ -1,116 +1,122 b''
1 1 """
2 2 Module with tests for Strings
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 from IPython.testing import decorators as dec
17 18 from ...tests.base import TestsBase
18 19 from ..strings import (wrap_text, html2text, add_anchor, strip_dollars,
19 20 strip_files_prefix, get_lines, comment_lines, ipython2python)
20 21
21 22
22 23 #-----------------------------------------------------------------------------
23 24 # Class
24 25 #-----------------------------------------------------------------------------
25 26
26 27 class TestStrings(TestsBase):
27 28
29 @dec.parametric
28 30 def test_wrap_text(self):
29 31 """wrap_text test"""
30 32 test_text = """
31 33 Tush! never tell me; I take it much unkindly
32 34 That thou, Iago, who hast had my purse
33 35 As if the strings were thine, shouldst know of this.
34 36 """
35 37 for length in [30,5,1]:
36 38 yield self._confirm_wrap_text, test_text, length
37 39
40
38 41 def _confirm_wrap_text(self, text, length):
39 42 for line in wrap_text(text, length).split('\n'):
40 43 assert len(line) <= length
41 44
45
42 46 def test_html2text(self):
43 47 """html2text test"""
44 48 #TODO: More tests
45 49 self.assert_equal(html2text('<name>joe</name>'), 'joe')
46 50
47 51
48 52 def test_add_anchor(self):
49 53 """add_anchor test"""
50 54 #TODO: More tests
51 55 results = add_anchor('<b>Hello World!</b>')
52 56 assert 'Hello World!' in results
53 57 assert 'id="' in results
54 58 assert 'class="anchor-link"' in results
55 59 assert '<b' in results
56 60 assert '</b>' in results
57 61
58 62
63 @dec.parametric
59 64 def test_strip_dollars(self):
60 65 """strip_dollars test"""
61 66 tests = [
62 67 ('', ''),
63 68 ('$$', ''),
64 69 ('$H$', 'H'),
65 70 ('$He', 'He'),
66 71 ('H$el', 'H$el'),
67 72 ('Hell$', 'Hell'),
68 73 ('Hello', 'Hello'),
69 74 ('W$o$rld', 'W$o$rld')]
70 75 for test in tests:
71 76 yield self._try_strip_dollars, test[0], test[1]
72 77
73 78
74 79 def _try_strip_dollars(self, test, result):
75 80 self.assert_equal(strip_dollars(test), result)
76 81
77 82
83 @dec.parametric
78 84 def test_strip_files_prefix(self):
79 85 """strip_files_prefix test"""
80 86 tests = [
81 87 ('', ''),
82 88 ('/files', '/files'),
83 89 ('test="/files"', 'test="/files"'),
84 90 ('My files are in `files/`', 'My files are in `files/`'),
85 91 ('<a href="files/test.html">files/test.html</a>', '<a href="test.html">files/test.html</a>')]
86 92 for test in tests:
87 93 yield self._try_files_prefix, test[0], test[1]
88 94
89 95
90 96 def _try_files_prefix(self, test, result):
91 97 self.assert_equal(strip_files_prefix(test), result)
92 98
93 99
94 100 def test_comment_lines(self):
95 101 """comment_lines test"""
96 102 for line in comment_lines('hello\nworld\n!').split('\n'):
97 103 assert line.startswith('# ')
98 104 for line in comment_lines('hello\nworld\n!', 'beep').split('\n'):
99 105 assert line.startswith('beep')
100 106
101 107
102 108 def test_get_lines(self):
103 109 """get_lines test"""
104 110 text = "hello\nworld\n!"
105 111 self.assert_equal(get_lines(text, start=1), "world\n!")
106 112 self.assert_equal(get_lines(text, end=2), "hello\nworld")
107 113 self.assert_equal(get_lines(text, start=2, end=5), "!")
108 114 self.assert_equal(get_lines(text, start=-2), "world\n!")
109 115
110 116
111 117 def test_ipython2python(self):
112 118 """ipython2python test"""
113 119 #TODO: More tests
114 120 results = ipython2python(u'%%pylab\nprint("Hello-World")').replace("u'", "'")
115 121 self.fuzzy_compare(results, u"get_ipython().run_cell_magic('pylab', '', 'print(\"Hello-World\")')",
116 122 ignore_spaces=True, ignore_newlines=True)
General Comments 0
You need to be logged in to leave comments. Login now