##// END OF EJS Templates
Clean up simple_test.py:...
Maximilian Albert -
Show More
@@ -3,38 +3,46 b' import nose.tools as nt'
3
3
4 import os
4 import os
5 import glob
5 import glob
6 import tempfile
7 import shutil
6 from IPython.nbformat import current as nbformat
8 from IPython.nbformat import current as nbformat
7
9
8 fname = 'tests/test.ipynb'
9 out_fname = 'tests/test.rst'
10
10
11 class TestSimple(object):
12 def setUp(self):
13 """
14 Create a new temporary directory and copy the input file
15 'test.ipynb' there.
16 """
17 self.fname = 'test.ipynb'
18 self.basename = os.path.splitext(self.fname)[0]
19 self.wrkdir = tempfile.mkdtemp()
20 self.infile = os.path.join(self.wrkdir, self.fname)
21 shutil.copy(os.path.join('tests', self.fname), self.infile)
11
22
12 def clean_dir():
23 def tearDown(self):
13 "Remove .rst files created during conversion"
24 shutil.rmtree(self.wrkdir)
14 map(os.remove, glob.glob("./tests/*.rst"))
15 map(os.remove, glob.glob("./tests/*.png"))
16 map(os.remove, glob.glob("./tests/*.html"))
17 map(os.remove, glob.glob("./tests/test_files/*"))
18
25
26 def outfile_exists(self, fmt):
27 return os.path.exists(os.path.join(self.wrkdir,
28 self.basename + '.' + fmt))
19
29
20 @nt.with_setup(clean_dir, clean_dir)
30 def test_simple(self):
21 def test_simple():
31 c = ConverterRST(self.infile)
22 c = ConverterRST(fname)
23 f = c.render()
32 f = c.render()
24 nt.assert_true(f.endswith('.rst'), 'changed file extension to rst')
33 nt.assert_true(f.endswith('.rst'), 'changed file extension to rst')
25
34
26
35 def test_main(self):
27 @nt.with_setup(clean_dir, clean_dir)
28 def test_main():
29 """
36 """
30 Test main entry point
37 Test main entry point
31 """
38 """
32 main(fname)
39 main(self.infile)
33 nt.assert_true(os.path.exists(out_fname))
40 nt.assert_true(self.outfile_exists('rst'))
34
41
35
42 def test_render_heading(self):
36 def test_render_heading():
43 """
37 """ Unit test for cell type "heading" """
44 Unit test for cell type "heading"
45 """
38 # Generate and test heading cells level 1-6
46 # Generate and test heading cells level 1-6
39 for level in xrange(1, 7):
47 for level in xrange(1, 7):
40 cell = {
48 cell = {
@@ -42,30 +50,33 b' def test_render_heading():'
42 'level': level,
50 'level': level,
43 'source': ['Test for heading type H{0}'.format(level)]
51 'source': ['Test for heading type H{0}'.format(level)]
44 }
52 }
53
45 # Convert cell dictionaries to NotebookNode
54 # Convert cell dictionaries to NotebookNode
46 cell_nb = nbformat.NotebookNode(cell)
55 cell_nb = nbformat.NotebookNode(cell)
56
47 # Make sure "source" attribute is uniconde not list.
57 # Make sure "source" attribute is uniconde not list.
48 # For some reason, creating a NotebookNode manually like
58 # For some reason, creating a NotebookNode manually like
49 # this isn't converting source to a string like using
59 # this isn't converting source to a string like using
50 # the create-from-file routine.
60 # the create-from-file routine.
51 if type(cell_nb.source) is list:
61 if type(cell_nb.source) is list:
52 cell_nb.source = '\n'.join(cell_nb.source)
62 cell_nb.source = '\n'.join(cell_nb.source)
63
53 # Render to rst
64 # Render to rst
54 c = ConverterRST('')
65 c = ConverterRST('')
55 rst_list = c.render_heading(cell_nb)
66 rst_list = c.render_heading(cell_nb)
67
56 # render should return a list
68 # render should return a list
57 nt.assert_true(isinstance(rst_list, list))
69 nt.assert_true(isinstance(rst_list, list))
58 rst_str = "".join(rst_list)
70 rst_str = "".join(rst_list)
71
59 # Confirm rst content
72 # Confirm rst content
60 chk_str = "Test for heading type H{0}\n{1}\n".format(
73 chk_str = "Test for heading type H{0}\n{1}\n".format(
61 level, c.heading_level[level] * 24)
74 level, c.heading_level[level] * 24)
62 nt.assert_equal(rst_str, chk_str)
75 nt.assert_equal(rst_str, chk_str)
63
76
64
77 def test_main_html(self):
65 @nt.with_setup(clean_dir, clean_dir)
66 def test_main_html():
67 """
78 """
68 Test main entry point
79 Test main entry point
69 """
80 """
70 main(fname, format='html')
81 main(self.infile, format='html')
71 nt.assert_true(os.path.exists('tests/test.html'))
82 nt.assert_true(self.outfile_exists('html'))
General Comments 0
You need to be logged in to leave comments. Login now