Show More
@@ -1,71 +1,82 b'' | |||||
1 | from nbconvert import ConverterRST, main |
|
1 | from nbconvert import ConverterRST, main | |
2 | import nose.tools as nt |
|
2 | 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) |
|
32 | f = c.render() | |
23 | f = c.render() |
|
33 | nt.assert_true(f.endswith('.rst'), 'changed file extension to rst') | |
24 | nt.assert_true(f.endswith('.rst'), 'changed file extension to rst') |
|
|||
25 |
|
34 | |||
|
35 | def test_main(self): | |||
|
36 | """ | |||
|
37 | Test main entry point | |||
|
38 | """ | |||
|
39 | main(self.infile) | |||
|
40 | nt.assert_true(self.outfile_exists('rst')) | |||
26 |
|
41 | |||
27 | @nt.with_setup(clean_dir, clean_dir) |
|
42 | def test_render_heading(self): | |
28 | def test_main(): |
|
43 | """ | |
29 | """ |
|
44 | Unit test for cell type "heading" | |
30 | Test main entry point |
|
45 | """ | |
31 | """ |
|
46 | # Generate and test heading cells level 1-6 | |
32 | main(fname) |
|
47 | for level in xrange(1, 7): | |
33 | nt.assert_true(os.path.exists(out_fname)) |
|
48 | cell = { | |
|
49 | 'cell_type': 'heading', | |||
|
50 | 'level': level, | |||
|
51 | 'source': ['Test for heading type H{0}'.format(level)] | |||
|
52 | } | |||
34 |
|
53 | |||
|
54 | # Convert cell dictionaries to NotebookNode | |||
|
55 | cell_nb = nbformat.NotebookNode(cell) | |||
35 |
|
56 | |||
36 | def test_render_heading(): |
|
57 | # Make sure "source" attribute is uniconde not list. | |
37 | """ Unit test for cell type "heading" """ |
|
58 | # For some reason, creating a NotebookNode manually like | |
38 | # Generate and test heading cells level 1-6 |
|
59 | # this isn't converting source to a string like using | |
39 | for level in xrange(1, 7): |
|
60 | # the create-from-file routine. | |
40 | cell = { |
|
61 | if type(cell_nb.source) is list: | |
41 | 'cell_type': 'heading', |
|
62 | cell_nb.source = '\n'.join(cell_nb.source) | |
42 | 'level': level, |
|
|||
43 | 'source': ['Test for heading type H{0}'.format(level)] |
|
|||
44 | } |
|
|||
45 | # Convert cell dictionaries to NotebookNode |
|
|||
46 | cell_nb = nbformat.NotebookNode(cell) |
|
|||
47 | # Make sure "source" attribute is uniconde not list. |
|
|||
48 | # For some reason, creating a NotebookNode manually like |
|
|||
49 | # this isn't converting source to a string like using |
|
|||
50 | # the create-from-file routine. |
|
|||
51 | if type(cell_nb.source) is list: |
|
|||
52 | cell_nb.source = '\n'.join(cell_nb.source) |
|
|||
53 | # Render to rst |
|
|||
54 | c = ConverterRST('') |
|
|||
55 | rst_list = c.render_heading(cell_nb) |
|
|||
56 | # render should return a list |
|
|||
57 | nt.assert_true(isinstance(rst_list, list)) |
|
|||
58 | rst_str = "".join(rst_list) |
|
|||
59 | # Confirm rst content |
|
|||
60 | chk_str = "Test for heading type H{0}\n{1}\n".format( |
|
|||
61 | level, c.heading_level[level] * 24) |
|
|||
62 | nt.assert_equal(rst_str, chk_str) |
|
|||
63 |
|
63 | |||
|
64 | # Render to rst | |||
|
65 | c = ConverterRST('') | |||
|
66 | rst_list = c.render_heading(cell_nb) | |||
64 |
|
67 | |||
65 | @nt.with_setup(clean_dir, clean_dir) |
|
68 | # render should return a list | |
66 | def test_main_html(): |
|
69 | nt.assert_true(isinstance(rst_list, list)) | |
67 | """ |
|
70 | rst_str = "".join(rst_list) | |
68 | Test main entry point |
|
71 | ||
69 | """ |
|
72 | # Confirm rst content | |
70 | main(fname, format='html') |
|
73 | chk_str = "Test for heading type H{0}\n{1}\n".format( | |
71 | nt.assert_true(os.path.exists('tests/test.html')) |
|
74 | level, c.heading_level[level] * 24) | |
|
75 | nt.assert_equal(rst_str, chk_str) | |||
|
76 | ||||
|
77 | def test_main_html(self): | |||
|
78 | """ | |||
|
79 | Test main entry point | |||
|
80 | """ | |||
|
81 | main(self.infile, format='html') | |||
|
82 | nt.assert_true(self.outfile_exists('html')) |
General Comments 0
You need to be logged in to leave comments.
Login now