searchModel.test.ts 12.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 9 10 11 12 13
import * as sinon from 'sinon';
import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils';
import { DeferredPPromise } from 'vs/test/utils/promiseTestUtils';
import { PPromise } from 'vs/base/common/winjs.base';
import { nullEvent } from 'vs/base/common/timer';
import { SearchModel } from 'vs/workbench/parts/search/common/searchModel';
J
Johannes Rieken 已提交
14
import URI from 'vs/base/common/uri';
15
import {IFileMatch, ILineMatch} from 'vs/platform/search/common/search';
16 17 18
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { ISearchService, ISearchComplete, ISearchProgressItem } from 'vs/platform/search/common/search';
import { Range } from 'vs/editor/common/core/range';
19

20
suite('SearchModel', () => {
E
Erich Gamma 已提交
21

22 23
	let instantiationService: TestInstantiationService;
	let restoreStubs;
E
Erich Gamma 已提交
24 25

	setup(() => {
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
		restoreStubs= [];
		instantiationService= new TestInstantiationService();
		instantiationService.stub(ITelemetryService);
	});

	teardown(() => {
		restoreStubs.forEach(element => {
			element.restore();
		});
	});

	test('Search Model: Search adds to results', function () {
		let results= [aRawMatch('file://c:/1', aLineMatch('preview 1', 1, [[1, 3], [4, 7]])), aRawMatch('file://c:/2', aLineMatch('preview 2'))];
		instantiationService.stub(ISearchService, 'search',  PPromise.as({results: results}));

		let testObject= instantiationService.createInstance(SearchModel);
		testObject.search({contentPattern: {pattern: 'somestring'}, type: 1});

		let actual= testObject.searchResult.matches();

		assert.equal(2, actual.length);
		assert.equal('file://c:/1', actual[0].resource().toString());

		let actuaMatches= actual[0].matches();
		assert.equal(2, actuaMatches.length);
		assert.equal('preview 1', actuaMatches[0].text());
		assert.ok(new Range(2, 2, 2, 5).equalsRange(actuaMatches[0].range()));
		assert.equal('preview 1', actuaMatches[1].text());
		assert.ok(new Range(2, 5, 2, 12).equalsRange(actuaMatches[1].range()));

		actuaMatches= actual[1].matches();
		assert.equal(1, actuaMatches.length);
		assert.equal('preview 2', actuaMatches[0].text());
		assert.ok(new Range(2, 1, 2, 2).equalsRange(actuaMatches[0].range()));
	});

	test('Search Model: Search adds to results during progress', function (done) {
		let results= [aRawMatch('file://c:/1', aLineMatch('preview 1', 1, [[1, 3], [4, 7]])), aRawMatch('file://c:/2', aLineMatch('preview 2'))];
		let promise= new DeferredPPromise<ISearchComplete, ISearchProgressItem>();
		instantiationService.stub(ISearchService, 'search',  promise);

		let testObject= instantiationService.createInstance(SearchModel);
		let result= testObject.search({contentPattern: {pattern: 'somestring'}, type: 1});

		promise.progress(results[0]);
		promise.progress(results[1]);
		promise.complete({results: []});

		result.done(() => {
			let actual= testObject.searchResult.matches();

			assert.equal(2, actual.length);
			assert.equal('file://c:/1', actual[0].resource().toString());

			let actuaMatches= actual[0].matches();
			assert.equal(2, actuaMatches.length);
			assert.equal('preview 1', actuaMatches[0].text());
			assert.ok(new Range(2, 2, 2, 5).equalsRange(actuaMatches[0].range()));
			assert.equal('preview 1', actuaMatches[1].text());
			assert.ok(new Range(2, 5, 2, 12).equalsRange(actuaMatches[1].range()));

			actuaMatches= actual[1].matches();
			assert.equal(1, actuaMatches.length);
			assert.equal('preview 2', actuaMatches[0].text());
			assert.ok(new Range(2, 1, 2, 2).equalsRange(actuaMatches[0].range()));

			done();
		});
	});

	test('Search Model: Search reports telemetry on search completed', function () {
		let target= instantiationService.spy(ITelemetryService, 'publicLog');
		let results= [aRawMatch('file://c:/1', aLineMatch('preview 1', 1, [[1, 3], [4, 7]])), aRawMatch('file://c:/2', aLineMatch('preview 2'))];
		instantiationService.stub(ISearchService, 'search',  PPromise.as({results: results}));

		let testObject= instantiationService.createInstance(SearchModel);
		testObject.search({contentPattern: {pattern: 'somestring'}, type: 1});

		assert.ok(target.calledOnce);
		assert.deepEqual(['searchResultsShown', {count: 3, fileCount: 2}], target.args[0]);
	});

	test('Search Model: Search reports timed telemetry on search when progress is not called', function () {
		let target2= sinon.spy();
		stub(nullEvent, 'stop', target2);
		let target1= sinon.stub().returns(nullEvent);
		instantiationService.stub(ITelemetryService, 'timedPublicLog', target1);

		instantiationService.stub(ISearchService, 'search',  PPromise.as({results: []}));

		let testObject= instantiationService.createInstance(SearchModel);
		testObject.search({contentPattern: {pattern: 'somestring'}, type: 1});

		assert.ok(target1.calledTwice);
		assert.ok(target1.calledWith('searchResultsFirstRender'));
		assert.ok(target1.calledWith('searchResultsFinished'));
		assert.ok(target2.calledThrice);
	});

	test('Search Model: Search reports timed telemetry on search when progress is called', function (done) {
		let target2= sinon.spy();
		stub(nullEvent, 'stop', target2);
		let target1= sinon.stub().returns(nullEvent);
		instantiationService.stub(ITelemetryService, 'timedPublicLog', target1);

		let promise= new DeferredPPromise<ISearchComplete, ISearchProgressItem>();
		instantiationService.stub(ISearchService, 'search',  promise);

		let testObject= instantiationService.createInstance(SearchModel);
		let result= testObject.search({contentPattern: {pattern: 'somestring'}, type: 1});

		promise.progress(aRawMatch('file://c:/1', aLineMatch('some preview')));
		promise.complete({results: []});

		result.done(() => {
			assert.ok(target1.calledTwice);
			assert.ok(target1.calledWith('searchResultsFirstRender'));
			assert.ok(target1.calledWith('searchResultsFinished'));
			assert.equal(4, target2.callCount);

			done();
		});
	});

	test('Search Model: Search reports timed telemetry on search when error is called', function (done) {
		let target2= sinon.spy();
		stub(nullEvent, 'stop', target2);
		let target1= sinon.stub().returns(nullEvent);
		instantiationService.stub(ITelemetryService, 'timedPublicLog', target1);

		let promise= new DeferredPPromise<ISearchComplete, ISearchProgressItem>();
		instantiationService.stub(ISearchService, 'search',  promise);

		let testObject= instantiationService.createInstance(SearchModel);
		let result= testObject.search({contentPattern: {pattern: 'somestring'}, type: 1});

		promise.error('error');

		result.done(() => {}, () => {
			assert.ok(target1.calledTwice);
			assert.ok(target1.calledWith('searchResultsFirstRender'));
			assert.ok(target1.calledWith('searchResultsFinished'));
			assert.ok(target2.calledThrice);

			done();
		});
	});

	test('Search Model: Search reports timed telemetry on search when error is cancelled error', function (done) {
		let target2= sinon.spy();
		stub(nullEvent, 'stop', target2);
		let target1= sinon.stub().returns(nullEvent);
		instantiationService.stub(ITelemetryService, 'timedPublicLog', target1);

		let promise= new DeferredPPromise<ISearchComplete, ISearchProgressItem>();
		instantiationService.stub(ISearchService, 'search',  promise);

		let testObject= instantiationService.createInstance(SearchModel);
		let result= testObject.search({contentPattern: {pattern: 'somestring'}, type: 1});

		promise.cancel();

		result.done(() => {}, () => {
			assert.ok(target1.calledTwice);
			assert.ok(target1.calledWith('searchResultsFirstRender'));
			assert.ok(target1.calledWith('searchResultsFinished'));
			assert.ok(target2.calledThrice);
			done();
		});
	});

	test('Search Model: Search results are cleared during search', function () {
		let results= [aRawMatch('file://c:/1', aLineMatch('preview 1', 1, [[1, 3], [4, 7]])), aRawMatch('file://c:/2', aLineMatch('preview 2'))];
		instantiationService.stub(ISearchService, 'search',  PPromise.as({results: results}));
		let testObject:SearchModel= instantiationService.createInstance(SearchModel);
		testObject.search({contentPattern: {pattern: 'somestring'}, type: 1});
		assert.ok(!testObject.searchResult.isEmpty());

		instantiationService.stub(ISearchService, 'search',  new DeferredPPromise<ISearchComplete, ISearchProgressItem>());

		testObject.search({contentPattern: {pattern: 'somestring'}, type: 1});
		assert.ok(testObject.searchResult.isEmpty());
	});

	test('Search Model: Previous search is cancelled when new search is called', function () {
		let target= sinon.spy();
		instantiationService.stub(ISearchService, 'search',  new DeferredPPromise((c, e, p) => {}, target));
		let testObject:SearchModel= instantiationService.createInstance(SearchModel);

		testObject.search({contentPattern: {pattern: 'somestring'}, type: 1});
		instantiationService.stub(ISearchService, 'search',  new DeferredPPromise<ISearchComplete, ISearchProgressItem>());
		testObject.search({contentPattern: {pattern: 'somestring'}, type: 1});

		assert.ok(target.calledOnce);
	});

	test('Search Model: isReplaceActive return false if no replace text is set', function () {
		let testObject:SearchModel= instantiationService.createInstance(SearchModel);

		assert.ok(!testObject.isReplaceActive());
	});

	test('Search Model: isReplaceActive return false if replace text is set to null', function () {
		let testObject:SearchModel= instantiationService.createInstance(SearchModel);
		testObject.replaceText= null;

		assert.ok(!testObject.isReplaceActive());
	});

	test('Search Model: isReplaceActive return false if replace text is set to undefined', function () {
		let testObject:SearchModel= instantiationService.createInstance(SearchModel);
		testObject.replaceText= void 0;

		assert.ok(!testObject.isReplaceActive());
	});

	test('Search Model: isReplaceActive return true if replace text is set to empty string', function () {
		let testObject:SearchModel= instantiationService.createInstance(SearchModel);
		testObject.replaceText= '';

		assert.ok(testObject.isReplaceActive());
	});

	test('Search Model: isReplaceActive return true if replace text is set to non empty string', function () {
		let testObject:SearchModel= instantiationService.createInstance(SearchModel);
		testObject.replaceText= 'some value';

		assert.ok(testObject.isReplaceActive());
	});

	test('Search Model: hasReplaceText return false if no replace text is set', function () {
		let testObject:SearchModel= instantiationService.createInstance(SearchModel);

		assert.ok(!testObject.hasReplaceText());
	});

	test('Search Model: hasReplaceText return false if replace text is set to null', function () {
		let testObject:SearchModel= instantiationService.createInstance(SearchModel);
		testObject.replaceText= null;

		assert.ok(!testObject.hasReplaceText());
	});

	test('Search Model: hasReplaceText return false if replace text is set to undefined', function () {
		let testObject:SearchModel= instantiationService.createInstance(SearchModel);
		testObject.replaceText= void 0;

		assert.ok(!testObject.hasReplaceText());
	});

	test('Search Model: hasReplaceText return false if replace text is set to empty string', function () {
		let testObject:SearchModel= instantiationService.createInstance(SearchModel);
		testObject.replaceText= '';

		assert.ok(!testObject.hasReplaceText());
	});

	test('Search Model: hasReplaceText return true if replace text is set to non empty string', function () {
		let testObject:SearchModel= instantiationService.createInstance(SearchModel);
		testObject.replaceText= 'some value';

		assert.ok(testObject.hasReplaceText());
	});

	function aRawMatch(resource: string, ...lineMatches: ILineMatch[]): IFileMatch {
		return { resource: URI.parse(resource), lineMatches };
	}

	function aLineMatch(preview: string, lineNumber: number = 1, offsetAndLengths: number[][] = [[0, 1]]): ILineMatch {
		return { preview, lineNumber, offsetAndLengths };
	}

	function stub(arg1, arg2, arg3) : sinon.SinonStub {
		const stub= sinon.stub(arg1, arg2, arg3);
		restoreStubs.push(stub);
		return stub;
	}

E
Erich Gamma 已提交
304
});