forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatascience.unit.test.ts
More file actions
232 lines (218 loc) · 11.6 KB
/
datascience.unit.test.ts
File metadata and controls
232 lines (218 loc) · 11.6 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
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
// 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 { formatStreamText, stripComments } from '../../client/datascience/common';
import { InputHistory } from '../../datascience-ui/history-react/inputHistory';
// tslint:disable: max-func-body-length
suite('Data Science Tests', () => {
test('formatting stream text', async () => {
assert.equal(formatStreamText('\rExecute\rExecute 1'), 'Execute 1');
assert.equal(formatStreamText('\rExecute\r\nExecute 2'), 'Execute\nExecute 2');
assert.equal(formatStreamText('\rExecute\rExecute\r\nExecute 3'), 'Execute\nExecute 3');
assert.equal(formatStreamText('\rExecute\rExecute\nExecute 4'), 'Execute\nExecute 4');
assert.equal(formatStreamText('\rExecute\r\r \r\rExecute\nExecute 5'), 'Execute\nExecute 5');
assert.equal(formatStreamText('\rExecute\rExecute\nExecute 6\rExecute 7'), 'Execute\nExecute 7');
assert.equal(formatStreamText('\rExecute\rExecute\nExecute 8\rExecute 9\r\r'), 'Execute\n');
assert.equal(formatStreamText('\rExecute\rExecute\nExecute 10\rExecute 11\r\n'), 'Execute\nExecute 11\n');
});
test('input history', async () => {
let history = new InputHistory();
history.add('1', true);
history.add('2', true);
history.add('3', true);
history.add('4', true);
assert.equal(history.completeDown('5'), '5');
history.add('5', true);
assert.equal(history.completeUp(''), '5');
history.add('5', false);
assert.equal(history.completeUp('5'), '5');
assert.equal(history.completeUp('4'), '4');
assert.equal(history.completeUp('2'), '3');
assert.equal(history.completeUp('1'), '2');
assert.equal(history.completeUp(''), '1');
// Add should reset position.
history.add('6', true);
assert.equal(history.completeUp(''), '6');
assert.equal(history.completeUp(''), '5');
assert.equal(history.completeUp(''), '4');
assert.equal(history.completeUp(''), '3');
assert.equal(history.completeUp(''), '2');
assert.equal(history.completeUp(''), '1');
history = new InputHistory();
history.add('1', true);
history.add('2', true);
history.add('3', true);
history.add('4', true);
assert.equal(history.completeDown('5'), '5');
assert.equal(history.completeDown(''), '');
assert.equal(history.completeUp('1'), '4');
assert.equal(history.completeDown('4'), '4');
assert.equal(history.completeDown('4'), '4');
assert.equal(history.completeUp('1'), '3');
assert.equal(history.completeUp('4'), '2');
assert.equal(history.completeDown('3'), '3');
assert.equal(history.completeDown(''), '4');
assert.equal(history.completeUp(''), '3');
assert.equal(history.completeUp(''), '2');
assert.equal(history.completeUp(''), '1');
assert.equal(history.completeUp(''), '');
assert.equal(history.completeUp('1'), '1');
assert.equal(history.completeDown('1'), '2');
assert.equal(history.completeDown('2'), '3');
assert.equal(history.completeDown('3'), '4');
assert.equal(history.completeDown(''), '');
history.add('5', true);
assert.equal(history.completeUp('1'), '5');
assert.equal(history.completeUp('1'), '4');
assert.equal(history.completeUp('1'), '3');
history.add('3', false);
assert.equal(history.completeUp('1'), '3');
assert.equal(history.completeUp('1'), '2');
assert.equal(history.completeUp('1'), '1');
assert.equal(history.completeDown('1'), '2');
assert.equal(history.completeUp('1'), '1');
assert.equal(history.completeDown('1'), '2');
assert.equal(history.completeDown('1'), '3');
assert.equal(history.completeDown('1'), '4');
assert.equal(history.completeDown('1'), '5');
assert.equal(history.completeDown('1'), '3');
});
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');
});
});