Path: blob/main/extensions/copilot/src/util/node/test/markdown.spec.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { suite, test } from 'vitest';7import { createFilepathRegexp, extractCodeBlocks, mdCodeBlockLangToLanguageId } from '../../common/markdown';89suite('markdown', () => {10suite('extractCodeBlocks', () => {11test('should extract single code block', () => {12{13const result = extractCodeBlocks([14'```',15'single',16'```',17].join('\n'));18assert.strictEqual(result.length, 1);19assert.deepStrictEqual(result[0].code, 'single');20}21{22const result = extractCodeBlocks([23'```ts',24'single',25'```',26].join('\n'));27assert.strictEqual(result.length, 1);28assert.deepStrictEqual(result[0].code, 'single');29assert.deepStrictEqual(result[0].language, 'ts');30}31});3233test('should extract multiple code blocks', () => {34const result = extractCodeBlocks([35'```',36'one',37'```',38'',39'code',40'',41'```php',42'two',43'```',44].join('\n'));45assert.strictEqual(result.length, 2);46assert.deepStrictEqual(result[0].code, 'one');47assert.deepStrictEqual(result[0].language, '');4849assert.deepStrictEqual(result[1].code, 'two');50assert.deepStrictEqual(result[1].language, 'php');51});5253test('should detect nested code blocks', () => {54const result = extractCodeBlocks([55'```',56'one',57'```',58'',59'- code',60' ',61' ```php',62' two',63' ```',64].join('\n'));65assert.strictEqual(result.length, 2);66assert.deepStrictEqual(result[0].code, 'one');67assert.deepStrictEqual(result[0].language, '');6869assert.deepStrictEqual(result[1].code, 'two');70assert.deepStrictEqual(result[1].language, 'php');71});72});7374suite('createFilepathRegexp', () => {75test('should match filepath comment with //', () => {76const regexp = createFilepathRegexp('typescript');77const result = regexp.exec('// filepath: /path/to/file');78assert.ok(result);79assert.strictEqual(result[1], '/path/to/file');80});8182test('should match filepath comment with // with newline', () => {83const regexp = createFilepathRegexp('typescript');84const result = regexp.exec('// filepath: /path/to/file\n');85assert.ok(result);86assert.strictEqual(result[1], '/path/to/file');87});8889test('should match filepath comment with // with newline \r\n', () => {90const regexp = createFilepathRegexp('typescript');91const result = regexp.exec('// filepath: /path/to/file\r\n');92assert.ok(result);93assert.strictEqual(result[1], '/path/to/file');94});9596test('should match filepath comment with #', () => {97const regexp = createFilepathRegexp('python');98const result = regexp.exec('# filepath: /path/to/file');99assert.ok(result);100assert.strictEqual(result[1], '/path/to/file');101});102103test('should match filepath comment with <!--', () => {104const regexp = createFilepathRegexp('html');105const result = regexp.exec('<!-- filepath: /path/to/file -->');106assert.ok(result);107assert.strictEqual(result[1], '/path/to/file');108});109110test('should match filepath comment with <!-- but no -->', () => {111const regexp = createFilepathRegexp('html');112const result = regexp.exec('<!-- filepath: /path/to/file');113assert.ok(result);114assert.strictEqual(result[1], '/path/to/file');115});116117test('should match filepath comment with <!-- and spaces in path', () => {118const regexp = createFilepathRegexp('html');119const result = regexp.exec('<!-- filepath: /path/to/file with spaces -->');120assert.ok(result);121assert.strictEqual(result[1], '/path/to/file with spaces');122});123124test('should match filepath comment with <!-- and spaces in path no spaces at end', () => {125const regexp = createFilepathRegexp('html');126const result = regexp.exec('<!-- filepath: /path/to/file with spaces-->');127assert.ok(result);128assert.strictEqual(result[1], '/path/to/file with spaces');129});130131test('should match filepath comment with <!-- and spaces in path no spaces at end with newline', () => {132const regexp = createFilepathRegexp('html');133const result = regexp.exec('<!-- filepath: /path/to/file with spaces-->\n');134assert.ok(result);135assert.strictEqual(result[1], '/path/to/file with spaces');136});137138test('should match alternative BAT line comments', () => {139const regexp = createFilepathRegexp('bat');140const result = regexp.exec(':: filepath: /path/to/file');141assert.ok(result);142assert.strictEqual(result[1], '/path/to/file');143});144145test('should match BAT line comments', () => {146const regexp = createFilepathRegexp('bat');147const result = regexp.exec('REM filepath: /path/to/file');148assert.ok(result);149assert.strictEqual(result[1], '/path/to/file');150});151152test('should always match #', () => {153const regexp = createFilepathRegexp('html');154const result = regexp.exec('# filepath: /path/to/file');155assert.ok(result);156assert.strictEqual(result[1], '/path/to/file');157});158159test('should always match //', () => {160const regexp = createFilepathRegexp('html');161const result = regexp.exec('// filepath: /path/to/file');162assert.ok(result);163assert.strictEqual(result[1], '/path/to/file');164});165166test('should accept extra whitespaces', () => {167const regexp = createFilepathRegexp('html');168const result = regexp.exec(' // filepath:/path/to/file ');169assert.ok(result);170assert.strictEqual(result[1], '/path/to/file');171});172173test('should accept extra whitespaces in path', () => {174const regexp = createFilepathRegexp('html');175const result = regexp.exec(' // filepath:/path/to/file with spaces.py ');176assert.ok(result);177assert.strictEqual(result[1], '/path/to/file with spaces.py');178});179180test('should accept extra whitespaces in path and newline', () => {181const regexp = createFilepathRegexp('html');182const result = regexp.exec(' // filepath:/path/to/file with spaces.py \n');183assert.ok(result);184assert.strictEqual(result[1], '/path/to/file with spaces.py');185});186187test('should accept empty language', () => {188const regexp = createFilepathRegexp();189const result = regexp.exec(' // filepath: /path/to/file ');190assert.ok(result);191assert.strictEqual(result[1], '/path/to/file');192});193});194195suite('mdCodeBlockLangToLanguageId', () => {196test('ts is typescript', () => {197const result = mdCodeBlockLangToLanguageId('ts');198assert.strictEqual(result, 'typescript');199});200201test('tsreact is typescriptreact', () => {202const result = mdCodeBlockLangToLanguageId('tsx');203assert.strictEqual(result, 'typescriptreact');204});205206test('python is python', () => {207const result = mdCodeBlockLangToLanguageId('python');208assert.strictEqual(result, 'python');209});210211});212});213214215216