|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as assert from 'assert'; |
| 7 | +import 'mocha'; |
| 8 | +import * as vscode from 'vscode'; |
| 9 | +import SymbolProvider from '../features/documentSymbolProvider'; |
| 10 | +import { InMemoryDocument } from './inMemoryDocument'; |
| 11 | +import { createNewMarkdownEngine } from './engine'; |
| 12 | + |
| 13 | + |
| 14 | +const testFileName = vscode.Uri.parse('test.md'); |
| 15 | + |
| 16 | + |
| 17 | +function getSymbolsForFile(fileContents: string) { |
| 18 | + const doc = new InMemoryDocument(testFileName, fileContents); |
| 19 | + const provider = new SymbolProvider(createNewMarkdownEngine()); |
| 20 | + return provider.provideDocumentSymbols(doc); |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +suite('markdown.DocumentSymbolProvider', () => { |
| 25 | + test('Should not return anything for empty document', async () => { |
| 26 | + const symbols = await getSymbolsForFile(''); |
| 27 | + assert.strictEqual(symbols.length, 0); |
| 28 | + }); |
| 29 | + |
| 30 | + test('Should not return anything for document with no headers', async () => { |
| 31 | + const symbols = await getSymbolsForFile('a\na'); |
| 32 | + assert.strictEqual(symbols.length, 0); |
| 33 | + }); |
| 34 | + |
| 35 | + test('Should not return anything for document with # but no real headers', async () => { |
| 36 | + const symbols = await getSymbolsForFile('a#a\na#'); |
| 37 | + assert.strictEqual(symbols.length, 0); |
| 38 | + }); |
| 39 | + |
| 40 | + test('Should return single symbol for single header', async () => { |
| 41 | + const symbols = await getSymbolsForFile('# h'); |
| 42 | + assert.strictEqual(symbols.length, 1); |
| 43 | + assert.strictEqual(symbols[0].name, '# h'); |
| 44 | + }); |
| 45 | + |
| 46 | + test('Should not care about symbol level for single header', async () => { |
| 47 | + const symbols = await getSymbolsForFile('### h'); |
| 48 | + assert.strictEqual(symbols.length, 1); |
| 49 | + assert.strictEqual(symbols[0].name, '### h'); |
| 50 | + }); |
| 51 | + |
| 52 | + test('Should put symbols of same level in flat list', async () => { |
| 53 | + const symbols = await getSymbolsForFile('## h\n## h2'); |
| 54 | + assert.strictEqual(symbols.length, 2); |
| 55 | + assert.strictEqual(symbols[0].name, '## h'); |
| 56 | + assert.strictEqual(symbols[1].name, '## h2'); |
| 57 | + }); |
| 58 | + |
| 59 | + test('Should nest symbol of level - 1 under parent', async () => { |
| 60 | + |
| 61 | + const symbols = await getSymbolsForFile('# h\n## h2\n## h3'); |
| 62 | + assert.strictEqual(symbols.length, 1); |
| 63 | + assert.strictEqual(symbols[0].name, '# h'); |
| 64 | + assert.strictEqual(symbols[0].children.length, 2); |
| 65 | + assert.strictEqual(symbols[0].children[0].name, '## h2'); |
| 66 | + assert.strictEqual(symbols[0].children[1].name, '## h3'); |
| 67 | + }); |
| 68 | + |
| 69 | + test('Should nest symbol of level - n under parent', async () => { |
| 70 | + const symbols = await getSymbolsForFile('# h\n#### h2'); |
| 71 | + assert.strictEqual(symbols.length, 1); |
| 72 | + assert.strictEqual(symbols[0].name, '# h'); |
| 73 | + assert.strictEqual(symbols[0].children.length, 1); |
| 74 | + assert.strictEqual(symbols[0].children[0].name, '#### h2'); |
| 75 | + }); |
| 76 | + |
| 77 | + test('Should flatten children where lower level occurs first', async () => { |
| 78 | + const symbols = await getSymbolsForFile('# h\n### h2\n## h3'); |
| 79 | + assert.strictEqual(symbols.length, 1); |
| 80 | + assert.strictEqual(symbols[0].name, '# h'); |
| 81 | + assert.strictEqual(symbols[0].children.length, 2); |
| 82 | + assert.strictEqual(symbols[0].children[0].name, '### h2'); |
| 83 | + assert.strictEqual(symbols[0].children[1].name, '## h3'); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
0 commit comments