7ef28150de2a0fe22159ed85a62ba91bf6b0f513
[obnox/wiki2beamer.git] / tests / test_wiki2beamer.py
1 #!/usr/bin/env python
2
3 #     This file is part of wiki2beamer.
4 # wiki2beamer is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # wiki2beamer is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with wiki2beamer.  If not, see <http://www.gnu.org/licenses/>.
16
17 import sys
18 import unittest
19 import re
20 import random
21
22 sys.path.append('../code')
23 from wiki2beamer import *
24
25 class TestBasics(unittest.TestCase):
26
27     def test_join_lines_standard(self):
28         lines = ['', 'foo%', 'bar']
29         joined = joinLines(lines)
30         self.assertEqual(len(joined), 2)
31         self.assertEqual(joined[0], lines[0])
32         self.assertEqual(joined[1], 'foobar')
33
34     def test_join_lines_shortlines(self):
35         lines = ['%', '%']
36         joined = joinLines(lines)
37         self.assertEqual(len(joined), 0)
38
39     def test_escape_resub(self):
40         string = r"foo \1 bar"
41         expected = r"foo \\1 bar"
42         out = escape_resub(string)
43         self.assertEqual(expected,out)
44
45     def test_escape_resub2(self):
46         instr = "abc"
47         substr = r'a\1'
48         p = re.compile(".*(b).*")
49         out = p.sub(escape_resub(substr), instr)
50         self.assertEqual(out, substr)
51
52
53 class TestTransform(unittest.TestCase):
54     def setUp(self):
55         self.state = w2bstate()
56         return
57
58     def tearDown(self):
59         self.state = None
60         return
61
62     def test_substitutions(self):
63         self.assertEqual(transform('foo --> bar', self.state), r'foo $\rightarrow$ bar')
64         self.assertEqual(transform('foo <-- bar', self.state), r'foo $\leftarrow$ bar')
65
66     def test_section(self):
67         self.assertEqual(transform('== foo ==', self.state), '\n\\section{foo}\n\n')
68     
69     def test_subsection(self):
70         self.assertEqual(transform('=== foo ===', self.state), '\n\\subsection{foo}\n\n')
71
72     def test_footnote(self):
73         self.assertEqual(transform('(((foo)))', self.state), '\\footnote{foo}')
74
75     def test_columns(self):
76         self.assertEqual(transform('[[[6cm]]]', self.state), '\\column{6cm}')
77
78     def test_typewriter(self):
79         input_expected = [('@TEST@', '\\texttt{TEST}'),
80                           ('@TEST', '@TEST'),
81                           ('TEST@', 'TEST@'),
82                           ('@TEST@TEST@', '\\texttt{TEST}TEST@'),
83                           ('@TEST@ test @TEST@',
84                               '\\texttt{TEST} test \\texttt{TEST}'),
85                           ('\@TEST\@', '@TEST@'),
86                           ('\\TEST', '\\TEST'),
87                           ('@TEST\@TEST@', '\\texttt{TEST@TEST}'),
88                           ('\@TEST @TEST@ TEST\@',
89                               '@TEST \\texttt{TEST} TEST@')]
90         for input_, expected in input_expected:
91             self.assertEqual(transform(input_, self.state), expected)
92
93     def test_alert(self):
94         input_expected = [('!TEST!', '\\alert{TEST}'),
95                           ('!TEST', '!TEST'),
96                           ('TEST!', 'TEST!'),
97                           ('!TEST!TEST!', '\\alert{TEST}TEST!'),
98                           ('!TEST! test !TEST!',
99                               '\\alert{TEST} test \\alert{TEST}'),
100                           ('\!TEST\!', '!TEST!'),
101                           ('\\TEST', '\\TEST'),
102                           ('!TEST\!TEST!', '\\alert{TEST!TEST}'),
103                           ('\!TEST !TEST! TEST\!',
104                               '!TEST \\alert{TEST} TEST!')]
105         for input_, expected in input_expected:
106             self.assertEqual(transform(input_, self.state), expected)
107
108     def test_vspace(self):
109         self.assertEqual(transform('--3em--', self.state), '\n\\vspace{3em}\n')
110         self.assertEqual(transform('--3em--foo', self.state), '--3em--foo')
111         self.assertEqual(transform(' --3em-- ', self.state), '\n\\vspace{3em}\n')
112
113     def test_vspacestar(self):
114         self.assertEqual(transform('--*3em--', self.state), '\n\\vspace*{3em}\n')
115         self.assertEqual(transform('--*3em--foo', self.state), '--*3em--foo')
116         self.assertEqual(transform(' --*3em-- ', self.state), '\n\\vspace*{3em}\n')
117
118     def test_uncover(self):
119         self.assertEqual(transform('+<2-> {foo}', self.state), '\uncover<2->{foo}')
120         self.assertEqual(transform(' +<2->{\nfoo', self.state), ' \uncover<2->{\nfoo')
121
122     def test_only(self):
123         self.assertEqual(transform('-<2-> {foo}', self.state), '\only<2->{foo}')
124         self.assertEqual(transform(' -<2->{\nfoo', self.state), ' \only<2->{\nfoo')
125     
126     def test_uncover_intext(self):
127         self.assertEqual(transform('foo +<2->{moo} bar', self.state), 'foo \uncover<2->{moo} bar')
128         self.assertEqual(transform(\
129             'foo +<2-3>  {\\begin{enumerate} \\end{enumerate}}', self.state),\
130             'foo \\uncover<2-3>{\\begin{enumerate} \\end{enumerate}}')
131
132
133 class TestExpandCode(unittest.TestCase):
134     def test_search_escape_sequences_basic(self):
135         code = "System435.out.println(\"foo\");123System.ou12t.println234(\"foo\");System.23out.23456println(\"foo\");S237yst28em.out.pr18intln(\"foo\");"
136         (open, close) = expand_code_search_escape_sequences(code)
137         self.assertEqual(code.find(open), -1)
138         self.assertEqual(code.find(close), -1)
139
140     def test_search_escape_sequences_short(self):
141         code = "12"
142         (open, close) = expand_code_search_escape_sequences(code)
143         self.assertEqual(code.find(open), -1)
144         self.assertEqual(code.find(close), -1)
145
146     def test_search_escape_sequences_veryshort(self):
147         code = ""
148         (open, close) = expand_code_search_escape_sequences(code)
149         self.assertEqual(code.find(open), -1)
150         self.assertEqual(code.find(close), -1)
151
152     def test_search_escape_sequences_large(self):
153         code = []
154         for i in xrange(0, 10000):
155             code.append(chr(random.randint(48,57)))
156         code = ''.join(code)
157
158         (open, close) = expand_code_search_escape_sequences(code)
159         self.assertEqual(code.find(open), -1)
160         self.assertEqual(code.find(close), -1)
161
162     def test_expand_code_tokenize_anims(self):
163         items = ['1', '2', '3', '-', ',', '[', ']', '<', '>', 'a', 'b', 'c', 'd', 'e', '}', '{']
164         code = []
165         for i in xrange(0, 100):
166             code.extend(items)
167             random.shuffle(items)
168         
169         out = expand_code_tokenize_anims(''.join(code))
170         self.assert_(len(out[0])>0)
171         self.assert_(len(out[1])>0)
172         for item in out[0]: #anims
173             self.assert_(item.startswith('[') and item.endswith(']'))
174         for item in out[1]: #non-anims
175             self.assert_(not (item.startswith('[') and item.endswith(']')))
176     
177     def test_expand_code_tokenize_anims_empty(self):
178         out = expand_code_tokenize_anims('')
179         self.assertEqual(out[0], [])
180         self.assertEqual(out[1], [''])
181
182 class TestConvert2Beamer(unittest.TestCase):
183     def setUp(self):
184         return
185     
186     def tearDown(self):
187         return
188
189     def test_nowiki(self):
190         lines = ['<[nowiki ]%',\
191                 '==== foo ====',\
192                 '[ nowiki]>moo']
193         expected = ['\n', '%',\
194                 '==== foo ====',\
195                 'moo',\
196                 '']
197         out = convert2beamer(lines)
198         self.assertEqual(out, expected)
199      
200     def test_not_nowiki(self):
201         lines = [' <[nowiki]', '== foo ==']
202         expected = ['\n', ' <[nowiki]', '\n\section{foo}\n\n', '']
203         out = convert2beamer(lines)
204         self.assertEqual(out, expected)
205     
206     def test_frame_open_close(self):
207         lines = ['==== foo ====']
208         expected = ['\n', '\\begin{frame}\n \\frametitle{foo}\n  \n', '', '  \n\\end{frame}\n']
209         out = convert2beamer(lines)
210         self.assertEqual(out, expected)
211     def test_frame_open_close_again(self):
212         lines = ['==== foo ====', '==== bar ====']
213         expected = ['\n', '\\begin{frame}\n \\frametitle{foo}\n  \n', '  \n\\end{frame}\n\\begin{frame}\n \\frametitle{bar}\n  \n', '', '  \n\\end{frame}\n']
214         out = convert2beamer(lines)
215         self.assertEqual(out, expected)
216
217     def test_frame_close_detect(self):
218         lines = ['==== foo ====', '[ frame ]>', '==== bar ====']
219         expected = ['\n', '\\begin{frame}\n \\frametitle{foo}\n  \n', '\\end{ frame }', '\\begin{frame}\n \\frametitle{bar}\n  \n', '', '  \n\\end{frame}\n']
220         out = convert2beamer(lines)
221         self.assertEqual(out, expected)
222
223
224     def test_itemize(self):
225         lines = ['* foo', '* bar', '** foobar']
226         expected = ['\n', '\\begin{itemize}\n  \\item foo', '  \\item bar', '\\begin{itemize}\n  \\item foobar', '\\end{itemize}\n\\end{itemize}\n']
227         out = convert2beamer(lines)
228         self.assertEqual(out, expected)
229
230     def test_enumerate(self):
231         lines = ['# one', '# two', '## onetwo']
232         expected = ['\n', '\\begin{enumerate}\n  \\item one', '  \\item two', '\\begin{enumerate}\n  \\item onetwo', '\\end{enumerate}\n\\end{enumerate}\n']
233         out = convert2beamer(lines)
234         self.assertEqual(out,expected)
235
236     def test_itemenum(self):
237         lines = ['# one', '#* onefoo', '#* onebar', '## oneone', '#*# onefooone']
238         expected = ['\n', '\\begin{enumerate}\n  \\item one', '\\begin{itemize}\n  \\item onefoo', '  \\item onebar', '\\end{itemize}\n\\begin{enumerate}\n  \\item oneone', '\\end{enumerate}\n\\begin{itemize}\n\\begin{enumerate}\n  \\item onefooone', '\\end{enumerate}\n\\end{itemize}\n\\end{enumerate}\n']
239         out = convert2beamer(lines)
240         self.assertEqual(out, expected)
241
242     def test_header(self):
243         lines = ['==== foo ====', '@FRAMEHEADER=bar', '==== bar ====']
244         expected = ['\n', '\\begin{frame}\n \\frametitle{foo}\n  \n', '', '  \n\\end{frame}\n\\begin{frame}\n \\frametitle{bar}\n bar \n', '', '  \n\\end{frame}\n']
245         out = convert2beamer(lines)
246         self.assertEqual(out,expected)
247
248     def test_footer(self):
249         lines = ['==== foo ====', '@FRAMEFOOTER=bar', '==== bar ====']
250         expected = ['\n', '\\begin{frame}\n \\frametitle{foo}\n  \n', '', '  \n\\end{frame}\n\\begin{frame}\n \\frametitle{bar}\n  \n', '', ' bar \n\\end{frame}\n']
251         out = convert2beamer(lines)
252         self.assertEqual(out,expected)
253
254     def test_subexp_footer(self):
255         lines = ['==== foo ====', '@FRAMEFOOTER=\\huge bar 3', '==== bar ====']
256         expected = ['\n', '\\begin{frame}\n \\frametitle{foo}\n  \n', '', '  \n\\end{frame}\n\\begin{frame}\n \\frametitle{bar}\n  \n', '', ' \\huge bar 3 \n\\end{frame}\n']
257         out = convert2beamer(lines)
258         self.assertEqual(out,expected)
259
260     def test_section_footer(self):
261         lines = ['==== foo ====', '@FRAMEFOOTER=bar', '== foosec ==', '==== bar ====']
262         expected = ['\n', '\\begin{frame}\n \\frametitle{foo}\n  \n', '', '  \n\\end{frame}\n\n\\section{foosec}\n\n', '\\begin{frame}\n \\frametitle{bar}\n  \n', '', ' bar \n\\end{frame}\n']
263         out = convert2beamer(lines)
264         self.assertEqual(out,expected)
265     
266     def test_itemizeclose_column(self):
267         lines = ['* foo', '[[[6cm]]]']
268         expected = ['\n', '\\begin{itemize}\n  \\item foo', '\\end{itemize}\n\\column{6cm}', '']
269         out = convert2beamer(lines)
270         self.assertEqual(out,expected)
271
272     def test_fragile(self):
273         lines = ['==== foo ====[fragile]', 'foo']
274         expected = ['\n',
275                     '\\begin{frame}[fragile]\n \\frametitle{foo}\n  \n',
276                     'foo',
277                     '',
278                     '  \n\\end{frame}\n']
279         out = convert2beamer(lines)
280         self.assertEqual(out, expected)
281
282 class TestFileInclusion(unittest.TestCase):
283     def setUp(self):
284         files = {'test_file': ['test file content'],
285                  'test_file2':['content from test_file2',
286                                '>>>test_file<<<'],
287                  'test_file3':['content from test_file3',
288                                '<[nowiki]',
289                                '>>>test_file<<<',
290                                '[nowiki]>'],
291                  'test_file_loop':['test_file_loop content',
292                                '>>>test_file_loop<<<'],
293                  'test_file_code':['<[code]',
294                                    '>>>test_file<<<',
295                                    '[code]>'],
296                  'test_file_code_nowiki':['<[code]',
297                                           '<[nowiki]',
298                                           '>>>test_file<<<',
299                                           '[nowiki]>',
300                                           '[code]>']
301
302                 }
303         for file_, lines in files.items():
304             add_lines_to_cache(file_, lines)
305         return
306     
307     def tearDown(self):
308         clear_file_cache()
309         return
310
311     def test_include_file_works(self):
312         expected = 'test_file'
313         line = ">>>test_file<<<"
314         out = include_file(line)
315         self.assertEqual(expected, out)
316
317     def test_include_file_recursive_works(self):
318         expected = ['content from test_file2',
319                   'test file content']
320         out = include_file_recursive('test_file2')
321         self.assertEqual(expected, out)
322
323     def test_include_file_recursive_honors_nowiki(self):
324         expected = ['content from test_file3', '<[nowiki]', '>>>test_file<<<', '[nowiki]>']
325         out = include_file_recursive('test_file3')
326         self.assertEqual(expected, out)
327
328     def test_include_file_recursive_detects_loop(self):
329         expected = ["test_file_loop content"]
330         self.assertRaises(Exception, include_file_recursive, 'test_file_loop')
331
332     def test_include_file_disabled_inside_code(self):
333         expected = ['\\defverbatim[colored]\\mfkjiamnpineejahopjoapckhioohfpa{\n\\begin{lstlisting}>>>test_file<<<\\end{lstlisting}\n}\n', '\n\\mfkjiamnpineejahopjoapckhioohfpa\n', '', '']
334         out = include_file_recursive('test_file_code')
335         out = convert2beamer(out)
336         self.assertEqual(out,expected)
337
338 def test_include_file_inside_code_inside_nowiki(self):
339         expected = ['\\defverbatim[colored]\\nebnimnjipaalcaeojiaajjiompiecho{\n\\begin{lstlisting}\\end{lstlisting}\n}\n', '', '>>>test_file<<<', '\n\\nebnimnjipaalcaeojiaajjiompiecho\n', '', '']
340         out = include_file_recursive('test_file_code_nowiki')
341         out = convert2beamer(out)
342         self.assertEqual(out,expected)
343
344 class TestSelectedFramesMode(unittest.TestCase):
345     def setUp(self):
346         return
347
348     def tearDown(self):
349         return
350     
351     def test_selected_frames_simple(self):
352         lines = ['!==== foo ====', 'mooo']
353         expected = ['!==== foo ====', 'mooo']
354         out = filter_selected_lines(lines)
355         self.assertEqual(out,expected)
356     
357     def test_unselected_frames_simple(self):
358         lines = ['==== foo ====', 'moo']
359         expected = []
360         out = filter_selected_lines(lines)
361         self.assertEqual(out,expected)
362
363     def test_selected_frames_mixed(self):
364         lines = ['==== unselected ====', 'foo', '', '!==== selected ====', 'moo', '==== unselected2 ====', 'moo', '!==== selected2 ====', 'moo']
365         expected = ['!==== selected ====', 'moo', '!==== selected2 ====', 'moo']
366         out = filter_selected_lines(lines)
367         self.assertEqual(out,expected)
368
369     def test_selected_frames_autotemplate(self):
370         lines = ['<[autotemplate]', '[autotemplate]>', '!==== selected ====', 'foo', '', '==== unselected ====']
371         expected = ['<[autotemplate]', '[autotemplate]>', '!==== selected ====', 'foo', '']
372         out = filter_selected_lines(lines)
373         self.assertEqual(out,expected)
374
375 if __name__=="__main__":
376     print "Testing wiki2beamer version %s" % (VERSIONTAG)
377     unittest.main()