Show More
@@ -1,69 +1,70 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 | from IPython.nbformat import current as nbformat |
|
6 | from IPython.nbformat import current as nbformat | |
7 |
|
7 | |||
8 | fname = 'tests/test.ipynb' |
|
8 | fname = 'tests/test.ipynb' | |
9 | out_fname = 'tests/test.rst' |
|
9 | out_fname = 'tests/test.rst' | |
10 |
|
10 | |||
11 |
|
11 | |||
12 | def clean_dir(): |
|
12 | def clean_dir(): | |
13 | "Remove .rst files created during conversion" |
|
13 | "Remove .rst files created during conversion" | |
14 | map(os.remove, glob.glob("./tests/*.rst")) |
|
14 | map(os.remove, glob.glob("./tests/*.rst")) | |
15 | map(os.remove, glob.glob("./tests/*.png")) |
|
15 | map(os.remove, glob.glob("./tests/*.png")) | |
16 | map(os.remove, glob.glob("./tests/*.html")) |
|
16 | map(os.remove, glob.glob("./tests/*.html")) | |
|
17 | map(os.remove, glob.glob("./tests/test_files/*")) | |||
17 |
|
18 | |||
18 |
|
19 | |||
19 | @nt.with_setup(clean_dir, clean_dir) |
|
20 | @nt.with_setup(clean_dir, clean_dir) | |
20 | def test_simple(): |
|
21 | def test_simple(): | |
21 | c = ConverterRST(fname) |
|
22 | c = ConverterRST(fname) | |
22 | f = c.render() |
|
23 | f = c.render() | |
23 | nt.assert_true('rst' in f, 'changed file extension to rst') |
|
24 | nt.assert_true('rst' in f, 'changed file extension to rst') | |
24 |
|
25 | |||
25 |
|
26 | |||
26 | @nt.with_setup(clean_dir, clean_dir) |
|
27 | @nt.with_setup(clean_dir, clean_dir) | |
27 | def test_main(): |
|
28 | def test_main(): | |
28 | """ |
|
29 | """ | |
29 | Test main entry point |
|
30 | Test main entry point | |
30 | """ |
|
31 | """ | |
31 | main(fname) |
|
32 | main(fname) | |
32 | nt.assert_true(os.path.exists(out_fname)) |
|
33 | nt.assert_true(os.path.exists(out_fname)) | |
33 |
|
34 | |||
34 |
|
35 | |||
35 | def test_render_heading(): |
|
36 | def test_render_heading(): | |
36 | """ Unit test for cell type "heading" """ |
|
37 | """ Unit test for cell type "heading" """ | |
37 | # Generate and test heading cells level 1-6 |
|
38 | # Generate and test heading cells level 1-6 | |
38 | for level in xrange(1, 7): |
|
39 | for level in xrange(1, 7): | |
39 | cell = { |
|
40 | cell = { | |
40 | 'cell_type': 'heading', |
|
41 | 'cell_type': 'heading', | |
41 | 'level' : level, |
|
42 | 'level' : level, | |
42 | 'source' : ['Test for heading type H{0}'.format(level)] |
|
43 | 'source' : ['Test for heading type H{0}'.format(level)] | |
43 | } |
|
44 | } | |
44 | # Convert cell dictionaries to NotebookNode |
|
45 | # Convert cell dictionaries to NotebookNode | |
45 | cell_nb = nbformat.NotebookNode(cell) |
|
46 | cell_nb = nbformat.NotebookNode(cell) | |
46 | # Make sure "source" attribute is uniconde not list. |
|
47 | # Make sure "source" attribute is uniconde not list. | |
47 | # For some reason, creating a NotebookNode manually like |
|
48 | # For some reason, creating a NotebookNode manually like | |
48 | # this isn't converting source to a string like using |
|
49 | # this isn't converting source to a string like using | |
49 | # the create-from-file routine. |
|
50 | # the create-from-file routine. | |
50 | if type(cell_nb.source) is list: |
|
51 | if type(cell_nb.source) is list: | |
51 | cell_nb.source = '\n'.join(cell_nb.source) |
|
52 | cell_nb.source = '\n'.join(cell_nb.source) | |
52 | # Render to rst |
|
53 | # Render to rst | |
53 | c = ConverterRST('') |
|
54 | c = ConverterRST('') | |
54 | rst_list = c.render_heading(cell_nb) |
|
55 | rst_list = c.render_heading(cell_nb) | |
55 | nt.assert_true(isinstance(rst_list, list)) # render should return a list |
|
56 | nt.assert_true(isinstance(rst_list, list)) # render should return a list | |
56 | rst_str = "".join(rst_list) |
|
57 | rst_str = "".join(rst_list) | |
57 | # Confirm rst content |
|
58 | # Confirm rst content | |
58 | chk_str = "Test for heading type H{0}\n{1}\n".format( |
|
59 | chk_str = "Test for heading type H{0}\n{1}\n".format( | |
59 | level, c.heading_level[level] * 24) |
|
60 | level, c.heading_level[level] * 24) | |
60 | nt.assert_equal(rst_str, chk_str) |
|
61 | nt.assert_equal(rst_str, chk_str) | |
61 |
|
62 | |||
62 |
|
63 | |||
63 | @nt.with_setup(clean_dir, clean_dir) |
|
64 | @nt.with_setup(clean_dir, clean_dir) | |
64 | def test_main_html(): |
|
65 | def test_main_html(): | |
65 | """ |
|
66 | """ | |
66 | Test main entry point |
|
67 | Test main entry point | |
67 | """ |
|
68 | """ | |
68 | main(fname, format='html') |
|
69 | main(fname, format='html') | |
69 | nt.assert_true(os.path.exists('tests/test.html')) |
|
70 | nt.assert_true(os.path.exists('tests/test.html')) |
General Comments 0
You need to be logged in to leave comments.
Login now