##// END OF EJS Templates
Add test for prompt numbers
Jessica B. Hamrick -
Show More
@@ -1,82 +1,90 b''
1 1 """
2 2 Module with tests for the execute preprocessor.
3 3 """
4 4
5 5 # Copyright (c) IPython Development Team.
6 6 # Distributed under the terms of the Modified BSD License.
7 7
8 8 import copy
9 9 import glob
10 10 import os
11 11 import re
12 12
13 13 from IPython.nbformat import current as nbformat
14 14
15 15 from .base import PreprocessorTestsBase
16 16 from ..execute import ExecutePreprocessor
17 17
18 18 from IPython.nbconvert.filters import strip_ansi
19 19
20 20 addr_pat = re.compile(r'0x[0-9a-f]{7,9}')
21 21
22 22 class TestExecute(PreprocessorTestsBase):
23 23 """Contains test functions for execute.py"""
24 24
25 25 @staticmethod
26 26 def normalize_output(output):
27 27 """
28 28 Normalizes outputs for comparison.
29 29 """
30 30 output = dict(output)
31 31 if 'metadata' in output:
32 32 del output['metadata']
33 33 if 'text' in output:
34 34 output['text'] = re.sub(addr_pat, '<HEXADDR>', output['text'])
35 35 if 'svg' in output:
36 36 del output['text']
37 37 if 'traceback' in output:
38 38 tb = []
39 39 for line in output['traceback']:
40 40 tb.append(strip_ansi(line))
41 41 output['traceback'] = tb
42 42
43 43 return output
44 44
45 45
46 46 def assert_notebooks_equal(self, expected, actual):
47 47 expected_cells = expected['worksheets'][0]['cells']
48 48 actual_cells = actual['worksheets'][0]['cells']
49 49 assert len(expected_cells) == len(actual_cells)
50 50
51 51 for expected_cell, actual_cell in zip(expected_cells, actual_cells):
52 52 expected_outputs = expected_cell.get('outputs', [])
53 53 actual_outputs = actual_cell.get('outputs', [])
54 54 normalized_expected_outputs = list(map(self.normalize_output, expected_outputs))
55 55 normalized_actual_outputs = list(map(self.normalize_output, actual_outputs))
56 56 assert normalized_expected_outputs == normalized_actual_outputs
57 57
58 expected_prompt_number = expected_cell.get('prompt_number', None)
59 actual_prompt_number = actual_cell.get('prompt_number', None)
60 assert expected_prompt_number == actual_prompt_number
61
58 62
59 63 def build_preprocessor(self):
60 64 """Make an instance of a preprocessor"""
61 65 preprocessor = ExecutePreprocessor()
62 66 preprocessor.enabled = True
63 67 return preprocessor
64 68
65 69
66 70 def test_constructor(self):
67 71 """Can a ExecutePreprocessor be constructed?"""
68 72 self.build_preprocessor()
69 73
70 74
71 75 def test_run_notebooks(self):
72 76 """Runs a series of test notebooks and compares them to their actual output"""
73 77 current_dir = os.path.dirname(__file__)
74 78 input_files = glob.glob(os.path.join(current_dir, 'files', '*.ipynb'))
75 79 for filename in input_files:
76 80 with open(os.path.join(current_dir, 'files', filename)) as f:
77 81 input_nb = nbformat.read(f, 'ipynb')
78 82 res = self.build_resources()
79 83 preprocessor = self.build_preprocessor()
80 output_nb, _ = preprocessor(copy.deepcopy(input_nb), res)
84 cleaned_input_nb = copy.deepcopy(input_nb)
85 for cell in cleaned_input_nb.worksheets[0].cells:
86 if 'prompt_number' in cell:
87 del cell['prompt_number']
88 cell['outputs'] = []
89 output_nb, _ = preprocessor(cleaned_input_nb, res)
81 90 self.assert_notebooks_equal(output_nb, input_nb)
82
General Comments 0
You need to be logged in to leave comments. Login now