diff --git a/protocol/src/common/protocol.ts b/protocol/src/common/protocol.ts index 01f8b6d0f..af6a9f23e 100644 --- a/protocol/src/common/protocol.ts +++ b/protocol/src/common/protocol.ts @@ -329,7 +329,7 @@ export type NotebookDocumentFilter = NotebookDocumentFilterNotebookType | Notebo export namespace NotebookDocumentFilter { export function is(value: any): value is NotebookDocumentFilter { const candidate: NotebookDocumentFilter = value; - return Is.objectLiteral(candidate) && (Is.string(candidate.notebookType) || Is.string(candidate.scheme) || Is.string(candidate.pattern)); + return Is.objectLiteral(candidate) && (Is.string(candidate.notebookType) || Is.string(candidate.scheme) || GlobPattern.is(candidate.pattern)); } } diff --git a/protocol/src/node/test/documentFilter.test.ts b/protocol/src/node/test/documentFilter.test.ts new file mode 100644 index 000000000..69ac21282 --- /dev/null +++ b/protocol/src/node/test/documentFilter.test.ts @@ -0,0 +1,51 @@ +/* -------------------------------------------------------------------------------------------- + * 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 { DocumentSelector, NotebookCellTextDocumentFilter, NotebookDocumentFilter, RelativePattern, TextDocumentRegistrationOptions } from '../main'; + +suite('Document filter type guards', () => { + + const relativePattern: RelativePattern = { baseUri: 'file:///workspace', pattern: '**/*.ipynb' }; + + suite('NotebookDocumentFilter.is', () => { + test('string pattern', () => { + assert.strictEqual(NotebookDocumentFilter.is({ pattern: '**/*.ipynb' }), true); + }); + test('relative pattern', () => { + assert.strictEqual(NotebookDocumentFilter.is({ pattern: relativePattern }), true); + }); + test('relative pattern with workspace folder', () => { + const pattern: RelativePattern = { baseUri: { uri: 'file:///workspace', name: 'workspace' }, pattern: '**/*.ipynb' }; + assert.strictEqual(NotebookDocumentFilter.is({ pattern }), true); + }); + test('invalid pattern', () => { + assert.strictEqual(NotebookDocumentFilter.is({}), false); + assert.strictEqual(NotebookDocumentFilter.is({ pattern: 1 }), false); + assert.strictEqual(NotebookDocumentFilter.is({ pattern: {} }), false); + assert.strictEqual(NotebookDocumentFilter.is({ pattern: { baseUri: 1, pattern: '**/*.ipynb' } }), false); + }); + }); + + suite('NotebookCellTextDocumentFilter.is', () => { + test('notebook filter with relative pattern', () => { + assert.strictEqual(NotebookCellTextDocumentFilter.is({ notebook: { pattern: relativePattern }, language: 'python' }), true); + assert.strictEqual(NotebookCellTextDocumentFilter.is({ notebook: { pattern: relativePattern } }), true); + }); + test('text document filter with relative pattern', () => { + assert.strictEqual(NotebookCellTextDocumentFilter.is({ language: 'python', pattern: relativePattern }), false); + }); + }); + + suite('DocumentSelector.is', () => { + test('notebook cell filter with relative pattern', () => { + const documentSelector = [{ notebook: { pattern: relativePattern } }]; + assert.strictEqual(DocumentSelector.is(documentSelector), true); + assert.strictEqual(TextDocumentRegistrationOptions.is({ documentSelector }), true); + }); + }); +});