forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.unit.test.ts
More file actions
117 lines (98 loc) · 6.62 KB
/
provider.unit.test.ts
File metadata and controls
117 lines (98 loc) · 6.62 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
// tslint:disable:no-any
import * as assert from 'assert';
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as path from 'path';
import { anything, instance, mock, verify, when } from 'ts-mockito';
import { CancellationTokenSource, Uri } from 'vscode';
import { CommandManager } from '../../client/common/application/commandManager';
import { ICommandManager } from '../../client/common/application/types';
import { Commands } from '../../client/common/constants';
import { FileSystem } from '../../client/common/platform/fileSystem';
import { IFileSystem } from '../../client/common/platform/types';
import { Generator } from '../../client/workspaceSymbols/generator';
import { WorkspaceSymbolProvider } from '../../client/workspaceSymbols/provider';
use(chaiAsPromised);
const workspaceUri = Uri.file(path.join(__dirname, '..', '..', '..', 'src', 'test'));
// tslint:disable-next-line:max-func-body-length
suite('Workspace Symbols Provider', () => {
let generator: Generator;
let fs: IFileSystem;
let commandManager: ICommandManager;
setup(() => {
fs = mock(FileSystem);
commandManager = mock(CommandManager);
generator = mock(Generator);
});
test('Returns 0 tags without any generators', async () => {
const provider = new WorkspaceSymbolProvider(instance(fs), instance(commandManager), []);
const tags = await provider.provideWorkspaceSymbols('', new CancellationTokenSource().token);
expect(tags).to.be.lengthOf(0);
});
test('Builds tags when a tag file doesn\'t exist', async () => {
const provider = new WorkspaceSymbolProvider(instance(fs), instance(commandManager), [instance(generator)]);
const tagFilePath = 'No existing tagFilePath';
when(generator.tagFilePath).thenReturn(tagFilePath);
when(fs.fileExists(tagFilePath)).thenResolve(false);
when(commandManager.executeCommand(Commands.Build_Workspace_Symbols, true, anything())).thenResolve();
const tags = await provider.provideWorkspaceSymbols('', new CancellationTokenSource().token);
expect(tags).to.be.lengthOf(0);
verify(commandManager.executeCommand(Commands.Build_Workspace_Symbols, true, anything())).once();
});
test('Builds tags when a tag file doesn\'t exist', async () => {
const provider = new WorkspaceSymbolProvider(instance(fs), instance(commandManager), [instance(generator)]);
const tagFilePath = 'No existing tagFilePath';
when(generator.tagFilePath).thenReturn(tagFilePath);
when(fs.fileExists(tagFilePath)).thenResolve(false);
when(commandManager.executeCommand(Commands.Build_Workspace_Symbols, true, anything())).thenResolve();
const tags = await provider.provideWorkspaceSymbols('', new CancellationTokenSource().token);
expect(tags).to.be.lengthOf(0);
verify(commandManager.executeCommand(Commands.Build_Workspace_Symbols, true, anything())).once();
});
test('Symbols should not be returned when disabled', async () => {
const provider = new WorkspaceSymbolProvider(instance(fs), instance(commandManager), [instance(generator)]);
const tagFilePath = 'existing tagFilePath';
when(generator.tagFilePath).thenReturn(tagFilePath);
when(generator.enabled).thenReturn(false);
when(fs.fileExists(tagFilePath)).thenResolve(true);
when(commandManager.executeCommand(Commands.Build_Workspace_Symbols, true, anything())).thenResolve();
const tags = await provider.provideWorkspaceSymbols('', new CancellationTokenSource().token);
expect(tags).to.be.lengthOf(0);
verify(commandManager.executeCommand(Commands.Build_Workspace_Symbols, true, anything())).never();
});
test('symbols should be returned when enabeld and vice versa', async () => {
const provider = new WorkspaceSymbolProvider(instance(fs), instance(commandManager), [instance(generator)]);
const tagFilePath = path.join(workspaceUri.fsPath, '.vscode', 'tags');
when(generator.tagFilePath).thenReturn(tagFilePath);
when(generator.workspaceFolder).thenReturn(workspaceUri);
when(generator.enabled).thenReturn(true);
when(fs.fileExists(tagFilePath)).thenResolve(true);
when(commandManager.executeCommand(Commands.Build_Workspace_Symbols, true, anything())).thenResolve();
const tags = await provider.provideWorkspaceSymbols('', new CancellationTokenSource().token);
expect(tags).to.be.lengthOf(100);
verify(commandManager.executeCommand(Commands.Build_Workspace_Symbols, true, anything())).never();
});
test('symbols should be filtered correctly', async () => {
const provider = new WorkspaceSymbolProvider(instance(fs), instance(commandManager), [instance(generator)]);
const tagFilePath = path.join(workspaceUri.fsPath, '.vscode', 'tags');
when(generator.tagFilePath).thenReturn(tagFilePath);
when(generator.workspaceFolder).thenReturn(workspaceUri);
when(generator.enabled).thenReturn(true);
when(fs.fileExists(tagFilePath)).thenResolve(true);
when(commandManager.executeCommand(Commands.Build_Workspace_Symbols, true, anything())).thenResolve();
const symbols = await provider.provideWorkspaceSymbols('meth1Of', new CancellationTokenSource().token);
expect(symbols).to.be.length.greaterThan(0);
verify(commandManager.executeCommand(Commands.Build_Workspace_Symbols, true, anything())).never();
assert.equal(symbols.length >= 2, true, 'Incorrect number of symbols returned');
assert.notEqual(symbols.findIndex(sym => sym.location.uri.fsPath.endsWith('childFile.py')), -1, 'File with symbol not found in child workspace folder');
assert.notEqual(symbols.findIndex(sym => sym.location.uri.fsPath.endsWith('workspace2File.py')), -1, 'File with symbol not found in child workspace folder');
const symbolsForMeth = await provider.provideWorkspaceSymbols('meth', new CancellationTokenSource().token);
assert.equal(symbolsForMeth.length >= 10, true, 'Incorrect number of symbols returned');
assert.notEqual(symbolsForMeth.findIndex(sym => sym.location.uri.fsPath.endsWith('childFile.py')), -1, 'Symbols not returned for childFile.py');
assert.notEqual(symbolsForMeth.findIndex(sym => sym.location.uri.fsPath.endsWith('workspace2File.py')), -1, 'Symbols not returned for workspace2File.py');
assert.notEqual(symbolsForMeth.findIndex(sym => sym.location.uri.fsPath.endsWith('file.py')), -1, 'Symbols not returned for file.py');
});
});