-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathtest_header.py
101 lines (82 loc) · 3.03 KB
/
test_header.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from nbformat.v4.nbbase import new_notebook, new_raw_cell, new_markdown_cell
from jupytext.compare import compare
import jupytext
from jupytext.header import uncomment_line, header_to_metadata_and_cell, metadata_and_cell_to_header
from jupytext.formats import get_format_implementation
def test_uncomment():
assert uncomment_line('# line one', '#') == 'line one'
assert uncomment_line('#line two', '#') == 'line two'
assert uncomment_line('#line two', '') == '#line two'
def test_header_to_metadata_and_cell_blank_line():
text = """---
title: Sample header
---
Header is followed by a blank line
"""
lines = text.splitlines()
metadata, _, cell, pos = header_to_metadata_and_cell(lines, '')
assert metadata == {}
assert cell.cell_type == 'raw'
assert cell.source == """---
title: Sample header
---"""
assert cell.metadata == {}
assert lines[pos].startswith('Header is')
def test_header_to_metadata_and_cell_no_blank_line():
text = """---
title: Sample header
---
Header is not followed by a blank line
"""
lines = text.splitlines()
metadata, _, cell, pos = header_to_metadata_and_cell(lines, '')
assert metadata == {}
assert cell.cell_type == 'raw'
assert cell.source == """---
title: Sample header
---"""
assert cell.metadata == {'lines_to_next_cell': 0}
assert lines[pos].startswith('Header is')
def test_header_to_metadata_and_cell_metadata():
text = """---
title: Sample header
jupyter:
mainlanguage: python
---
"""
lines = text.splitlines()
metadata, _, cell, pos = header_to_metadata_and_cell(lines, '')
assert metadata == {'mainlanguage': 'python'}
assert cell.cell_type == 'raw'
assert cell.source == """---
title: Sample header
---"""
assert cell.metadata == {'lines_to_next_cell': 0}
assert pos == len(lines)
def test_metadata_and_cell_to_header(no_jupytext_version_number):
metadata = {'jupytext': {'mainlanguage': 'python'}}
nb = new_notebook(
metadata=metadata,
cells=[new_raw_cell(source="---\ntitle: Sample header\n---")])
header, lines_to_next_cell = metadata_and_cell_to_header(nb, metadata, get_format_implementation('.md'), '.md')
assert '\n'.join(header) == """---
title: Sample header
jupyter:
jupytext:
mainlanguage: python
---"""
assert nb.cells == []
assert lines_to_next_cell is None
def test_metadata_and_cell_to_header2(no_jupytext_version_number):
nb = new_notebook(cells=[new_markdown_cell(source="Some markdown\ntext")])
header, lines_to_next_cell = metadata_and_cell_to_header(nb, {}, get_format_implementation('.md'), '.md')
assert header == []
assert len(nb.cells) == 1
assert lines_to_next_cell is None
def test_notebook_from_plain_script_has_metadata_filter(script="""print('Hello world")
"""):
nb = jupytext.reads(script, '.py')
assert nb.metadata.get('jupytext', {}).get('notebook_metadata_filter') == '-all'
assert nb.metadata.get('jupytext', {}).get('cell_metadata_filter') == '-all'
scripts2 = jupytext.writes(nb, '.py')
compare(script, scripts2)