resources.test.ts 2.4 KB
Newer Older
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';
B
Benjamin Pasero 已提交
8
import { normalize } from 'vs/base/common/paths';
R
Rob Lourens 已提交
9 10
import { dirname, distinctParents, joinPath } from 'vs/base/common/resources';
import URI from 'vs/base/common/uri';
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

suite('Resources', () => {

	test('distinctParents', () => {

		// Basic
		let resources = [
			URI.file('/some/folderA/file.txt'),
			URI.file('/some/folderB/file.txt'),
			URI.file('/some/folderC/file.txt')
		];

		let distinct = distinctParents(resources, r => r);
		assert.equal(distinct.length, 3);
		assert.equal(distinct[0].toString(), resources[0].toString());
		assert.equal(distinct[1].toString(), resources[1].toString());
		assert.equal(distinct[2].toString(), resources[2].toString());

		// Parent / Child
		resources = [
			URI.file('/some/folderA'),
			URI.file('/some/folderA/file.txt'),
			URI.file('/some/folderA/child/file.txt'),
			URI.file('/some/folderA2/file.txt'),
			URI.file('/some/file.txt')
		];

		distinct = distinctParents(resources, r => r);
		assert.equal(distinct.length, 3);
		assert.equal(distinct[0].toString(), resources[0].toString());
		assert.equal(distinct[1].toString(), resources[3].toString());
		assert.equal(distinct[2].toString(), resources[4].toString());
	});
B
Benjamin Pasero 已提交
44

45
	test('dirname', () => {
B
Benjamin Pasero 已提交
46 47
		const f = URI.file('/some/file/test.txt');
		const d = dirname(f);
B
Benjamin Pasero 已提交
48
		assert.equal(d.fsPath, normalize('/some/file', true));
B
Benjamin Pasero 已提交
49 50

		// does not explode (https://github.com/Microsoft/vscode/issues/41987)
51
		dirname(URI.from({ scheme: 'file', authority: '/users/someone/portal.h' }));
B
Benjamin Pasero 已提交
52
	});
R
Rob Lourens 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

	test('joinPath', () => {
		assert.equal(
			joinPath(URI.file('/foo/bar'), '/file.js').toString(),
			'file:///foo/bar/file.js');

		assert.equal(
			joinPath(URI.file('/foo/bar/'), '/file.js').toString(),
			'file:///foo/bar/file.js');

		assert.equal(
			joinPath(URI.file('/'), '/file.js').toString(),
			'file:///file.js');

		assert.equal(
			joinPath(URI.from({ scheme: 'myScheme', authority: 'authority', path: '/path', query: 'query', fragment: 'fragment' }), '/file.js').toString(),
			'myScheme://authority/path/file.js?query#fragment');
	});
71
});