completionModel.test.ts 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*---------------------------------------------------------------------------------------------
 *  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';
import {ISuggestion, ISuggestResult} from 'vs/editor/common/modes';
import {ISuggestionItem} from 'vs/editor/contrib/suggest/common/suggest';
import {CompletionModel} from 'vs/editor/contrib/suggest/common/completionModel';


suite('CompletionModel', function () {

J
Johannes Rieken 已提交
15
	function createSuggestItem(label: string, overwriteBefore: number, incomplete: boolean = false): ISuggestionItem {
16 17 18 19 20 21 22 23 24 25 26 27

		return new class implements ISuggestionItem {

			suggestion: ISuggestion = {
				label,
				overwriteBefore,
				insertText: label,
				type: 'property'
			};

			container: ISuggestResult = {
				currentWord: '',
J
Johannes Rieken 已提交
28
				incomplete,
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
				suggestions: [this.suggestion]
			};

			support = null;

			resolve() {
				return null;
			}
		};
	}

	let model: CompletionModel;

	setup(function () {

		model = new CompletionModel([
			createSuggestItem('foo', 3),
			createSuggestItem('Foo', 3),
			createSuggestItem('foo', 2),
		], {
J
Johannes Rieken 已提交
49 50 51
			leadingLineContent: 'foo',
			characterCountDelta: 0
		});
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
	});

	test('filtering - cached', function () {

		const itemsNow = model.items;
		let itemsThen = model.items;
		assert.ok(itemsNow === itemsThen);

		// still the same context
		model.lineContext = { leadingLineContent: 'foo', characterCountDelta: 0 };
		itemsThen = model.items;
		assert.ok(itemsNow === itemsThen);

		// different context, refilter
		model.lineContext = { leadingLineContent: 'foo1', characterCountDelta: 1 };
		itemsThen = model.items;
		assert.ok(itemsNow !== itemsThen);
	});

	test('top score', function () {

		assert.equal(model.topScoreIdx, 0);

		model.lineContext = { leadingLineContent: 'Foo', characterCountDelta: 0 };
		assert.equal(model.topScoreIdx, 1);
	});
J
Johannes Rieken 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91

	test('complete/incomplete', function () {

		assert.equal(model.incomplete.length, 0);

		let incompleteModel = new CompletionModel([
			createSuggestItem('foo', 3, true),
			createSuggestItem('foo', 2),
		], {
			leadingLineContent: 'foo',
			characterCountDelta: 0
		});
		assert.equal(incompleteModel.incomplete.length, 1);
	});
92
});