-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathhelper.test.ts
169 lines (141 loc) · 8.27 KB
/
helper.test.ts
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// tslint:disable:no-multiline-string no-trailing-whitespace
import { expect } from 'chai';
import { EOL } from 'os';
import * as TypeMoq from 'typemoq';
import { Range, Selection, TextDocument, TextEditor, TextLine, Uri } from 'vscode';
import { IApplicationShell, IDocumentManager } from '../../../client/common/application/types';
import { PythonLanguage } from '../../../client/common/constants';
import { CodeExecutionHelper } from '../../../client/terminals/codeExecution/helper';
import { ICodeExecutionHelper } from '../../../client/terminals/types';
// tslint:disable-next-line:max-func-body-length
suite('Terminal - Code Execution Helper', () => {
let documentManager: TypeMoq.IMock<IDocumentManager>;
let applicationShell: TypeMoq.IMock<IApplicationShell>;
let helper: ICodeExecutionHelper;
let document: TypeMoq.IMock<TextDocument>;
let editor: TypeMoq.IMock<TextEditor>;
setup(() => {
documentManager = TypeMoq.Mock.ofType<IDocumentManager>();
applicationShell = TypeMoq.Mock.ofType<IApplicationShell>();
helper = new CodeExecutionHelper(documentManager.object, applicationShell.object);
document = TypeMoq.Mock.ofType<TextDocument>();
editor = TypeMoq.Mock.ofType<TextEditor>();
editor.setup(e => e.document).returns(() => document.object);
});
test('Ensure blank lines are removed', async () => {
const code = ['import sys', '', '', '', 'print(sys.executable)', '', 'print("1234")', '', '', 'print(1)', 'print(2)'];
const expectedCode = code.filter(line => line.trim().length > 0).join(EOL);
const normalizedZCode = helper.normalizeLines(code.join(EOL));
expect(normalizedZCode).to.be.equal(expectedCode);
});
test('Display message if there\s no active file', async () => {
documentManager.setup(doc => doc.activeTextEditor).returns(() => undefined);
const uri = await helper.getFileToExecute();
expect(uri).to.be.an('undefined');
applicationShell.verify(a => a.showErrorMessage(TypeMoq.It.isAnyString()), TypeMoq.Times.once());
});
test('Display message if active file is unsaved', async () => {
documentManager.setup(doc => doc.activeTextEditor).returns(() => editor.object);
document.setup(doc => doc.isUntitled).returns(() => true);
const uri = await helper.getFileToExecute();
expect(uri).to.be.an('undefined');
applicationShell.verify(a => a.showErrorMessage(TypeMoq.It.isAnyString()), TypeMoq.Times.once());
});
test('Display message if active file is non-python', async () => {
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.languageId).returns(() => 'html');
documentManager.setup(doc => doc.activeTextEditor).returns(() => editor.object);
const uri = await helper.getFileToExecute();
expect(uri).to.be.an('undefined');
applicationShell.verify(a => a.showErrorMessage(TypeMoq.It.isAnyString()), TypeMoq.Times.once());
});
test('Returns file uri', async () => {
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.languageId).returns(() => PythonLanguage.language);
const expectedUri = Uri.file('one.py');
document.setup(doc => doc.uri).returns(() => expectedUri);
documentManager.setup(doc => doc.activeTextEditor).returns(() => editor.object);
const uri = await helper.getFileToExecute();
expect(uri).to.be.deep.equal(expectedUri);
});
test('Returns file uri even if saving fails', async () => {
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.isDirty).returns(() => true);
document.setup(doc => doc.languageId).returns(() => PythonLanguage.language);
document.setup(doc => doc.save()).returns(() => Promise.resolve(false));
const expectedUri = Uri.file('one.py');
document.setup(doc => doc.uri).returns(() => expectedUri);
documentManager.setup(doc => doc.activeTextEditor).returns(() => editor.object);
const uri = await helper.getFileToExecute();
expect(uri).to.be.deep.equal(expectedUri);
});
test('Dirty files are saved', async () => {
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.isDirty).returns(() => true);
document.setup(doc => doc.languageId).returns(() => PythonLanguage.language);
const expectedUri = Uri.file('one.py');
document.setup(doc => doc.uri).returns(() => expectedUri);
documentManager.setup(doc => doc.activeTextEditor).returns(() => editor.object);
const uri = await helper.getFileToExecute();
expect(uri).to.be.deep.equal(expectedUri);
document.verify(doc => doc.save(), TypeMoq.Times.once());
});
test('Non-Dirty files are not-saved', async () => {
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.isDirty).returns(() => false);
document.setup(doc => doc.languageId).returns(() => PythonLanguage.language);
const expectedUri = Uri.file('one.py');
document.setup(doc => doc.uri).returns(() => expectedUri);
documentManager.setup(doc => doc.activeTextEditor).returns(() => editor.object);
const uri = await helper.getFileToExecute();
expect(uri).to.be.deep.equal(expectedUri);
document.verify(doc => doc.save(), TypeMoq.Times.never());
});
test('Returns current line if nothing is selected', async () => {
const lineContents = 'Line Contents';
editor.setup(e => e.selection).returns(() => new Selection(3, 0, 3, 0));
const textLine = TypeMoq.Mock.ofType<TextLine>();
textLine.setup(t => t.text).returns(() => lineContents);
document.setup(d => d.lineAt(TypeMoq.It.isAny())).returns(() => textLine.object);
const content = await helper.getSelectedTextToExecute(editor.object);
expect(content).to.be.equal(lineContents);
});
test('Returns selected text', async () => {
const lineContents = 'Line Contents';
editor.setup(e => e.selection).returns(() => new Selection(3, 0, 10, 5));
const textLine = TypeMoq.Mock.ofType<TextLine>();
textLine.setup(t => t.text).returns(() => lineContents);
document.setup(d => d.getText(TypeMoq.It.isAny())).returns((r: Range) => `${r.start.line}.${r.start.character}.${r.end.line}.${r.end.character}`);
const content = await helper.getSelectedTextToExecute(editor.object);
expect(content).to.be.equal('3.0.10.5');
});
test('saveFileIfDirty will not fail if file is not opened', async () => {
documentManager.setup(d => d.textDocuments).returns(() => []).verifiable(TypeMoq.Times.once());
await helper.saveFileIfDirty(Uri.file(`${__filename}.py`));
documentManager.verifyAll();
});
test('File will be saved if file is dirty', async () => {
documentManager.setup(d => d.textDocuments).returns(() => [document.object]).verifiable(TypeMoq.Times.once());
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.isDirty).returns(() => true);
document.setup(doc => doc.languageId).returns(() => PythonLanguage.language);
const expectedUri = Uri.file('one.py');
document.setup(doc => doc.uri).returns(() => expectedUri);
await helper.saveFileIfDirty(expectedUri);
documentManager.verifyAll();
document.verify(doc => doc.save(), TypeMoq.Times.once());
});
test('File will be not saved if file is not dirty', async () => {
documentManager.setup(d => d.textDocuments).returns(() => [document.object]).verifiable(TypeMoq.Times.once());
document.setup(doc => doc.isUntitled).returns(() => false);
document.setup(doc => doc.isDirty).returns(() => false);
document.setup(doc => doc.languageId).returns(() => PythonLanguage.language);
const expectedUri = Uri.file('one.py');
document.setup(doc => doc.uri).returns(() => expectedUri);
await helper.saveFileIfDirty(expectedUri);
documentManager.verifyAll();
document.verify(doc => doc.save(), TypeMoq.Times.never());
});
});