-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
file-search.spec.js
116 lines (91 loc) · 6.04 KB
/
file-search.spec.js
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
/********************************************************************************
* Copyright (C) 2021 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
// @ts-check
describe('file-search', function () {
const { assert } = chai;
const Uri = require('@theia/core/lib/common/uri');
const { QuickFileOpenService } = require('@theia/file-search/lib/browser/quick-file-open');
const { CancellationTokenSource } = require('@theia/core/lib/common/cancellation');
/** @type {import('inversify').Container} */
const container = window['theia'].container;
const quickFileOpenService = container.get(QuickFileOpenService);
describe('quick-file-open', () => {
describe('#compareItems', () => {
it('should compare two quick-open-items by `label`', () => {
/** @type import ('@theia/file-search/lib/browser/quick-file-open').FileQuickPickItem*/
const a = { label: 'a', uri: new Uri.default('b') };
/** @type import ('@theia/file-search/lib/browser/quick-file-open').FileQuickPickItem*/
const b = { label: 'b', uri: new Uri.default('a') };
assert.equal(quickFileOpenService['compareItems'](a, b), 1, 'a should be before b');
assert.equal(quickFileOpenService['compareItems'](b, a), -1, 'a should be before b');
assert.equal(quickFileOpenService['compareItems'](a, a), 0, 'items should be equal');
});
it('should compare two quick-open-items by `uri`', () => {
/** @type import ('@theia/file-search/lib/browser/quick-file-open').FileQuickPickItem*/
const a = { label: 'a', uri: new Uri.default('a') };
/** @type import ('@theia/file-search/lib/browser/quick-file-open').FileQuickPickItem*/
const b = { label: 'a', uri: new Uri.default('b') };
assert.equal(quickFileOpenService['compareItems'](a, b), 1, 'a should be before b');
assert.equal(quickFileOpenService['compareItems'](b, a), -1, 'a should be before b');
assert.equal(quickFileOpenService['compareItems'](a, a), 0, 'items should be equal');
});
});
describe('#filterAndRange', () => {
it('should return the default when not searching', () => {
const filterAndRange = quickFileOpenService['filterAndRange'];
assert.equal(filterAndRange, quickFileOpenService['filterAndRangeDefault']);
});
it('should update when searching', () => {
quickFileOpenService['getPicks']('a:2:1', new CancellationTokenSource().token); // perform a mock search.
const filterAndRange = quickFileOpenService['filterAndRange'];
assert.equal(filterAndRange.filter, 'a');
assert.deepEqual(filterAndRange.range, { start: { line: 1, character: 0 }, end: { line: 1, character: 0 } });
});
});
describe('#splitFilterAndRange', () => {
const expression1 = 'a:2:1';
const expression2 = 'a:2,1';
const expression3 = 'a:2#2';
const expression4 = 'a#2:2';
const expression5 = 'a#2,1';
const expression6 = 'a#2#2';
const expression7 = 'a:2';
const expression8 = 'a#2';
it('should split the filter correctly for different combinations', () => {
assert.equal((quickFileOpenService['splitFilterAndRange'](expression1).filter), 'a');
assert.equal((quickFileOpenService['splitFilterAndRange'](expression2).filter), 'a');
assert.equal((quickFileOpenService['splitFilterAndRange'](expression3).filter), 'a');
assert.equal((quickFileOpenService['splitFilterAndRange'](expression4).filter), 'a');
assert.equal((quickFileOpenService['splitFilterAndRange'](expression5).filter), 'a');
assert.equal((quickFileOpenService['splitFilterAndRange'](expression6).filter), 'a');
assert.equal((quickFileOpenService['splitFilterAndRange'](expression7).filter), 'a');
assert.equal((quickFileOpenService['splitFilterAndRange'](expression8).filter), 'a');
});
it('should split the range correctly for different combinations', () => {
const rangeTest1 = { start: { line: 1, character: 0 }, end: { line: 1, character: 0 } };
const rangeTest2 = { start: { line: 1, character: 1 }, end: { line: 1, character: 1 } };
assert.deepEqual(quickFileOpenService['splitFilterAndRange'](expression1).range, rangeTest1);
assert.deepEqual(quickFileOpenService['splitFilterAndRange'](expression2).range, rangeTest1);
assert.deepEqual(quickFileOpenService['splitFilterAndRange'](expression3).range, rangeTest2);
assert.deepEqual(quickFileOpenService['splitFilterAndRange'](expression4).range, rangeTest2);
assert.deepEqual(quickFileOpenService['splitFilterAndRange'](expression5).range, rangeTest1);
assert.deepEqual(quickFileOpenService['splitFilterAndRange'](expression6).range, rangeTest2);
assert.deepEqual(quickFileOpenService['splitFilterAndRange'](expression7).range, rangeTest1);
assert.deepEqual(quickFileOpenService['splitFilterAndRange'](expression8).range, rangeTest1);
});
});
});
});