Show More
@@ -3,69 +3,80 b' import nose.tools as nt' | |||
|
3 | 3 | |
|
4 | 4 | import os |
|
5 | 5 | import glob |
|
6 | import tempfile | |
|
7 | import shutil | |
|
6 | 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(): | |
|
13 | "Remove .rst files created during conversion" | |
|
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/*")) | |
|
23 | def tearDown(self): | |
|
24 | shutil.rmtree(self.wrkdir) | |
|
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) | |
|
21 | def test_simple(): | |
|
22 | c = ConverterRST(fname) | |
|
23 | f = c.render() | |
|
24 | nt.assert_true(f.endswith('.rst'), 'changed file extension to rst') | |
|
30 | def test_simple(self): | |
|
31 | c = ConverterRST(self.infile) | |
|
32 | f = c.render() | |
|
33 | 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) | |
|
28 | def test_main(): | |
|
29 | """ | |
|
30 | Test main entry point | |
|
31 | """ | |
|
32 | main(fname) | |
|
33 | nt.assert_true(os.path.exists(out_fname)) | |
|
42 | def test_render_heading(self): | |
|
43 | """ | |
|
44 | Unit test for cell type "heading" | |
|
45 | """ | |
|
46 | # Generate and test heading cells level 1-6 | |
|
47 | for level in xrange(1, 7): | |
|
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(): | |
|
37 | """ Unit test for cell type "heading" """ | |
|
38 | # Generate and test heading cells level 1-6 | |
|
39 | for level in xrange(1, 7): | |
|
40 | cell = { | |
|
41 | 'cell_type': 'heading', | |
|
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) | |
|
57 | # Make sure "source" attribute is uniconde not list. | |
|
58 | # For some reason, creating a NotebookNode manually like | |
|
59 | # this isn't converting source to a string like using | |
|
60 | # the create-from-file routine. | |
|
61 | if type(cell_nb.source) is list: | |
|
62 | cell_nb.source = '\n'.join(cell_nb.source) | |
|
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) | |
|
66 | def test_main_html(): | |
|
67 | """ | |
|
68 | Test main entry point | |
|
69 | """ | |
|
70 | main(fname, format='html') | |
|
71 | nt.assert_true(os.path.exists('tests/test.html')) | |
|
68 | # render should return a list | |
|
69 | nt.assert_true(isinstance(rst_list, list)) | |
|
70 | rst_str = "".join(rst_list) | |
|
71 | ||
|
72 | # Confirm rst content | |
|
73 | chk_str = "Test for heading type H{0}\n{1}\n".format( | |
|
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