extHostConfiguration.test.ts 2.6 KB
Newer Older
J
Johannes Rieken 已提交
1 2 3 4 5 6 7 8 9 10 11
/*---------------------------------------------------------------------------------------------
 *  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 {ExtHostConfiguration} from 'vs/workbench/api/node/extHostConfiguration';
import {MainThreadConfigurationShape} from 'vs/workbench/api/node/extHost.protocol';
import {TPromise} from 'vs/base/common/winjs.base';
12
import {ConfigurationTarget, ConfigurationEditingErrorCode, IConfigurationEditingError} from 'vs/workbench/services/configuration/common/configurationEditing';
J
Johannes Rieken 已提交
13 14 15 16 17

suite('ExtHostConfiguration', function () {

	class RecordingShape extends MainThreadConfigurationShape {
		lastArgs: [ConfigurationTarget, string, any];
18
		$updateConfigurationOption(target: ConfigurationTarget, key: string, value: any): TPromise<void> {
J
Johannes Rieken 已提交
19
			this.lastArgs = [target, key, value];
20
			return TPromise.as(void 0);
J
Johannes Rieken 已提交
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
		}
	};

	function createExtHostConfiguration(data: any = {}, shape?: MainThreadConfigurationShape) {
		if (!shape) {
			shape = new class extends MainThreadConfigurationShape { };
		}
		const result = new ExtHostConfiguration(shape);
		result.$acceptConfigurationChanged(data);
		return result;
	}

	test('check illegal state', function () {
		assert.throws(() => new ExtHostConfiguration(new class extends MainThreadConfigurationShape { }).getConfiguration('foo'));
	});

	test('udate / section to key', function () {

		const shape = new RecordingShape();
		const allConfig = createExtHostConfiguration({ foo: { bar: 1, far: 2 } }, shape);

		let config = allConfig.getConfiguration('foo');
		config.update('bar', 42, true);

		assert.equal(shape.lastArgs[1], 'foo.bar');
		assert.equal(shape.lastArgs[2], 42);

		config = allConfig.getConfiguration('');
		config.update('bar', 42, true);
		assert.equal(shape.lastArgs[1], 'bar');

		config.update('foo.bar', 42, true);
		assert.equal(shape.lastArgs[1], 'foo.bar');
	});

	test('update / error-state not OK', function () {

		const shape = new class extends MainThreadConfigurationShape {
59 60
			$updateConfigurationOption(target: ConfigurationTarget, key: string, value: any): TPromise<any> {
				return TPromise.wrapError(<IConfigurationEditingError>{ code: ConfigurationEditingErrorCode.ERROR_UNKNOWN_KEY, message: 'Unknown Key' }); // something !== OK
J
Johannes Rieken 已提交
61 62 63 64 65 66 67 68 69
			}
		};

		return createExtHostConfiguration({}, shape)
			.getConfiguration('')
			.update('', true, false)
			.then(() => assert.ok(false), err => { /* expecting rejection */});
	});
});