/
test_read_folding_markers.py
146 lines (112 loc) · 3.4 KB
/
test_read_folding_markers.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from jupytext.compare import compare
import jupytext
# region Folding markers as cell boundaries
# region Sub-region with metadata {"key": "value"}
def test_mark_cell_with_vim_folding_markers(
script="""# This is a markdown cell
# {{{ And this is a foldable code region with metadata {"key": "value"}
a = 1
b = 2
c = 3
# }}}
""",
):
nb = jupytext.reads(script, "py")
assert nb.metadata["jupytext"]["cell_markers"] == "{{{,}}}"
assert len(nb.cells) == 2
assert nb.cells[0].cell_type == "markdown"
assert nb.cells[0].source == "This is a markdown cell"
assert nb.cells[1].cell_type == "code"
assert nb.cells[1].source == "a = 1\n\nb = 2\n\nc = 3"
assert nb.cells[1].metadata == {
"title": "And this is a foldable code region with metadata",
"key": "value",
}
script2 = jupytext.writes(nb, "py")
compare(script2, script)
# endregion
def test_mark_cell_with_vscode_pycharm_folding_markers(
script="""# This is a markdown cell
# region And this is a foldable code region with metadata {"key": "value"}
a = 1
b = 2
c = 3
# endregion
""",
):
nb = jupytext.reads(script, "py")
assert len(nb.cells) == 2
assert nb.cells[0].cell_type == "markdown"
assert nb.cells[0].source == "This is a markdown cell"
assert nb.cells[1].cell_type == "code"
assert nb.cells[1].source == "a = 1\n\nb = 2\n\nc = 3"
assert nb.cells[1].metadata == {
"title": "And this is a foldable code region with metadata",
"key": "value",
}
script2 = jupytext.writes(nb, "py")
compare(script2, script)
def test_mark_cell_with_no_title_and_inner_region(
script="""# This is a markdown cell
# region {"key": "value"}
a = 1
# region An inner region
b = 2
# endregion
def f(x):
return x + 1
# endregion
d = 4
""",
):
nb = jupytext.reads(script, "py")
assert nb.cells[0].cell_type == "markdown"
assert nb.cells[0].source == "This is a markdown cell"
assert nb.cells[1].cell_type == "code"
assert nb.cells[1].source == '# region {"key": "value"}\na = 1'
assert nb.cells[2].cell_type == "code"
assert nb.cells[2].metadata["title"] == "An inner region"
assert nb.cells[2].source == "b = 2"
assert nb.cells[3].cell_type == "code"
assert nb.cells[3].source == "def f(x):\n return x + 1"
assert nb.cells[4].cell_type == "code"
assert nb.cells[4].source == "# endregion"
assert nb.cells[5].cell_type == "code"
assert nb.cells[5].source == "d = 4"
assert len(nb.cells) == 6
script2 = jupytext.writes(nb, "py")
compare(script2, script)
# endregion
def test_adjacent_regions(
script="""# region global
# region innermost
a = 1
b = 2
# endregion
# endregion
""",
):
nb = jupytext.reads(script, "py")
assert len(nb.cells) == 3
assert nb.cells[0].cell_type == "code"
assert nb.cells[0].source == "# region global"
assert nb.cells[1].cell_type == "code"
assert nb.cells[1].source == "a = 1\n\nb = 2"
assert nb.cells[2].cell_type == "code"
assert nb.cells[2].source == "# endregion"
script2 = jupytext.writes(nb, "py")
compare(script2, script)
def test_indented_markers_are_ignored(
script="""# region global
# region indented
a = 1
b = 2
# endregion
# endregion
""",
):
nb = jupytext.reads(script, "py")
assert len(nb.cells) == 1
assert nb.cells[0].cell_type == "code"
script2 = jupytext.writes(nb, "py")
compare(script2, script)