-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathtest_active_cells.py
235 lines (199 loc) · 8.14 KB
/
test_active_cells.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import pytest
from jupytext.compare import compare
import jupytext
from .utils import skip_if_dict_is_not_ordered
HEADER = {'.py': '''# ---
# jupyter:
# jupytext:
# main_language: python
# ---
''',
'.R': '''# ---
# jupyter:
# jupytext:
# main_language: python
# ---
''',
'.Rmd': '''---
jupyter:
jupytext:
main_language: python
---
'''}
ACTIVE_ALL = {'.py': """# + {"active": "ipynb,py,R,Rmd"}
# This cell is active in all extensions
""",
'.Rmd': """```{python active="ipynb,py,R,Rmd"}
# This cell is active in all extensions
```
""",
'.R': """# + {"active": "ipynb,py,R,Rmd"}
# This cell is active in all extensions
""",
'.ipynb': {'cell_type': 'code',
'source': '# This cell is active in all extensions',
'metadata': {'active': 'ipynb,py,R,Rmd'},
'execution_count': None,
'outputs': []}}
@pytest.mark.parametrize('ext', ['.Rmd', '.py', '.R'])
def test_active_all(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_ALL[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_ALL[ext], jupytext.writes(nb, ext))
compare(nb.cells[0], ACTIVE_ALL['.ipynb'])
ACTIVE_IPYNB = {'.py': """# + {"active": "ipynb"}
# # This cell is active only in ipynb
# %matplotlib inline
""",
'.Rmd': """```{python active="ipynb", eval=FALSE}
# This cell is active only in ipynb
%matplotlib inline
```
""",
'.R': """# + {"active": "ipynb"}
# # This cell is active only in ipynb
# %matplotlib inline
""",
'.ipynb': {'cell_type': 'code',
'source': '# This cell is active only in ipynb\n'
'%matplotlib inline',
'metadata': {'active': 'ipynb'},
'execution_count': None,
'outputs': []}}
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('ext', ['.Rmd', '.py', '.R'])
def test_active_ipynb(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_IPYNB[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_IPYNB[ext], jupytext.writes(nb, ext))
compare(nb.cells[0], ACTIVE_IPYNB['.ipynb'])
ACTIVE_IPYNB_RMD_USING_TAG = {'.py': """# + {"tags": ["active-ipynb-Rmd"]}
# # This cell is active only in ipynb and Rmd
# %matplotlib inline
""",
'.Rmd': """```{python tags=c("active-ipynb-Rmd")}
# This cell is active only in ipynb and Rmd
# %matplotlib inline
```
""",
'.R': """# + {"tags": ["active-ipynb-Rmd"]}
# # This cell is active only in ipynb and Rmd
# %matplotlib inline
""",
'.ipynb': {'cell_type': 'code',
'source': '# This cell is active only in ipynb and Rmd\n'
'%matplotlib inline',
'metadata': {'tags': ['active-ipynb-Rmd']},
'execution_count': None,
'outputs': []}}
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('ext', ['.Rmd', '.py', '.R'])
def test_active_ipynb_rmd_using_tags(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_IPYNB_RMD_USING_TAG[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_IPYNB_RMD_USING_TAG[ext], jupytext.writes(nb, ext))
compare(nb.cells[0], ACTIVE_IPYNB_RMD_USING_TAG['.ipynb'])
ACTIVE_IPYNB_RSPIN = {'.R': """#+ active="ipynb", eval=FALSE
# # This cell is active only in ipynb
# 1 + 1
""",
'.ipynb': {'cell_type': 'code',
'source': '# This cell is active only in ipynb\n'
'1 + 1',
'metadata': {'active': 'ipynb'},
'execution_count': None,
'outputs': []}}
@skip_if_dict_is_not_ordered
def test_active_ipynb_rspin(no_jupytext_version_number):
nb = jupytext.reads(ACTIVE_IPYNB_RSPIN['.R'], 'R:spin')
assert len(nb.cells) == 1
compare(ACTIVE_IPYNB_RSPIN['.R'], jupytext.writes(nb, 'R:spin'))
compare(nb.cells[0], ACTIVE_IPYNB_RSPIN['.ipynb'])
ACTIVE_PY_IPYNB = {'.py': """# + {"active": "ipynb,py"}
# This cell is active in py and ipynb extensions
""",
'.Rmd': """```{python active="ipynb,py", eval=FALSE}
# This cell is active in py and ipynb extensions
```
""",
'.R': """# + {"active": "ipynb,py"}
# # This cell is active in py and ipynb extensions
""",
'.ipynb': {'cell_type': 'code',
'source': '# This cell is active in py and '
'ipynb extensions',
'metadata': {'active': 'ipynb,py'},
'execution_count': None,
'outputs': []}}
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('ext', ['.Rmd', '.py', '.R'])
def test_active_py_ipynb(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_PY_IPYNB[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_PY_IPYNB[ext], jupytext.writes(nb, ext))
compare(nb.cells[0], ACTIVE_PY_IPYNB['.ipynb'])
ACTIVE_PY_R_IPYNB = {'.py': """# + {"active": "ipynb,py,R"}
# This cell is active in py, R and ipynb extensions
""",
'.Rmd': """```{python active="ipynb,py,R", eval=FALSE}
# This cell is active in py, R and ipynb extensions
```
""",
'.R': """# + {"active": "ipynb,py,R"}
# This cell is active in py, R and ipynb extensions
""",
'.ipynb': {'cell_type': 'code',
'source': '# This cell is active in py, R and '
'ipynb extensions',
'metadata': {'active': 'ipynb,py,R'},
'execution_count': None,
'outputs': []}}
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('ext', ['.Rmd', '.py', '.R'])
def test_active_py_r_ipynb(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_PY_R_IPYNB[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_PY_R_IPYNB[ext], jupytext.writes(nb, ext))
compare(nb.cells[0], ACTIVE_PY_R_IPYNB['.ipynb'])
ACTIVE_RMD = {'.py': """# + {"active": "Rmd"}
# # This cell is active in Rmd only
""",
'.Rmd': """```{python active="Rmd"}
# This cell is active in Rmd only
```
""",
'.R': """# + {"active": "Rmd"}
# # This cell is active in Rmd only
""",
'.ipynb': {'cell_type': 'raw',
'source': '# This cell is active in Rmd only',
'metadata': {'active': 'Rmd'}}}
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('ext', ['.Rmd', '.py', '.R'])
def test_active_rmd(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_RMD[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_RMD[ext], jupytext.writes(nb, ext))
compare(nb.cells[0], ACTIVE_RMD['.ipynb'])
ACTIVE_NOT_INCLUDE_RMD = {'.py': """# + {"hide_output": true, "active": "Rmd"}
# # This cell is active in Rmd only
""",
'.Rmd': """```{python include=FALSE, active="Rmd"}
# This cell is active in Rmd only
```
""",
'.R': """# + {"hide_output": true, "active": "Rmd"}
# # This cell is active in Rmd only
""",
'.ipynb':
{'cell_type': 'raw',
'source': '# This cell is active in Rmd only',
'metadata': {'active': 'Rmd',
'hide_output': True}}}
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('ext', ['.Rmd', '.py', '.R'])
def test_active_not_include_rmd(ext, no_jupytext_version_number):
nb = jupytext.reads(ACTIVE_NOT_INCLUDE_RMD[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_NOT_INCLUDE_RMD[ext], jupytext.writes(nb, ext))
compare(nb.cells[0], ACTIVE_NOT_INCLUDE_RMD['.ipynb'])