##// END OF EJS Templates
fix some validation bugs in v3...
MinRK -
Show More
@@ -1,73 +1,60 b''
1 """
2 Module with tests for the coalescestreams preprocessor
3 """
1 """Tests for the coalescestreams preprocessor"""
4 2
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
7 #
3 # Copyright (c) IPython Development Team.
8 4 # Distributed under the terms of the Modified BSD License.
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
12 5
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16 6 from IPython.nbformat import current as nbformat
17 7
18 8 from .base import PreprocessorTestsBase
19 9 from ..coalescestreams import coalesce_streams
20 10
21 11
22 #-----------------------------------------------------------------------------
23 # Class
24 #-----------------------------------------------------------------------------
25 12 class TestCoalesceStreams(PreprocessorTestsBase):
26 13 """Contains test functions for coalescestreams.py"""
27 14
28 15 def test_coalesce_streams(self):
29 16 """coalesce_streams preprocessor output test"""
30 17 nb = self.build_notebook()
31 18 res = self.build_resources()
32 19 nb, res = coalesce_streams(nb, res)
33 20 outputs = nb.worksheets[0].cells[0].outputs
34 21 self.assertEqual(outputs[0].text, "a")
35 22 self.assertEqual(outputs[1].output_type, "text")
36 23 self.assertEqual(outputs[2].text, "cd")
37 24 self.assertEqual(outputs[3].text, "ef")
38 25
39 26 def test_coalesce_sequenced_streams(self):
40 27 """Can the coalesce streams preprocessor merge a sequence of streams?"""
41 28 outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="0"),
42 29 nbformat.new_output(output_type="stream", stream="stdout", output_text="1"),
43 30 nbformat.new_output(output_type="stream", stream="stdout", output_text="2"),
44 31 nbformat.new_output(output_type="stream", stream="stdout", output_text="3"),
45 32 nbformat.new_output(output_type="stream", stream="stdout", output_text="4"),
46 33 nbformat.new_output(output_type="stream", stream="stdout", output_text="5"),
47 34 nbformat.new_output(output_type="stream", stream="stdout", output_text="6"),
48 35 nbformat.new_output(output_type="stream", stream="stdout", output_text="7")]
49 36 cells=[nbformat.new_code_cell(input="# None", prompt_number=1,outputs=outputs)]
50 worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)]
37 worksheets = [nbformat.new_worksheet(cells=cells)]
51 38
52 39 nb = nbformat.new_notebook(name="notebook1", worksheets=worksheets)
53 40 res = self.build_resources()
54 41 nb, res = coalesce_streams(nb, res)
55 42 outputs = nb.worksheets[0].cells[0].outputs
56 43 self.assertEqual(outputs[0].text, u'01234567')
57 44
58 45 def test_coalesce_replace_streams(self):
59 46 """Are \\r characters handled?"""
60 47 outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="z"),
61 48 nbformat.new_output(output_type="stream", stream="stdout", output_text="\ra"),
62 49 nbformat.new_output(output_type="stream", stream="stdout", output_text="\nz\rb"),
63 50 nbformat.new_output(output_type="stream", stream="stdout", output_text="\nz"),
64 51 nbformat.new_output(output_type="stream", stream="stdout", output_text="\rc\n"),
65 52 nbformat.new_output(output_type="stream", stream="stdout", output_text="z\rz\rd")]
66 53 cells=[nbformat.new_code_cell(input="# None", prompt_number=1,outputs=outputs)]
67 worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)]
54 worksheets = [nbformat.new_worksheet(cells=cells)]
68 55
69 56 nb = nbformat.new_notebook(name="notebook1", worksheets=worksheets)
70 57 res = self.build_resources()
71 58 nb, res = coalesce_streams(nb, res)
72 59 outputs = nb.worksheets[0].cells[0].outputs
73 60 self.assertEqual(outputs[0].text, u'a\nb\nc\nd')
@@ -1,94 +1,79 b''
1 """
2 Module with tests for the revealhelp preprocessor
3 """
1 """Tests for the revealhelp preprocessor"""
4 2
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
7 #
3 # Copyright (c) IPython Development Team.
8 4 # Distributed under the terms of the Modified BSD License.
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16 5
17 6 from IPython.nbformat import current as nbformat
18 7
19 8 from .base import PreprocessorTestsBase
20 9 from ..revealhelp import RevealHelpPreprocessor
21 10
22 11
23 #-----------------------------------------------------------------------------
24 # Class
25 #-----------------------------------------------------------------------------
26
27 12 class Testrevealhelp(PreprocessorTestsBase):
28 13 """Contains test functions for revealhelp.py"""
29 14
30 15 def build_notebook(self):
31 16 """Build a reveal slides notebook in memory for use with tests.
32 17 Overrides base in PreprocessorTestsBase"""
33 18
34 19 outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="a")]
35 20
36 21 slide_metadata = {'slideshow' : {'slide_type': 'slide'}}
37 22 subslide_metadata = {'slideshow' : {'slide_type': 'subslide'}}
38 23
39 24 cells=[nbformat.new_code_cell(input="", prompt_number=1, outputs=outputs),
40 25 nbformat.new_text_cell('markdown', source="", metadata=slide_metadata),
41 26 nbformat.new_code_cell(input="", prompt_number=2, outputs=outputs),
42 27 nbformat.new_text_cell('markdown', source="", metadata=slide_metadata),
43 28 nbformat.new_text_cell('markdown', source="", metadata=subslide_metadata)]
44 worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)]
29 worksheets = [nbformat.new_worksheet(cells=cells)]
45 30
46 31 return nbformat.new_notebook(name="notebook1", worksheets=worksheets)
47 32
48 33
49 34 def build_preprocessor(self):
50 35 """Make an instance of a preprocessor"""
51 36 preprocessor = RevealHelpPreprocessor()
52 37 preprocessor.enabled = True
53 38 return preprocessor
54 39
55 40
56 41 def test_constructor(self):
57 42 """Can a RevealHelpPreprocessor be constructed?"""
58 43 self.build_preprocessor()
59 44
60 45
61 46 def test_reveal_attribute(self):
62 47 """Make sure the reveal url_prefix resources is set"""
63 48 nb = self.build_notebook()
64 49 res = self.build_resources()
65 50 preprocessor = self.build_preprocessor()
66 51 nb, res = preprocessor(nb, res)
67 52 assert 'reveal' in res
68 53 assert 'url_prefix' in res['reveal']
69 54
70 55
71 56 def test_reveal_output(self):
72 57 """Make sure that the reveal preprocessor """
73 58 nb = self.build_notebook()
74 59 res = self.build_resources()
75 60 preprocessor = self.build_preprocessor()
76 61 nb, res = preprocessor(nb, res)
77 62 cells = nb.worksheets[0].cells
78 63
79 64 # Make sure correct metadata tags are available on every cell.
80 65 for cell in cells:
81 66 assert 'slide_type' in cell.metadata
82 67
83 68 # Make sure slide end is only applied to the cells preceeding slide
84 69 # cells.
85 70 assert 'slide_helper' in cells[1].metadata
86 71 self.assertEqual(cells[1].metadata['slide_helper'], '-')
87 72
88 73 # Verify 'slide-end'
89 74 assert 'slide_helper' in cells[0].metadata
90 75 self.assertEqual(cells[0].metadata['slide_helper'], 'slide_end')
91 76 assert 'slide_helper' in cells[2].metadata
92 77 self.assertEqual(cells[2].metadata['slide_helper'], 'slide_end')
93 78 assert 'slide_helper' in cells[3].metadata
94 79 self.assertEqual(cells[3].metadata['slide_helper'], 'subslide_end')
@@ -1,205 +1,200 b''
1 1 """The basic dict based notebook format.
2 2
3 3 The Python representation of a notebook is a nested structure of
4 4 dictionary subclasses that support attribute access
5 5 (IPython.utils.ipstruct.Struct). The functions in this module are merely
6 6 helpers to build the structs in the right form.
7 7 """
8 8
9 9 # Copyright (c) IPython Development Team.
10 10 # Distributed under the terms of the Modified BSD License.
11 11
12 12 import pprint
13 13 import uuid
14 14
15 15 from IPython.utils.ipstruct import Struct
16 16 from IPython.utils.py3compat import cast_unicode, unicode_type
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Code
20 20 #-----------------------------------------------------------------------------
21 21
22 22 # Change this when incrementing the nbformat version
23 23 nbformat = 3
24 24 nbformat_minor = 0
25 25 nbformat_schema = 'nbformat.v3.schema.json'
26 26
27 27 class NotebookNode(Struct):
28 28 pass
29 29
30 30
31 31 def from_dict(d):
32 32 if isinstance(d, dict):
33 33 newd = NotebookNode()
34 34 for k,v in d.items():
35 35 newd[k] = from_dict(v)
36 36 return newd
37 37 elif isinstance(d, (tuple, list)):
38 38 return [from_dict(i) for i in d]
39 39 else:
40 40 return d
41 41
42 42
43 43 def new_output(output_type, output_text=None, output_png=None,
44 44 output_html=None, output_svg=None, output_latex=None, output_json=None,
45 45 output_javascript=None, output_jpeg=None, prompt_number=None,
46 46 ename=None, evalue=None, traceback=None, stream=None, metadata=None):
47 47 """Create a new output, to go in the ``cell.outputs`` list of a code cell.
48 48 """
49 49 output = NotebookNode()
50 50 output.output_type = unicode_type(output_type)
51 51
52 52 if metadata is None:
53 53 metadata = {}
54 54 if not isinstance(metadata, dict):
55 55 raise TypeError("metadata must be dict")
56 output.metadata = metadata
57 56
58 57 if output_type != 'pyerr':
59 58 if output_text is not None:
60 59 output.text = cast_unicode(output_text)
61 60 if output_png is not None:
62 61 output.png = cast_unicode(output_png)
63 62 if output_jpeg is not None:
64 63 output.jpeg = cast_unicode(output_jpeg)
65 64 if output_html is not None:
66 65 output.html = cast_unicode(output_html)
67 66 if output_svg is not None:
68 67 output.svg = cast_unicode(output_svg)
69 68 if output_latex is not None:
70 69 output.latex = cast_unicode(output_latex)
71 70 if output_json is not None:
72 71 output.json = cast_unicode(output_json)
73 72 if output_javascript is not None:
74 73 output.javascript = cast_unicode(output_javascript)
75 74
76 75 if output_type == u'pyout':
77 76 if prompt_number is not None:
78 77 output.prompt_number = int(prompt_number)
79 78
80 79 if output_type == u'pyerr':
81 80 if ename is not None:
82 81 output.ename = cast_unicode(ename)
83 82 if evalue is not None:
84 83 output.evalue = cast_unicode(evalue)
85 84 if traceback is not None:
86 85 output.traceback = [cast_unicode(frame) for frame in list(traceback)]
87 86
88 87 if output_type == u'stream':
89 88 output.stream = 'stdout' if stream is None else cast_unicode(stream)
89 else:
90 output.metadata = metadata
90 91
91 92 return output
92 93
93 94
94 95 def new_code_cell(input=None, prompt_number=None, outputs=None,
95 96 language=u'python', collapsed=False, metadata=None):
96 97 """Create a new code cell with input and output"""
97 98 cell = NotebookNode()
98 99 cell.cell_type = u'code'
99 100 if language is not None:
100 101 cell.language = cast_unicode(language)
101 102 if input is not None:
102 103 cell.input = cast_unicode(input)
103 104 if prompt_number is not None:
104 105 cell.prompt_number = int(prompt_number)
105 106 if outputs is None:
106 107 cell.outputs = []
107 108 else:
108 109 cell.outputs = outputs
109 110 if collapsed is not None:
110 111 cell.collapsed = bool(collapsed)
111 112 cell.metadata = NotebookNode(metadata or {})
112 113
113 114 return cell
114 115
115 116 def new_text_cell(cell_type, source=None, rendered=None, metadata=None):
116 117 """Create a new text cell."""
117 118 cell = NotebookNode()
118 119 # VERSIONHACK: plaintext -> raw
119 120 # handle never-released plaintext name for raw cells
120 121 if cell_type == 'plaintext':
121 122 cell_type = 'raw'
122 123 if source is not None:
123 124 cell.source = cast_unicode(source)
124 if rendered is not None:
125 cell.rendered = cast_unicode(rendered)
126 125 cell.metadata = NotebookNode(metadata or {})
127 126 cell.cell_type = cell_type
128 127 return cell
129 128
130 129
131 def new_heading_cell(source=None, rendered=None, level=1, metadata=None):
130 def new_heading_cell(source=None, level=1, rendered=None, metadata=None):
132 131 """Create a new section cell with a given integer level."""
133 132 cell = NotebookNode()
134 133 cell.cell_type = u'heading'
135 134 if source is not None:
136 135 cell.source = cast_unicode(source)
137 if rendered is not None:
138 cell.rendered = cast_unicode(rendered)
139 136 cell.level = int(level)
140 137 cell.metadata = NotebookNode(metadata or {})
141 138 return cell
142 139
143 140
144 141 def new_worksheet(name=None, cells=None, metadata=None):
145 142 """Create a worksheet by name with with a list of cells."""
146 143 ws = NotebookNode()
147 if name is not None:
148 ws.name = cast_unicode(name)
149 144 if cells is None:
150 145 ws.cells = []
151 146 else:
152 147 ws.cells = list(cells)
153 148 ws.metadata = NotebookNode(metadata or {})
154 149 return ws
155 150
156 151
157 152 def new_notebook(name=None, metadata=None, worksheets=None):
158 153 """Create a notebook by name, id and a list of worksheets."""
159 154 nb = NotebookNode()
160 155 nb.nbformat = nbformat
161 156 nb.nbformat_minor = nbformat_minor
162 157 if worksheets is None:
163 158 nb.worksheets = []
164 159 else:
165 160 nb.worksheets = list(worksheets)
166 161 if metadata is None:
167 162 nb.metadata = new_metadata()
168 163 else:
169 164 nb.metadata = NotebookNode(metadata)
170 165 if name is not None:
171 166 nb.metadata.name = cast_unicode(name)
172 167 return nb
173 168
174 169
175 170 def new_metadata(name=None, authors=None, license=None, created=None,
176 171 modified=None, gistid=None):
177 172 """Create a new metadata node."""
178 173 metadata = NotebookNode()
179 174 if name is not None:
180 175 metadata.name = cast_unicode(name)
181 176 if authors is not None:
182 177 metadata.authors = list(authors)
183 178 if created is not None:
184 179 metadata.created = cast_unicode(created)
185 180 if modified is not None:
186 181 metadata.modified = cast_unicode(modified)
187 182 if license is not None:
188 183 metadata.license = cast_unicode(license)
189 184 if gistid is not None:
190 185 metadata.gistid = cast_unicode(gistid)
191 186 return metadata
192 187
193 188 def new_author(name=None, email=None, affiliation=None, url=None):
194 189 """Create a new author."""
195 190 author = NotebookNode()
196 191 if name is not None:
197 192 author.name = cast_unicode(name)
198 193 if email is not None:
199 194 author.email = cast_unicode(email)
200 195 if affiliation is not None:
201 196 author.affiliation = cast_unicode(affiliation)
202 197 if url is not None:
203 198 author.url = cast_unicode(url)
204 199 return author
205 200
@@ -1,363 +1,363 b''
1 1 {
2 2 "$schema": "http://json-schema.org/draft-04/schema#",
3 3 "description": "IPython Notebook v3.0 JSON schema.",
4 4 "type": "object",
5 5 "additionalProperties": false,
6 6 "required": ["metadata", "nbformat_minor", "nbformat", "worksheets"],
7 7 "properties": {
8 8 "metadata": {
9 9 "description": "Notebook root-level metadata.",
10 10 "type": "object",
11 11 "additionalProperties": true,
12 12 "properties": {
13 13 "kernel_info": {
14 14 "description": "Kernel information.",
15 15 "type": "object",
16 16 "required": ["name", "language"],
17 17 "properties": {
18 18 "name": {
19 19 "description": "Name of the kernel specification.",
20 20 "type": "string"
21 21 },
22 22 "language": {
23 23 "description": "The programming language which this kernel runs.",
24 24 "type": "string"
25 25 },
26 26 "codemirror_mode": {
27 27 "description": "The codemirror mode to use for code in this language.",
28 28 "type": "string"
29 29 }
30 30 }
31 31 },
32 32 "signature": {
33 33 "description": "Hash of the notebook.",
34 34 "type": "string"
35 35 }
36 36 }
37 37 },
38 38 "nbformat_minor": {
39 39 "description": "Notebook format (minor number). Incremented for backward compatible changes to the notebook format.",
40 40 "type": "integer",
41 41 "minimum": 0
42 42 },
43 43 "nbformat": {
44 44 "description": "Notebook format (major number). Incremented between backwards incompatible changes to the notebook format.",
45 45 "type": "integer",
46 46 "minimum": 3,
47 47 "maximum": 3
48 48 },
49 49 "orig_nbformat": {
50 50 "description": "Original notebook format (major number) before converting the notebook between versions.",
51 51 "type": "integer",
52 52 "minimum": 1
53 53 },
54 54 "worksheets" : {
55 55 "description": "Array of worksheets",
56 56 "type": "array",
57 57 "items": {"$ref": "#/definitions/worksheet"}
58 58 }
59 59 },
60 60
61 61 "definitions": {
62 62 "worksheet": {
63 63 "additionalProperties": false,
64 64 "required" : ["cells"],
65 65 "properties":{
66 66 "cells": {
67 67 "description": "Array of cells of the current notebook.",
68 68 "type": "array",
69 69 "items": {
70 70 "type": "object",
71 71 "oneOf": [
72 72 {"$ref": "#/definitions/raw_cell"},
73 73 {"$ref": "#/definitions/markdown_cell"},
74 74 {"$ref": "#/definitions/heading_cell"},
75 75 {"$ref": "#/definitions/code_cell"}
76 76 ]
77 77 }
78 78 },
79 79 "metadata": {
80 80 "type": "object",
81 81 "description": "metadata of the current worksheet"
82 82 }
83 83 }
84 84 },
85 85 "raw_cell": {
86 86 "description": "Notebook raw nbconvert cell.",
87 87 "type": "object",
88 88 "additionalProperties": false,
89 89 "required": ["cell_type", "source"],
90 90 "properties": {
91 91 "cell_type": {
92 92 "description": "String identifying the type of cell.",
93 93 "enum": ["raw"]
94 94 },
95 95 "metadata": {
96 96 "description": "Cell-level metadata.",
97 97 "type": "object",
98 98 "additionalProperties": true,
99 99 "properties": {
100 100 "format": {
101 101 "description": "Raw cell metadata format for nbconvert.",
102 102 "type": "string"
103 103 },
104 104 "name": {"$ref": "#/definitions/misc/metadata_name"},
105 105 "tags": {"$ref": "#/definitions/misc/metadata_tags"}
106 106 }
107 107 },
108 108 "source": {"$ref": "#/definitions/misc/source"}
109 109 }
110 110 },
111 111
112 112 "markdown_cell": {
113 113 "description": "Notebook markdown cell.",
114 114 "type": "object",
115 115 "additionalProperties": false,
116 116 "required": ["cell_type", "source"],
117 117 "properties": {
118 118 "cell_type": {
119 119 "description": "String identifying the type of cell.",
120 "enum": ["markdown"]
120 "enum": ["markdown", "html"]
121 121 },
122 122 "metadata": {
123 123 "description": "Cell-level metadata.",
124 124 "type": "object",
125 125 "properties": {
126 126 "name": {"$ref": "#/definitions/misc/metadata_name"},
127 127 "tags": {"$ref": "#/definitions/misc/metadata_tags"}
128 128 },
129 129 "additionalProperties": true
130 130 },
131 131 "source": {"$ref": "#/definitions/misc/source"}
132 132 }
133 133 },
134 134
135 135 "heading_cell": {
136 136 "description": "Notebook heading cell.",
137 137 "type": "object",
138 138 "additionalProperties": false,
139 139 "required": ["cell_type", "source", "level"],
140 140 "properties": {
141 141 "cell_type": {
142 142 "description": "String identifying the type of cell.",
143 143 "enum": ["heading"]
144 144 },
145 145 "metadata": {
146 146 "description": "Cell-level metadata.",
147 147 "type": "object",
148 148 "additionalProperties": true
149 149 },
150 150 "source": {"$ref": "#/definitions/misc/source"},
151 151 "level": {
152 152 "description": "Level of heading cells.",
153 153 "type": "integer",
154 154 "minimum": 1
155 155 }
156 156 }
157 157 },
158 158
159 159 "code_cell": {
160 160 "description": "Notebook code cell.",
161 161 "type": "object",
162 162 "additionalProperties": false,
163 163 "required": ["cell_type", "input", "outputs", "collapsed", "language"],
164 164 "properties": {
165 165 "cell_type": {
166 166 "description": "String identifying the type of cell.",
167 167 "enum": ["code"]
168 168 },
169 169 "language": {
170 170 "description": "The cell's language (always Python)",
171 171 "type": "string"
172 172 },
173 173 "collapsed": {
174 174 "description": "Whether the cell is collapsed/expanded.",
175 175 "type": "boolean"
176 176 },
177 177 "metadata": {
178 178 "description": "Cell-level metadata.",
179 179 "type": "object",
180 180 "additionalProperties": true
181 181 },
182 182 "input": {"$ref": "#/definitions/misc/source"},
183 183 "outputs": {
184 184 "description": "Execution, display, or stream outputs.",
185 185 "type": "array",
186 186 "items": {"$ref": "#/definitions/output"}
187 187 },
188 188 "prompt_number": {
189 189 "description": "The code cell's prompt number. Will be null if the cell has not been run.",
190 190 "type": ["integer", "null"],
191 191 "minimum": 0
192 192 }
193 193 }
194 194 },
195 195 "output": {
196 196 "type": "object",
197 197 "oneOf": [
198 198 {"$ref": "#/definitions/pyout"},
199 199 {"$ref": "#/definitions/display_data"},
200 200 {"$ref": "#/definitions/stream"},
201 201 {"$ref": "#/definitions/pyerr"}
202 202 ]
203 203 },
204 204 "pyout": {
205 205 "description": "Result of executing a code cell.",
206 206 "type": "object",
207 207 "additionalProperties": false,
208 208 "required": ["output_type", "prompt_number"],
209 209 "properties": {
210 210 "output_type": {
211 211 "description": "Type of cell output.",
212 212 "enum": ["pyout"]
213 213 },
214 214 "prompt_number": {
215 215 "description": "A result's prompt number.",
216 216 "type": ["integer"],
217 217 "minimum": 0
218 218 },
219 219 "text": {"$ref": "#/definitions/misc/multiline_string"},
220 220 "latex": {"$ref": "#/definitions/misc/multiline_string"},
221 221 "png": {"$ref": "#/definitions/misc/multiline_string"},
222 222 "jpeg": {"$ref": "#/definitions/misc/multiline_string"},
223 223 "svg": {"$ref": "#/definitions/misc/multiline_string"},
224 224 "html": {"$ref": "#/definitions/misc/multiline_string"},
225 225 "javascript": {"$ref": "#/definitions/misc/multiline_string"},
226 226 "json": {"$ref": "#/definitions/misc/multiline_string"},
227 227 "pdf": {"$ref": "#/definitions/misc/multiline_string"},
228 228 "metadata": {"$ref": "#/definitions/misc/output_metadata"}
229 229 },
230 230 "patternProperties": {
231 231 "^[a-zA-Z0-9]+/[a-zA-Z0-9\\-\\+\\.]+$": {
232 232 "description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
233 233 "$ref": "#/definitions/misc/multiline_string"
234 234 }
235 235 }
236 236 },
237 237
238 238 "display_data": {
239 239 "description": "Data displayed as a result of code cell execution.",
240 240 "type": "object",
241 241 "additionalProperties": false,
242 242 "required": ["output_type"],
243 243 "properties": {
244 244 "output_type": {
245 245 "description": "Type of cell output.",
246 246 "enum": ["display_data"]
247 247 },
248 248 "text": {"$ref": "#/definitions/misc/multiline_string"},
249 249 "latex": {"$ref": "#/definitions/misc/multiline_string"},
250 250 "png": {"$ref": "#/definitions/misc/multiline_string"},
251 251 "jpeg": {"$ref": "#/definitions/misc/multiline_string"},
252 252 "svg": {"$ref": "#/definitions/misc/multiline_string"},
253 253 "html": {"$ref": "#/definitions/misc/multiline_string"},
254 254 "javascript": {"$ref": "#/definitions/misc/multiline_string"},
255 255 "json": {"$ref": "#/definitions/misc/multiline_string"},
256 256 "pdf": {"$ref": "#/definitions/misc/multiline_string"},
257 257 "metadata": {"$ref": "#/definitions/misc/output_metadata"}
258 258 },
259 259 "patternProperties": {
260 260 "[a-zA-Z0-9]+/[a-zA-Z0-9\\-\\+\\.]+$": {
261 261 "description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
262 262 "$ref": "#/definitions/misc/multiline_string"
263 263 }
264 264 }
265 265 },
266 266
267 267 "stream": {
268 268 "description": "Stream output from a code cell.",
269 269 "type": "object",
270 270 "additionalProperties": false,
271 271 "required": ["output_type", "stream", "text"],
272 272 "properties": {
273 273 "output_type": {
274 274 "description": "Type of cell output.",
275 275 "enum": ["stream"]
276 276 },
277 277 "stream": {
278 278 "description": "The stream type/destination.",
279 279 "type": "string"
280 280 },
281 281 "text": {
282 282 "description": "The stream's text output, represented as an array of strings.",
283 283 "$ref": "#/definitions/misc/multiline_string"
284 284 }
285 285 }
286 286 },
287 287
288 288 "pyerr": {
289 289 "description": "Output of an error that occurred during code cell execution.",
290 290 "type": "object",
291 291 "additionalProperties": false,
292 292 "required": ["output_type", "ename", "evalue", "traceback"],
293 293 "properties": {
294 294 "output_type": {
295 295 "description": "Type of cell output.",
296 296 "enum": ["pyerr"]
297 297 },
298 298 "metadata": {"$ref": "#/definitions/misc/output_metadata"},
299 299 "ename": {
300 300 "description": "The name of the error.",
301 301 "type": "string"
302 302 },
303 303 "evalue": {
304 304 "description": "The value, or message, of the error.",
305 305 "type": "string"
306 306 },
307 307 "traceback": {
308 308 "description": "The error's traceback, represented as an array of strings.",
309 309 "type": "array",
310 310 "items": {"type": "string"}
311 311 }
312 312 }
313 313 },
314 314
315 315 "misc": {
316 316 "metadata_name": {
317 317 "description": "The cell's name. If present, must be a non-empty string.",
318 318 "type": "string",
319 319 "pattern": "^.+$"
320 320 },
321 321 "metadata_tags": {
322 322 "description": "The cell's tags. Tags must be unique, and must not contain commas.",
323 323 "type": "array",
324 324 "uniqueItems": true,
325 325 "items": {
326 326 "type": "string",
327 327 "pattern": "^[^,]+$"
328 328 }
329 329 },
330 330 "source": {
331 331 "description": "Contents of the cell, represented as an array of lines.",
332 332 "$ref": "#/definitions/misc/multiline_string"
333 333 },
334 334 "prompt_number": {
335 335 "description": "The code cell's prompt number. Will be null if the cell has not been run.",
336 336 "type": ["integer", "null"],
337 337 "minimum": 0
338 338 },
339 339 "mimetype": {
340 340 "patternProperties": {
341 341 "^[a-zA-Z0-9\\-\\+]+/[a-zA-Z0-9\\-\\+]+": {
342 342 "description": "The cell's mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
343 343 "$ref": "#/definitions/misc/multiline_string"
344 344 }
345 345 }
346 346 },
347 347 "output_metadata": {
348 348 "description": "Cell output metadata.",
349 349 "type": "object",
350 350 "additionalProperties": true
351 351 },
352 352 "multiline_string": {
353 353 "oneOf" : [
354 354 {"type": "string"},
355 355 {
356 356 "type": "array",
357 357 "items": {"type": "string"}
358 358 }
359 359 ]
360 360 }
361 361 }
362 362 }
363 363 }
@@ -1,154 +1,152 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 import os
4 4 from base64 import encodestring
5 5
6 6 from ..nbbase import (
7 7 NotebookNode,
8 8 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
9 9 new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor
10 10 )
11 11
12 12 # some random base64-encoded *text*
13 13 png = encodestring(os.urandom(5)).decode('ascii')
14 14 jpeg = encodestring(os.urandom(6)).decode('ascii')
15 15
16 ws = new_worksheet(name='worksheet1')
16 ws = new_worksheet()
17 17
18 18 ws.cells.append(new_text_cell(
19 19 u'html',
20 20 source='Some NumPy Examples',
21 rendered='Some NumPy Examples'
22 21 ))
23 22
24 23
25 24 ws.cells.append(new_code_cell(
26 25 input='import numpy',
27 26 prompt_number=1,
28 27 collapsed=False
29 28 ))
30 29
31 30 ws.cells.append(new_text_cell(
32 31 u'markdown',
33 32 source='A random array',
34 rendered='A random array'
35 33 ))
36 34
37 35 ws.cells.append(new_text_cell(
38 36 u'raw',
39 37 source='A random array',
40 38 ))
41 39
42 40 ws.cells.append(new_heading_cell(
43 41 u'My Heading',
44 42 level=2
45 43 ))
46 44
47 45 ws.cells.append(new_code_cell(
48 46 input='a = numpy.random.rand(100)',
49 47 prompt_number=2,
50 48 collapsed=True
51 49 ))
52 50 ws.cells.append(new_code_cell(
53 51 input='a = 10\nb = 5\n',
54 52 prompt_number=3,
55 53 ))
56 54 ws.cells.append(new_code_cell(
57 55 input='a = 10\nb = 5',
58 56 prompt_number=4,
59 57 ))
60 58
61 59 ws.cells.append(new_code_cell(
62 60 input=u'print "ünîcødé"',
63 61 prompt_number=3,
64 62 collapsed=False,
65 63 outputs=[new_output(
66 64 output_type=u'pyout',
67 65 output_text=u'<array a>',
68 66 output_html=u'The HTML rep',
69 67 output_latex=u'$a$',
70 68 output_png=png,
71 69 output_jpeg=jpeg,
72 70 output_svg=u'<svg>',
73 output_json=u'json data',
71 output_json=u'{"json": "data"}',
74 72 output_javascript=u'var i=0;',
75 73 prompt_number=3
76 74 ),new_output(
77 75 output_type=u'display_data',
78 76 output_text=u'<array a>',
79 77 output_html=u'The HTML rep',
80 78 output_latex=u'$a$',
81 79 output_png=png,
82 80 output_jpeg=jpeg,
83 81 output_svg=u'<svg>',
84 output_json=u'json data',
82 output_json=u'{"json": "data"}',
85 83 output_javascript=u'var i=0;'
86 84 ),new_output(
87 85 output_type=u'pyerr',
88 86 ename=u'NameError',
89 87 evalue=u'NameError was here',
90 88 traceback=[u'frame 0', u'frame 1', u'frame 2']
91 89 ),new_output(
92 90 output_type=u'stream',
93 91 output_text='foo\rbar\r\n'
94 92 ),new_output(
95 93 output_type=u'stream',
96 94 stream='stderr',
97 95 output_text='\rfoo\rbar\n'
98 96 )]
99 97 ))
100 98
101 99 authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com',
102 100 affiliation=u'Fox',url=u'http://www.fox.com')]
103 101 md = new_metadata(name=u'My Notebook',license=u'BSD',created=u'8601_goes_here',
104 102 modified=u'8601_goes_here',gistid=u'21341231',authors=authors)
105 103
106 104 nb0 = new_notebook(
107 worksheets=[ws, new_worksheet(name='worksheet2')],
105 worksheets=[ws, new_worksheet()],
108 106 metadata=md
109 107 )
110 108
111 109 nb0_py = u"""# -*- coding: utf-8 -*-
112 110 # <nbformat>%i.%i</nbformat>
113 111
114 112 # <htmlcell>
115 113
116 114 # Some NumPy Examples
117 115
118 116 # <codecell>
119 117
120 118 import numpy
121 119
122 120 # <markdowncell>
123 121
124 122 # A random array
125 123
126 124 # <rawcell>
127 125
128 126 # A random array
129 127
130 128 # <headingcell level=2>
131 129
132 130 # My Heading
133 131
134 132 # <codecell>
135 133
136 134 a = numpy.random.rand(100)
137 135
138 136 # <codecell>
139 137
140 138 a = 10
141 139 b = 5
142 140
143 141 # <codecell>
144 142
145 143 a = 10
146 144 b = 5
147 145
148 146 # <codecell>
149 147
150 148 print "ünîcødé"
151 149
152 150 """ % (nbformat, nbformat_minor)
153 151
154 152
@@ -1,157 +1,148 b''
1 1 from unittest import TestCase
2 2
3 3 from ..nbbase import (
4 4 NotebookNode,
5 5 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
6 6 new_author, new_metadata, new_heading_cell, nbformat
7 7 )
8 8
9 9 class TestCell(TestCase):
10 10
11 11 def test_empty_code_cell(self):
12 12 cc = new_code_cell()
13 13 self.assertEqual(cc.cell_type,u'code')
14 14 self.assertEqual(u'input' not in cc, True)
15 15 self.assertEqual(u'prompt_number' not in cc, True)
16 16 self.assertEqual(cc.outputs, [])
17 17 self.assertEqual(cc.collapsed, False)
18 18
19 19 def test_code_cell(self):
20 20 cc = new_code_cell(input='a=10', prompt_number=0, collapsed=True)
21 21 cc.outputs = [new_output(output_type=u'pyout',
22 22 output_svg=u'foo',output_text=u'10',prompt_number=0)]
23 23 self.assertEqual(cc.input, u'a=10')
24 24 self.assertEqual(cc.prompt_number, 0)
25 25 self.assertEqual(cc.language, u'python')
26 26 self.assertEqual(cc.outputs[0].svg, u'foo')
27 27 self.assertEqual(cc.outputs[0].text, u'10')
28 28 self.assertEqual(cc.outputs[0].prompt_number, 0)
29 29 self.assertEqual(cc.collapsed, True)
30 30
31 31 def test_pyerr(self):
32 32 o = new_output(output_type=u'pyerr', ename=u'NameError',
33 33 evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2']
34 34 )
35 35 self.assertEqual(o.output_type, u'pyerr')
36 36 self.assertEqual(o.ename, u'NameError')
37 37 self.assertEqual(o.evalue, u'Name not found')
38 38 self.assertEqual(o.traceback, [u'frame 0', u'frame 1', u'frame 2'])
39 39
40 40 def test_empty_html_cell(self):
41 41 tc = new_text_cell(u'html')
42 42 self.assertEqual(tc.cell_type, u'html')
43 43 self.assertEqual(u'source' not in tc, True)
44 self.assertEqual(u'rendered' not in tc, True)
45 44
46 45 def test_html_cell(self):
47 tc = new_text_cell(u'html', 'hi', 'hi')
46 tc = new_text_cell(u'html', 'hi')
48 47 self.assertEqual(tc.source, u'hi')
49 self.assertEqual(tc.rendered, u'hi')
50 48
51 49 def test_empty_markdown_cell(self):
52 50 tc = new_text_cell(u'markdown')
53 51 self.assertEqual(tc.cell_type, u'markdown')
54 52 self.assertEqual(u'source' not in tc, True)
55 self.assertEqual(u'rendered' not in tc, True)
56 53
57 54 def test_markdown_cell(self):
58 tc = new_text_cell(u'markdown', 'hi', 'hi')
55 tc = new_text_cell(u'markdown', 'hi')
59 56 self.assertEqual(tc.source, u'hi')
60 self.assertEqual(tc.rendered, u'hi')
61 57
62 58 def test_empty_raw_cell(self):
63 59 tc = new_text_cell(u'raw')
64 60 self.assertEqual(tc.cell_type, u'raw')
65 61 self.assertEqual(u'source' not in tc, True)
66 self.assertEqual(u'rendered' not in tc, True)
67 62
68 63 def test_raw_cell(self):
69 tc = new_text_cell(u'raw', 'hi', 'hi')
64 tc = new_text_cell(u'raw', 'hi')
70 65 self.assertEqual(tc.source, u'hi')
71 self.assertEqual(tc.rendered, u'hi')
72 66
73 67 def test_empty_heading_cell(self):
74 68 tc = new_heading_cell()
75 69 self.assertEqual(tc.cell_type, u'heading')
76 70 self.assertEqual(u'source' not in tc, True)
77 self.assertEqual(u'rendered' not in tc, True)
78 71
79 72 def test_heading_cell(self):
80 tc = new_heading_cell(u'hi', u'hi', level=2)
73 tc = new_heading_cell(u'hi', level=2)
81 74 self.assertEqual(tc.source, u'hi')
82 self.assertEqual(tc.rendered, u'hi')
83 75 self.assertEqual(tc.level, 2)
84 76
85 77
86 78 class TestWorksheet(TestCase):
87 79
88 80 def test_empty_worksheet(self):
89 81 ws = new_worksheet()
90 82 self.assertEqual(ws.cells,[])
91 83 self.assertEqual(u'name' not in ws, True)
92 84
93 85 def test_worksheet(self):
94 86 cells = [new_code_cell(), new_text_cell(u'html')]
95 ws = new_worksheet(cells=cells,name=u'foo')
87 ws = new_worksheet(cells=cells)
96 88 self.assertEqual(ws.cells,cells)
97 self.assertEqual(ws.name,u'foo')
98 89
99 90 class TestNotebook(TestCase):
100 91
101 92 def test_empty_notebook(self):
102 93 nb = new_notebook()
103 94 self.assertEqual(nb.worksheets, [])
104 95 self.assertEqual(nb.metadata, NotebookNode())
105 96 self.assertEqual(nb.nbformat,nbformat)
106 97
107 98 def test_notebook(self):
108 99 worksheets = [new_worksheet(),new_worksheet()]
109 100 metadata = new_metadata(name=u'foo')
110 101 nb = new_notebook(metadata=metadata,worksheets=worksheets)
111 102 self.assertEqual(nb.metadata.name,u'foo')
112 103 self.assertEqual(nb.worksheets,worksheets)
113 104 self.assertEqual(nb.nbformat,nbformat)
114 105
115 106 def test_notebook_name(self):
116 107 worksheets = [new_worksheet(),new_worksheet()]
117 108 nb = new_notebook(name='foo',worksheets=worksheets)
118 109 self.assertEqual(nb.metadata.name,u'foo')
119 110 self.assertEqual(nb.worksheets,worksheets)
120 111 self.assertEqual(nb.nbformat,nbformat)
121 112
122 113 class TestMetadata(TestCase):
123 114
124 115 def test_empty_metadata(self):
125 116 md = new_metadata()
126 117 self.assertEqual(u'name' not in md, True)
127 118 self.assertEqual(u'authors' not in md, True)
128 119 self.assertEqual(u'license' not in md, True)
129 120 self.assertEqual(u'saved' not in md, True)
130 121 self.assertEqual(u'modified' not in md, True)
131 122 self.assertEqual(u'gistid' not in md, True)
132 123
133 124 def test_metadata(self):
134 125 authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com')]
135 126 md = new_metadata(name=u'foo',license=u'BSD',created=u'today',
136 127 modified=u'now',gistid=u'21341231',authors=authors)
137 128 self.assertEqual(md.name, u'foo')
138 129 self.assertEqual(md.license, u'BSD')
139 130 self.assertEqual(md.created, u'today')
140 131 self.assertEqual(md.modified, u'now')
141 132 self.assertEqual(md.gistid, u'21341231')
142 133 self.assertEqual(md.authors, authors)
143 134
144 135 class TestOutputs(TestCase):
145 136 def test_binary_png(self):
146 137 out = new_output(output_png=b'\x89PNG\r\n\x1a\n', output_type='display_data')
147 138
148 139 def test_b64b6tes_png(self):
149 140 out = new_output(output_png=b'iVBORw0KG', output_type='display_data')
150 141
151 142 def test_binary_jpeg(self):
152 143 out = new_output(output_jpeg=b'\xff\xd8', output_type='display_data')
153 144
154 145 def test_b64b6tes_jpeg(self):
155 146 out = new_output(output_jpeg=b'/9', output_type='display_data')
156 147
157 148
General Comments 0
You need to be logged in to leave comments. Login now