Welcome to mirror list, hosted at ThFree Co, Russian Federation.

commands.test.ts « singlefolder-tests « src « vscode-api-tests « extensions - github.com/microsoft/vscode.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b241b7170ecfb1001b459e6c76fbb7951cc7f82c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as assert from 'assert';
import 'mocha';
import { join } from 'path';
import { commands, Position, Range, Uri, ViewColumn, window, workspace } from 'vscode';
import { assertNoRpc, closeAllEditors } from '../utils';

suite('vscode API - commands', () => {

	teardown(assertNoRpc);

	test('getCommands', function (done) {

		let p1 = commands.getCommands().then(commands => {
			let hasOneWithUnderscore = false;
			for (let command of commands) {
				if (command[0] === '_') {
					hasOneWithUnderscore = true;
					break;
				}
			}
			assert.ok(hasOneWithUnderscore);
		}, done);

		let p2 = commands.getCommands(true).then(commands => {
			let hasOneWithUnderscore = false;
			for (let command of commands) {
				if (command[0] === '_') {
					hasOneWithUnderscore = true;
					break;
				}
			}
			assert.ok(!hasOneWithUnderscore);
		}, done);

		Promise.all([p1, p2]).then(() => {
			done();
		}, done);
	});

	test('command with args', async function () {

		let args: IArguments;
		let registration = commands.registerCommand('t1', function () {
			args = arguments;
		});

		await commands.executeCommand('t1', 'start');
		registration.dispose();
		assert.ok(args!);
		assert.strictEqual(args!.length, 1);
		assert.strictEqual(args![0], 'start');
	});

	test('editorCommand with extra args', function () {

		let args: IArguments;
		let registration = commands.registerTextEditorCommand('t1', function () {
			args = arguments;
		});

		return workspace.openTextDocument(join(workspace.rootPath || '', './far.js')).then(doc => {
			return window.showTextDocument(doc).then(_editor => {
				return commands.executeCommand('t1', 12345, commands);
			}).then(() => {
				assert.ok(args);
				assert.strictEqual(args.length, 4);
				assert.ok(args[2] === 12345);
				assert.ok(args[3] === commands);
				registration.dispose();
			});
		});

	});

	test('api-command: vscode.diff', function () {

		let registration = workspace.registerTextDocumentContentProvider('sc', {
			provideTextDocumentContent(uri) {
				return `content of URI <b>${uri.toString()}</b>#${Math.random()}`;
			}
		});


		let a = commands.executeCommand('vscode.diff', Uri.parse('sc:a'), Uri.parse('sc:b'), 'DIFF').then(value => {
			assert.ok(value === undefined);
			registration.dispose();
		});

		let b = commands.executeCommand('vscode.diff', Uri.parse('sc:a'), Uri.parse('sc:b')).then(value => {
			assert.ok(value === undefined);
			registration.dispose();
		});

		let c = commands.executeCommand('vscode.diff', Uri.parse('sc:a'), Uri.parse('sc:b'), 'Title', { selection: new Range(new Position(1, 1), new Position(1, 2)) }).then(value => {
			assert.ok(value === undefined);
			registration.dispose();
		});

		let d = commands.executeCommand('vscode.diff').then(() => assert.ok(false), () => assert.ok(true));
		let e = commands.executeCommand('vscode.diff', 1, 2, 3).then(() => assert.ok(false), () => assert.ok(true));

		return Promise.all([a, b, c, d, e]);
	});

	test('api-command: vscode.open', async function () {
		let uri = Uri.parse(workspace.workspaceFolders![0].uri.toString() + '/far.js');

		await commands.executeCommand('vscode.open', uri);
		assert.strictEqual(window.activeTextEditor?.viewColumn, ViewColumn.One);
		assert.strictEqual(window.tabGroups.all[0].activeTab?.group.viewColumn, ViewColumn.One);

		await commands.executeCommand('vscode.open', uri, ViewColumn.Two);
		assert.strictEqual(window.activeTextEditor?.viewColumn, ViewColumn.Two);
		assert.strictEqual(window.tabGroups.all[1].activeTab?.group.viewColumn, ViewColumn.Two);

		await commands.executeCommand('vscode.open', uri, ViewColumn.One);
		assert.strictEqual(window.activeTextEditor?.viewColumn, ViewColumn.One);
		assert.strictEqual(window.tabGroups.all[0].activeTab?.group.viewColumn, ViewColumn.One);

		let e1: Error | undefined = undefined;
		try {
			await commands.executeCommand('vscode.open');
		} catch (error) {
			e1 = error;
		}
		assert.ok(e1);

		let e2: Error | undefined = undefined;
		try {
			await commands.executeCommand('vscode.open', uri, true);
		} catch (error) {
			e2 = error;
		}
		assert.ok(e2);


		// we support strings but only http/https. those we cannot test but we can
		// enforce that other schemes are treated strict
		try {
			await commands.executeCommand('vscode.open', 'file:///some/path/not/http');
			assert.fail('expecting exception');
		} catch {
			assert.ok(true);
		}

	});

	test('api-command: vscode.open with untitled supports associated resource (#138925)', async function () {
		let uri = Uri.parse(workspace.workspaceFolders![0].uri.toString() + '/untitled-file.txt').with({ scheme: 'untitled' });
		await commands.executeCommand('vscode.open', uri).then(() => assert.ok(true), () => assert.ok(false));

		// untitled with associated resource are dirty from the beginning
		assert.ok(window.activeTextEditor?.document.isDirty);

		return closeAllEditors();
	});
});