Path: blob/main/extensions/copilot/src/platform/parser/test/node/getNodeToDocument.ts.spec.ts
13405 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 { outdent } from 'outdent';6import { afterAll, expect, suite, test } from 'vitest';7import {8_dispose,9_getNodeToDocument10} from '../../node/parserImpl';11import { WASMLanguage } from '../../node/treeSitterLanguages';12import { srcWithAnnotatedNodeToDoc } from './getNodeToDocument.util';131415suite('getNodeToDocument - typescript', () => {1617afterAll(() => _dispose());1819async function run(annotatedSrc: string) {20return srcWithAnnotatedNodeToDoc(21WASMLanguage.TypeScript,22annotatedSrc,23);24}2526test('should return root node for invalid range', async () => {27const result = await _getNodeToDocument(28WASMLanguage.TypeScript,29'const a = 1;',30{31startIndex: 100,32endIndex: 200,33}34);35expect(result).toMatchInlineSnapshot(`36{37"nodeIdentifier": undefined,38"nodeSelectionBy": "expanding",39"nodeToDocument": {40"endIndex": 12,41"startIndex": 0,42"type": "program",43},44}45`);46});4748test('should return root node for empty source', async () => {4950const result = await run('<<>>');5152expect(result).toMatchInlineSnapshot(`"<PROGRAM></PROGRAM>"`);53});5455test('should return node position for a variable declaration', async () => {56const result = await run(57'<<const>> a = 1;',58);59expect(result).toMatchInlineSnapshot(`"<LEXICAL_DECLARATION>const <IDENT>a</IDENT> = 1;</LEXICAL_DECLARATION>"`);60});6162test('should return node position for a function declaration', async () => {63const result = await srcWithAnnotatedNodeToDoc(64WASMLanguage.TypeScript,65'<<function>> add(a: number, b: number): number { return a + b; }',66);67expect(result).toMatchInlineSnapshot(`"<FUNCTION_DECLARATION>function <IDENT>add</IDENT>(a: number, b: number): number { return a + b; }</FUNCTION_DECLARATION>"`);68});6970test('should return node position for a class declaration', async () => {71const result = await run(72'<<class>> MyClass { constructor() {} }',73);74expect(result).toMatchInlineSnapshot(`"<CLASS_DECLARATION>class <IDENT>MyClass</IDENT> { constructor() {} }</CLASS_DECLARATION>"`);75});7677test('should return the whole program', async () => {78const result = await run(79outdent`80/**81* This is a comment82*/83<<const>> foo = 1;84`,85);86expect(result).toMatchInlineSnapshot(`87"/**88* This is a comment89*/90<LEXICAL_DECLARATION>const <IDENT>foo</IDENT> = 1;</LEXICAL_DECLARATION>"91`);92});9394test('should return the whole program - function', async () => {95const result = await run(96outdent`97/**98* This is a comment99*/100<<function>> add(a: number, b: number): number {101return a + b;102}103`,104);105expect(result).toMatchInlineSnapshot(`106"/**107* This is a comment108*/109<FUNCTION_DECLARATION>function <IDENT>add</IDENT>(a: number, b: number): number {110return a + b;111}</FUNCTION_DECLARATION>"112`);113});114115test('should return the whole program - class', async () => {116const result = await run(117outdent`118/**119* This is a comment120*/121<<class>> MyClass {122constructor() {}123}124`,125);126expect(result).toMatchInlineSnapshot(`127"/**128* This is a comment129*/130<CLASS_DECLARATION>class <IDENT>MyClass</IDENT> {131constructor() {}132}</CLASS_DECLARATION>"133`);134});135});136137138