forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcellFactory.unit.test.ts
More file actions
204 lines (183 loc) · 8.84 KB
/
cellFactory.unit.test.ts
File metadata and controls
204 lines (183 loc) · 8.84 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { assert } from 'chai';
import { generateCells } from '../../client/datascience/cellFactory';
import { removeLinesFromFrontAndBack, stripComments } from '../../datascience-ui/common';
// tslint:disable: max-func-body-length
suite('DataScience CellFactory', () => {
test('parsing cells', () => {
let cells = generateCells(undefined, '#%%\na=1\na', 'foo', 0, true, '1');
assert.equal(cells.length, 1, 'Simple cell, not right number found');
cells = generateCells(undefined, '#%% [markdown]\na=1\na', 'foo', 0, true, '1');
assert.equal(cells.length, 2, 'Split cell, not right number found');
cells = generateCells(undefined, '#%% [markdown]\n# #a=1\n#a', 'foo', 0, true, '1');
assert.equal(cells.length, 1, 'Markdown split wrong');
assert.equal(cells[0].data.cell_type, 'markdown', 'Markdown cell not generated');
cells = generateCells(undefined, "#%% [markdown]\n'''\n# a\nb\n'''", 'foo', 0, true, '1');
assert.equal(cells.length, 1, 'Markdown cell multline failed');
assert.equal(cells[0].data.cell_type, 'markdown', 'Markdown cell not generated');
assert.equal(cells[0].data.source.length, 2, 'Lines for markdown not emitted');
cells = generateCells(undefined, '#%% [markdown]\n"""\n# a\nb\n"""', 'foo', 0, true, '1');
assert.equal(cells.length, 1, 'Markdown cell multline failed');
assert.equal(cells[0].data.cell_type, 'markdown', 'Markdown cell not generated');
assert.equal(cells[0].data.source.length, 2, 'Lines for markdown not emitted');
cells = generateCells(undefined, '#%% \n"""\n# a\nb\n"""', 'foo', 0, true, '1');
assert.equal(cells.length, 1, 'Code cell multline failed');
assert.equal(cells[0].data.cell_type, 'code', 'Code cell not generated');
assert.equal(cells[0].data.source.length, 5, 'Lines for cell not emitted');
cells = generateCells(undefined, '#%% [markdown] \n"""# a\nb\n"""', 'foo', 0, true, '1');
assert.equal(cells.length, 1, 'Markdown cell multline failed');
assert.equal(cells[0].data.cell_type, 'markdown', 'Markdown cell not generated');
assert.equal(cells[0].data.source.length, 2, 'Lines for cell not emitted');
// tslint:disable-next-line: no-multiline-string
const multilineCode = `#%%
myvar = """ # Lorem Ipsum
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nullam eget varius ligula, eget fermentum mauris.
Cras ultrices, enim sit amet iaculis ornare, nisl nibh aliquet elit, sed ultrices velit ipsum dignissim nisl.
Nunc quis orci ante. Vivamus vel blandit velit.
Sed mattis dui diam, et blandit augue mattis vestibulum.
Suspendisse ornare interdum velit. Suspendisse potenti.
Morbi molestie lacinia sapien nec porttitor. Nam at vestibulum nisi.
"""`;
// tslint:disable-next-line: no-multiline-string
const multilineTwo = `#%%
""" # Lorem Ipsum
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nullam eget varius ligula, eget fermentum mauris.
Cras ultrices, enim sit amet iaculis ornare, nisl nibh aliquet elit, sed ultrices velit ipsum dignissim nisl.
Nunc quis orci ante. Vivamus vel blandit velit.
Sed mattis dui diam, et blandit augue mattis vestibulum.
Suspendisse ornare interdum velit. Suspendisse potenti.
Morbi molestie lacinia sapien nec porttitor. Nam at vestibulum nisi.
""" print('bob')`;
cells = generateCells(undefined, multilineCode, 'foo', 0, true, '1');
assert.equal(cells.length, 1, 'code cell multline failed');
assert.equal(cells[0].data.cell_type, 'code', 'Code cell not generated');
assert.equal(cells[0].data.source.length, 10, 'Lines for cell not emitted');
cells = generateCells(undefined, multilineTwo, 'foo', 0, true, '1');
assert.equal(cells.length, 1, 'code cell multline failed');
assert.equal(cells[0].data.cell_type, 'code', 'Code cell not generated');
assert.equal(cells[0].data.source.length, 10, 'Lines for cell not emitted');
// tslint:disable-next-line: no-multiline-string
assert.equal(cells[0].data.source[9], `""" print('bob')`, 'Lines for cell not emitted');
// tslint:disable-next-line: no-multiline-string
const multilineMarkdown = `#%% [markdown]
# ## Block of Interest
#
# ### Take a look
#
#
# 1. Item 1
#
# - Item 1-a
# 1. Item 1-a-1
# - Item 1-a-1-a
# - Item 1-a-1-b
# 2. Item 1-a-2
# - Item 1-a-2-a
# - Item 1-a-2-b
# 3. Item 1-a-3
# - Item 1-a-3-a
# - Item 1-a-3-b
# - Item 1-a-3-c
#
# 2. Item 2`;
cells = generateCells(undefined, multilineMarkdown, 'foo', 0, true, '1');
assert.equal(cells.length, 1, 'markdown cell multline failed');
assert.equal(cells[0].data.cell_type, 'markdown', 'markdown cell not generated');
assert.equal(cells[0].data.source.length, 20, 'Lines for cell not emitted');
assert.equal(cells[0].data.source[17], ' - Item 1-a-3-c\n', 'Lines for markdown not emitted');
// tslint:disable-next-line: no-multiline-string
const multilineQuoteWithOtherDelimiter = `#%% [markdown]
'''
### Take a look
2. Item 2
""" Not a comment delimiter
'''
`;
cells = generateCells(undefined, multilineQuoteWithOtherDelimiter, 'foo', 0, true, '1');
assert.equal(cells.length, 1, 'markdown cell multline failed');
assert.equal(cells[0].data.cell_type, 'markdown', 'markdown cell not generated');
assert.equal(cells[0].data.source.length, 3, 'Lines for cell not emitted');
assert.equal(cells[0].data.source[2], '""" Not a comment delimiter', 'Lines for markdown not emitted');
// tslint:disable-next-line: no-multiline-string
const multilineQuoteInFunc = `#%%
import requests
def download(url, filename):
""" utility function to download a file """
response = requests.get(url, stream=True)
with open(filename, "wb") as handle:
for data in response.iter_content():
handle.write(data)
`;
cells = generateCells(undefined, multilineQuoteInFunc, 'foo', 0, true, '1');
assert.equal(cells.length, 1, 'cell multline failed');
assert.equal(cells[0].data.cell_type, 'code', 'code cell not generated');
assert.equal(cells[0].data.source.length, 9, 'Lines for cell not emitted');
assert.equal(
cells[0].data.source[3],
' """ utility function to download a file """\n',
'Lines for cell not emitted'
);
// tslint:disable-next-line: no-multiline-string
const multilineMarkdownWithCell = `#%% [markdown]
# # Define a simple class
class Pizza(object):
def __init__(self, size, toppings, price, rating):
self.size = size
self.toppings = toppings
self.price = price
self.rating = rating
`;
cells = generateCells(undefined, multilineMarkdownWithCell, 'foo', 0, true, '1');
assert.equal(cells.length, 2, 'cell split failed');
assert.equal(cells[0].data.cell_type, 'markdown', 'markdown cell not generated');
assert.equal(cells[0].data.source.length, 1, 'Lines for markdown not emitted');
assert.equal(cells[1].data.cell_type, 'code', 'code cell not generated');
assert.equal(cells[1].data.source.length, 7, 'Lines for code not emitted');
assert.equal(cells[1].data.source[3], ' self.toppings = toppings\n', 'Lines for cell not emitted');
// Non comments tests
let nonComments = stripComments(multilineCode);
assert.ok(nonComments.startsWith('myvar = """ # Lorem Ipsum'), 'Variable set to multiline string not working');
nonComments = stripComments(multilineTwo);
assert.equal(nonComments, '', 'Multline comment is not being stripped');
nonComments = stripComments(multilineQuoteInFunc);
assert.equal(nonComments.splitLines().length, 6, 'Splitting quote in func wrong number of lines');
});
test('Line removal', () => {
const entry1 = `# %% CELL
first line`;
const expected1 = `# %% CELL
first line`;
const entry2 = `# %% CELL
first line
`;
const expected2 = `# %% CELL
first line`;
const entry3 = `# %% CELL
first line
second line
`;
const expected3 = `# %% CELL
first line
second line`;
const entry4 = `
if (foo):
print('stuff')
print('some more')
`;
const expected4 = `if (foo):
print('stuff')
print('some more')`;
let removed = removeLinesFromFrontAndBack(entry1);
assert.equal(removed, expected1);
removed = removeLinesFromFrontAndBack(entry2);
assert.equal(removed, expected2);
removed = removeLinesFromFrontAndBack(entry3);
assert.equal(removed, expected3);
removed = removeLinesFromFrontAndBack(entry4);
assert.equal(removed, expected4);
});
});