Show More
@@ -1,82 +1,84 b'' | |||||
1 | from nbconvert import ConverterRST, main |
|
1 | from nbconvert import ConverterRST, main, converters | |
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 |
|
6 | import tempfile | |
7 | import shutil |
|
7 | import shutil | |
8 | from IPython.nbformat import current as nbformat |
|
8 | from IPython.nbformat import current as nbformat | |
9 |
|
9 | |||
10 |
|
10 | |||
11 | class TestSimple(object): |
|
11 | class TestSimple(object): | |
12 | def setUp(self): |
|
12 | def setUp(self): | |
13 | """ |
|
13 | """ | |
14 | Create a new temporary directory and copy the input file |
|
14 | Create a new temporary directory and copy the input file | |
15 | 'test.ipynb' there. |
|
15 | 'test.ipynb' there. | |
16 | """ |
|
16 | """ | |
17 | self.fname = 'test.ipynb' |
|
17 | self.fname = 'test.ipynb' | |
18 | self.basename = os.path.splitext(self.fname)[0] |
|
18 | self.basename = os.path.splitext(self.fname)[0] | |
19 | self.wrkdir = tempfile.mkdtemp() |
|
19 | self.wrkdir = tempfile.mkdtemp() | |
20 | self.infile = os.path.join(self.wrkdir, self.fname) |
|
20 | self.infile = os.path.join(self.wrkdir, self.fname) | |
21 | shutil.copy(os.path.join('tests', self.fname), self.infile) |
|
21 | shutil.copy(os.path.join('tests', self.fname), self.infile) | |
22 |
|
22 | |||
23 | def tearDown(self): |
|
23 | def tearDown(self): | |
24 | shutil.rmtree(self.wrkdir) |
|
24 | shutil.rmtree(self.wrkdir) | |
25 |
|
25 | |||
26 | def outfile_exists(self, fmt): |
|
26 | def outfile_exists(self, fmt): | |
|
27 | extension = converters[fmt].extension | |||
27 | return os.path.exists(os.path.join(self.wrkdir, |
|
28 | return os.path.exists(os.path.join(self.wrkdir, | |
28 |
self.basename + '.' + |
|
29 | self.basename + '.' + extension)) | |
29 |
|
30 | |||
30 | def test_simple(self): |
|
31 | def test_simple(self): | |
31 | c = ConverterRST(self.infile) |
|
32 | c = ConverterRST(self.infile) | |
32 | f = c.render() |
|
33 | f = c.render() | |
33 | nt.assert_true(f.endswith('.rst'), 'changed file extension to rst') |
|
34 | nt.assert_true(f.endswith('.rst'), 'changed file extension to rst') | |
34 |
|
35 | |||
|
36 | def run_main(self, fmt): | |||
|
37 | """ | |||
|
38 | Run the 'main' method to convert the input file to the given | |||
|
39 | format and check that the expected output file exists. | |||
|
40 | """ | |||
|
41 | main(self.infile, format=fmt) | |||
|
42 | nt.assert_true(self.outfile_exists(fmt)) | |||
|
43 | ||||
35 | def test_main(self): |
|
44 | def test_main(self): | |
36 | """ |
|
45 | """ | |
37 | Test main entry point |
|
46 | Test main entry point with all known formats. | |
38 | """ |
|
47 | """ | |
39 | main(self.infile) |
|
48 | for fmt in converters: | |
40 | nt.assert_true(self.outfile_exists('rst')) |
|
49 | yield self.run_main, fmt | |
41 |
|
50 | |||
42 | def test_render_heading(self): |
|
51 | def test_render_heading(self): | |
43 | """ |
|
52 | """ | |
44 | Unit test for cell type "heading" |
|
53 | Unit test for cell type "heading" | |
45 | """ |
|
54 | """ | |
46 | # Generate and test heading cells level 1-6 |
|
55 | # Generate and test heading cells level 1-6 | |
47 | for level in xrange(1, 7): |
|
56 | for level in xrange(1, 7): | |
48 | cell = { |
|
57 | cell = { | |
49 | 'cell_type': 'heading', |
|
58 | 'cell_type': 'heading', | |
50 | 'level': level, |
|
59 | 'level': level, | |
51 | 'source': ['Test for heading type H{0}'.format(level)] |
|
60 | 'source': ['Test for heading type H{0}'.format(level)] | |
52 | } |
|
61 | } | |
53 |
|
62 | |||
54 | # Convert cell dictionaries to NotebookNode |
|
63 | # Convert cell dictionaries to NotebookNode | |
55 | cell_nb = nbformat.NotebookNode(cell) |
|
64 | cell_nb = nbformat.NotebookNode(cell) | |
56 |
|
65 | |||
57 | # Make sure "source" attribute is uniconde not list. |
|
66 | # Make sure "source" attribute is uniconde not list. | |
58 | # For some reason, creating a NotebookNode manually like |
|
67 | # For some reason, creating a NotebookNode manually like | |
59 | # this isn't converting source to a string like using |
|
68 | # this isn't converting source to a string like using | |
60 | # the create-from-file routine. |
|
69 | # the create-from-file routine. | |
61 | if type(cell_nb.source) is list: |
|
70 | if type(cell_nb.source) is list: | |
62 | cell_nb.source = '\n'.join(cell_nb.source) |
|
71 | cell_nb.source = '\n'.join(cell_nb.source) | |
63 |
|
72 | |||
64 | # Render to rst |
|
73 | # Render to rst | |
65 | c = ConverterRST('') |
|
74 | c = ConverterRST('') | |
66 | rst_list = c.render_heading(cell_nb) |
|
75 | rst_list = c.render_heading(cell_nb) | |
67 |
|
76 | |||
68 | # render should return a list |
|
77 | # render should return a list | |
69 | nt.assert_true(isinstance(rst_list, list)) |
|
78 | nt.assert_true(isinstance(rst_list, list)) | |
70 | rst_str = "".join(rst_list) |
|
79 | rst_str = "".join(rst_list) | |
71 |
|
80 | |||
72 | # Confirm rst content |
|
81 | # Confirm rst content | |
73 | chk_str = "Test for heading type H{0}\n{1}\n".format( |
|
82 | chk_str = "Test for heading type H{0}\n{1}\n".format( | |
74 | level, c.heading_level[level] * 24) |
|
83 | level, c.heading_level[level] * 24) | |
75 | nt.assert_equal(rst_str, chk_str) |
|
84 | 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