stringEditorModel.test.ts 2.7 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  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';
9
import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils';
10
import {StringEditorModel} from 'vs/workbench/common/editor/stringEditorModel';
11 12
import {IModelService} from 'vs/editor/common/services/modelService';
import {IModeService} from 'vs/editor/common/services/modeService';
13
import {createMockModelService} from 'vs/test/utils/servicesTestUtils';
E
Erich Gamma 已提交
14

B
Benjamin Pasero 已提交
15
suite('Workbench - StringEditorModel', () => {
E
Erich Gamma 已提交
16

17 18 19 20 21 22 23
	let instantiationService: TestInstantiationService;

	setup(() => {
		instantiationService= new TestInstantiationService();
		instantiationService.stub(IModeService);
	});

B
Benjamin Pasero 已提交
24
	test('StringEditorModel', function (done) {
25 26
		instantiationService.stub(IModelService, createMockModelService(instantiationService));
		let m = instantiationService.createInstance(StringEditorModel, 'value', 'mime', null);
B
Benjamin Pasero 已提交
27
		m.load().then(function (model) {
E
Erich Gamma 已提交
28 29 30
			assert(model === m);

			let textEditorModel = m.textEditorModel;
B
Benjamin Pasero 已提交
31
			assert.strictEqual(textEditorModel.getValue(), 'value');
E
Erich Gamma 已提交
32 33 34

			assert.strictEqual(m.isResolved(), true);

B
Benjamin Pasero 已提交
35 36
			(<any>m).value = 'something';
			return m.load().then(function (model) {
E
Erich Gamma 已提交
37
				assert(textEditorModel === m.textEditorModel);
B
Benjamin Pasero 已提交
38
				assert.strictEqual(m.getValue(), 'something');
E
Erich Gamma 已提交
39 40 41
			});
		}).done(() => {
			m.dispose();
B
Benjamin Pasero 已提交
42
			done();
E
Erich Gamma 已提交
43 44 45
		});
	});

B
Benjamin Pasero 已提交
46
	test('StringEditorModel - setValue, clearValue, append, trim', function (done) {
47 48
		instantiationService.stub(IModelService, createMockModelService(instantiationService));
		let m = instantiationService.createInstance(StringEditorModel, 'value', 'mime', null);
B
Benjamin Pasero 已提交
49
		m.load().then(function (model) {
E
Erich Gamma 已提交
50 51 52
			assert(model === m);

			let textEditorModel = m.textEditorModel;
B
Benjamin Pasero 已提交
53
			assert.strictEqual(textEditorModel.getValue(), 'value');
E
Erich Gamma 已提交
54

B
Benjamin Pasero 已提交
55 56 57
			m.setValue('foobar');
			assert.strictEqual(m.getValue(), 'foobar');
			assert.strictEqual(textEditorModel.getValue(), 'foobar');
E
Erich Gamma 已提交
58 59 60 61 62

			m.clearValue();
			assert(!m.getValue());
			assert(!textEditorModel.getValue());

B
Benjamin Pasero 已提交
63 64 65
			m.append('1');
			assert.strictEqual(m.getValue(), '1');
			assert.strictEqual(textEditorModel.getValue(), '1');
E
Erich Gamma 已提交
66

B
Benjamin Pasero 已提交
67 68 69
			m.append('1');
			assert.strictEqual(m.getValue(), '11');
			assert.strictEqual(textEditorModel.getValue(), '11');
E
Erich Gamma 已提交
70

B
Benjamin Pasero 已提交
71
			m.setValue('line\nline\nline');
E
Erich Gamma 已提交
72 73
			m.trim(2);

B
Benjamin Pasero 已提交
74 75
			assert.strictEqual(m.getValue(), 'line\nline');
			assert.strictEqual(textEditorModel.getValue(), 'line\nline');
E
Erich Gamma 已提交
76 77 78 79 80 81
		}).done(() => {
			m.dispose();
			done();
		});
	});
});