filters.test.ts 7.1 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import * as assert from 'assert';
8
import { IFilter, or, matchesPrefix, matchesStrictPrefix, matchesCamelCase, matchesSubString, matchesContiguousSubString, matchesWords } from 'vs/base/common/filters';
E
Erich Gamma 已提交
9 10

function filterOk(filter: IFilter, word: string, wordToMatchAgainst: string, highlights?: { start: number; end: number; }[]) {
11
	let r = filter(word, wordToMatchAgainst);
E
Erich Gamma 已提交
12 13 14 15 16 17 18 19 20 21
	assert(r);
	if (highlights) {
		assert.deepEqual(r, highlights);
	}
}

function filterNotOk(filter, word, suggestion) {
	assert(!filter(word, suggestion));
}

22 23
suite('Filters', () => {
	test('or', function () {
24 25
		let filter, counters;
		let newFilter = function (i, r) {
E
Erich Gamma 已提交
26 27 28
			return function () { counters[i]++; return r; };
		};

J
Johannes Rieken 已提交
29
		counters = [0, 0];
E
Erich Gamma 已提交
30
		filter = or(newFilter(0, false), newFilter(1, false));
31
		filterNotOk(filter, 'anything', 'anything');
J
Johannes Rieken 已提交
32
		assert.deepEqual(counters, [1, 1]);
E
Erich Gamma 已提交
33

J
Johannes Rieken 已提交
34
		counters = [0, 0];
E
Erich Gamma 已提交
35
		filter = or(newFilter(0, true), newFilter(1, false));
36
		filterOk(filter, 'anything', 'anything');
J
Johannes Rieken 已提交
37
		assert.deepEqual(counters, [1, 0]);
E
Erich Gamma 已提交
38

J
Johannes Rieken 已提交
39
		counters = [0, 0];
E
Erich Gamma 已提交
40
		filter = or(newFilter(0, true), newFilter(1, true));
41
		filterOk(filter, 'anything', 'anything');
J
Johannes Rieken 已提交
42
		assert.deepEqual(counters, [1, 0]);
E
Erich Gamma 已提交
43

J
Johannes Rieken 已提交
44
		counters = [0, 0];
E
Erich Gamma 已提交
45
		filter = or(newFilter(0, false), newFilter(1, true));
46
		filterOk(filter, 'anything', 'anything');
J
Johannes Rieken 已提交
47
		assert.deepEqual(counters, [1, 1]);
E
Erich Gamma 已提交
48 49
	});

50 51 52 53 54 55 56 57 58 59
	test('PrefixFilter - case sensitive', function () {
		filterNotOk(matchesStrictPrefix, '', '');
		filterOk(matchesStrictPrefix, '', 'anything', []);
		filterOk(matchesStrictPrefix, 'alpha', 'alpha', [{ start: 0, end: 5 }]);
		filterOk(matchesStrictPrefix, 'alpha', 'alphasomething', [{ start: 0, end: 5 }]);
		filterNotOk(matchesStrictPrefix, 'alpha', 'alp');
		filterOk(matchesStrictPrefix, 'a', 'alpha', [{ start: 0, end: 1 }]);
		filterNotOk(matchesStrictPrefix, 'x', 'alpha');
		filterNotOk(matchesStrictPrefix, 'A', 'alpha');
		filterNotOk(matchesStrictPrefix, 'AlPh', 'alPHA');
E
Erich Gamma 已提交
60 61
	});

62 63 64 65 66
	test('PrefixFilter - ignore case', function () {
		filterOk(matchesPrefix, 'alpha', 'alpha', [{ start: 0, end: 5 }]);
		filterOk(matchesPrefix, 'alpha', 'alphasomething', [{ start: 0, end: 5 }]);
		filterNotOk(matchesPrefix, 'alpha', 'alp');
		filterOk(matchesPrefix, 'a', 'alpha', [{ start: 0, end: 1 }]);
67
		filterOk(matchesPrefix, 'ä', 'Älpha', [{ start: 0, end: 1 }]);
68 69 70
		filterNotOk(matchesPrefix, 'x', 'alpha');
		filterOk(matchesPrefix, 'A', 'alpha', [{ start: 0, end: 1 }]);
		filterOk(matchesPrefix, 'AlPh', 'alPHA', [{ start: 0, end: 4 }]);
E
Erich Gamma 已提交
71 72
	});

73 74 75 76 77 78 79
	test('CamelCaseFilter', function () {
		filterNotOk(matchesCamelCase, '', '');
		filterOk(matchesCamelCase, '', 'anything', []);
		filterOk(matchesCamelCase, 'alpha', 'alpha', [{ start: 0, end: 5 }]);
		filterOk(matchesCamelCase, 'AlPhA', 'alpha', [{ start: 0, end: 5 }]);
		filterOk(matchesCamelCase, 'alpha', 'alphasomething', [{ start: 0, end: 5 }]);
		filterNotOk(matchesCamelCase, 'alpha', 'alp');
E
Erich Gamma 已提交
80

81
		filterOk(matchesCamelCase, 'c', 'CamelCaseRocks', [
E
Erich Gamma 已提交
82 83
			{ start: 0, end: 1 }
		]);
84
		filterOk(matchesCamelCase, 'cc', 'CamelCaseRocks', [
E
Erich Gamma 已提交
85 86 87
			{ start: 0, end: 1 },
			{ start: 5, end: 6 }
		]);
88
		filterOk(matchesCamelCase, 'ccr', 'CamelCaseRocks', [
E
Erich Gamma 已提交
89 90 91 92
			{ start: 0, end: 1 },
			{ start: 5, end: 6 },
			{ start: 9, end: 10 }
		]);
93
		filterOk(matchesCamelCase, 'cacr', 'CamelCaseRocks', [
E
Erich Gamma 已提交
94 95 96 97
			{ start: 0, end: 2 },
			{ start: 5, end: 6 },
			{ start: 9, end: 10 }
		]);
98
		filterOk(matchesCamelCase, 'cacar', 'CamelCaseRocks', [
E
Erich Gamma 已提交
99 100 101 102
			{ start: 0, end: 2 },
			{ start: 5, end: 7 },
			{ start: 9, end: 10 }
		]);
103
		filterOk(matchesCamelCase, 'ccarocks', 'CamelCaseRocks', [
E
Erich Gamma 已提交
104 105 106 107
			{ start: 0, end: 1 },
			{ start: 5, end: 7 },
			{ start: 9, end: 14 }
		]);
108
		filterOk(matchesCamelCase, 'cr', 'CamelCaseRocks', [
E
Erich Gamma 已提交
109 110 111
			{ start: 0, end: 1 },
			{ start: 9, end: 10 }
		]);
112
		filterOk(matchesCamelCase, 'fba', 'FooBarAbe', [
E
Erich Gamma 已提交
113 114 115
			{ start: 0, end: 1 },
			{ start: 3, end: 5 }
		]);
116
		filterOk(matchesCamelCase, 'fbar', 'FooBarAbe', [
E
Erich Gamma 已提交
117 118 119
			{ start: 0, end: 1 },
			{ start: 3, end: 6 }
		]);
120
		filterOk(matchesCamelCase, 'fbara', 'FooBarAbe', [
E
Erich Gamma 已提交
121 122 123
			{ start: 0, end: 1 },
			{ start: 3, end: 7 }
		]);
124
		filterOk(matchesCamelCase, 'fbaa', 'FooBarAbe', [
E
Erich Gamma 已提交
125 126 127 128
			{ start: 0, end: 1 },
			{ start: 3, end: 5 },
			{ start: 6, end: 7 }
		]);
129
		filterOk(matchesCamelCase, 'fbaab', 'FooBarAbe', [
E
Erich Gamma 已提交
130 131 132 133
			{ start: 0, end: 1 },
			{ start: 3, end: 5 },
			{ start: 6, end: 8 }
		]);
134
		filterOk(matchesCamelCase, 'c2d', 'canvasCreation2D', [
E
Erich Gamma 已提交
135 136 137
			{ start: 0, end: 1 },
			{ start: 14, end: 16 }
		]);
138
		filterOk(matchesCamelCase, 'cce', '_canvasCreationEvent', [
E
Erich Gamma 已提交
139 140 141 142 143 144
			{ start: 1, end: 2 },
			{ start: 7, end: 8 },
			{ start: 15, end: 16 }
		]);
	});

145
	test('CamelCaseFilter - #19256', function () {
E
Erich Gamma 已提交
146 147 148 149
		assert(matchesCamelCase('Debug Console', 'Open: Debug Console'));
		assert(matchesCamelCase('Debug console', 'Open: Debug Console'));
		assert(matchesCamelCase('debug console', 'Open: Debug Console'));
	});
B
Benjamin Pasero 已提交
150

151 152
	test('matchesContiguousSubString', function () {
		filterOk(matchesContiguousSubString, 'cela', 'cancelAnimationFrame()', [
B
Benjamin Pasero 已提交
153 154 155 156
			{ start: 3, end: 7 }
		]);
	});

157 158
	test('matchesSubString', function () {
		filterOk(matchesSubString, 'cmm', 'cancelAnimationFrame()', [
B
Benjamin Pasero 已提交
159 160 161 162 163
			{ start: 0, end: 1 },
			{ start: 9, end: 10 },
			{ start: 18, end: 19 }
		]);
	});
164 165 166 167 168 169 170 171 172 173 174

	test('WordFilter', function () {
		filterOk(matchesWords, 'alpha', 'alpha', [{ start: 0, end: 5 }]);
		filterOk(matchesWords, 'alpha', 'alphasomething', [{ start: 0, end: 5 }]);
		filterNotOk(matchesWords, 'alpha', 'alp');
		filterOk(matchesWords, 'a', 'alpha', [{ start: 0, end: 1 }]);
		filterNotOk(matchesWords, 'x', 'alpha');
		filterOk(matchesWords, 'A', 'alpha', [{ start: 0, end: 1 }]);
		filterOk(matchesWords, 'AlPh', 'alPHA', [{ start: 0, end: 4 }]);
		assert(matchesWords('Debug Console', 'Open: Debug Console'));

J
Johannes Rieken 已提交
175 176 177
		filterOk(matchesWords, 'gp', 'Git: Pull', [{ start: 0, end: 1 }, { start: 5, end: 6 }]);
		filterOk(matchesWords, 'g p', 'Git: Pull', [{ start: 0, end: 1 }, { start: 4, end: 6 }]);
		filterOk(matchesWords, 'gipu', 'Git: Pull', [{ start: 0, end: 2 }, { start: 5, end: 7 }]);
178

J
Johannes Rieken 已提交
179 180 181
		filterOk(matchesWords, 'gp', 'Category: Git: Pull', [{ start: 10, end: 11 }, { start: 15, end: 16 }]);
		filterOk(matchesWords, 'g p', 'Category: Git: Pull', [{ start: 10, end: 11 }, { start: 14, end: 16 }]);
		filterOk(matchesWords, 'gipu', 'Category: Git: Pull', [{ start: 10, end: 12 }, { start: 15, end: 17 }]);
182 183 184 185 186

		filterNotOk(matchesWords, 'it', 'Git: Pull');
		filterNotOk(matchesWords, 'll', 'Git: Pull');

		filterOk(matchesWords, 'git: プル', 'git: プル', [{ start: 0, end: 7 }]);
J
Johannes Rieken 已提交
187
		filterOk(matchesWords, 'git プル', 'git: プル', [{ start: 0, end: 3 }, { start: 4, end: 7 }]);
188 189

		filterOk(matchesWords, 'öäk', 'Öhm: Älles Klar', [{ start: 0, end: 1 }, { start: 5, end: 6 }, { start: 11, end: 12 }]);
S
Sandeep Somavarapu 已提交
190 191 192

		assert.ok(matchesWords('gipu', 'Category: Git: Pull', true) === null);
		assert.deepEqual(matchesWords('pu', 'Category: Git: Pull', true), [{ start: 15, end: 17 }]);
193
	});
E
Erich Gamma 已提交
194
});