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.
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.
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/>.
22 sys.path.append('../code')
23 from wiki2beamer import *
25 class TestBasics(unittest.TestCase):
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')
34 def test_join_lines_shortlines(self):
36 joined = joinLines(lines)
37 self.assertEqual(len(joined), 0)
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)
45 def test_escape_resub2(self):
48 p = re.compile(".*(b).*")
49 out = p.sub(escape_resub(substr), instr)
50 self.assertEqual(out, substr)
53 class TestTransform(unittest.TestCase):
55 self.state = w2bstate()
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')
66 def test_section(self):
67 self.assertEqual(transform('== foo ==', self.state), '\n\\section{foo}\n\n')
69 def test_subsection(self):
70 self.assertEqual(transform('=== foo ===', self.state), '\n\\subsection{foo}\n\n')
72 def test_footnote(self):
73 self.assertEqual(transform('(((foo)))', self.state), '\\footnote{foo}')
75 def test_columns(self):
76 self.assertEqual(transform('[[[6cm]]]', self.state), '\\column{6cm}')
78 def test_typewriter(self):
79 input_expected = [('@TEST@', '\\texttt{TEST}'),
82 ('@TEST@TEST@', '\\texttt{TEST}TEST@'),
83 ('@TEST@ test @TEST@',
84 '\\texttt{TEST} test \\texttt{TEST}'),
85 ('\@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)
94 input_expected = [('!TEST!', '\\alert{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)
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')
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')
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')
123 self.assertEqual(transform('-<2-> {foo}', self.state), '\only<2->{foo}')
124 self.assertEqual(transform(' -<2->{\nfoo', self.state), ' \only<2->{\nfoo')
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}}')
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)
140 def test_search_escape_sequences_short(self):
142 (open, close) = expand_code_search_escape_sequences(code)
143 self.assertEqual(code.find(open), -1)
144 self.assertEqual(code.find(close), -1)
146 def test_search_escape_sequences_veryshort(self):
148 (open, close) = expand_code_search_escape_sequences(code)
149 self.assertEqual(code.find(open), -1)
150 self.assertEqual(code.find(close), -1)
152 def test_search_escape_sequences_large(self):
154 for i in xrange(0, 10000):
155 code.append(chr(random.randint(48,57)))
158 (open, close) = expand_code_search_escape_sequences(code)
159 self.assertEqual(code.find(open), -1)
160 self.assertEqual(code.find(close), -1)
162 def test_expand_code_tokenize_anims(self):
163 items = ['1', '2', '3', '-', ',', '[', ']', '<', '>', 'a', 'b', 'c', 'd', 'e', '}', '{']
165 for i in xrange(0, 100):
167 random.shuffle(items)
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(']')))
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], [''])
182 class TestConvert2Beamer(unittest.TestCase):
189 def test_nowiki(self):
190 lines = ['<[nowiki ]%',\
193 expected = ['\n', '%',\
197 out = convert2beamer(lines)
198 self.assertEqual(out, expected)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
272 def test_fragile(self):
273 lines = ['==== foo ====[fragile]', 'foo']
275 '\\begin{frame}[fragile]\n \\frametitle{foo}\n \n',
279 out = convert2beamer(lines)
280 self.assertEqual(out, expected)
282 class TestFileInclusion(unittest.TestCase):
284 files = {'test_file': ['test file content'],
285 'test_file2':['content from test_file2',
287 'test_file3':['content from test_file3',
291 'test_file_loop':['test_file_loop content',
292 '>>>test_file_loop<<<'],
293 'test_file_code':['<[code]',
296 'test_file_code_nowiki':['<[code]',
303 for file_, lines in files.items():
304 add_lines_to_cache(file_, lines)
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)
317 def test_include_file_recursive_works(self):
318 expected = ['content from test_file2',
320 out = include_file_recursive('test_file2')
321 self.assertEqual(expected, out)
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)
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')
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)
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)
344 class TestSelectedFramesMode(unittest.TestCase):
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)
357 def test_unselected_frames_simple(self):
358 lines = ['==== foo ====', 'moo']
360 out = filter_selected_lines(lines)
361 self.assertEqual(out,expected)
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)
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)
375 if __name__=="__main__":
376 print "Testing wiki2beamer version %s" % (VERSIONTAG)