From 117ce52907d39f16b37b1520c11b94215914fe62 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Sun, 25 Aug 2024 03:23:34 +0800 Subject: [PATCH] Rewrite find references tests --- .../__snapshots__/completions.spec.ts.snap | 124 ++++++++++++++++++ .../__snapshots__/references.spec.ts.snap | 91 +++++++++++++ .../language-server/tests/references.spec.ts | 106 +++++++++++++++ .../reference/slot-default/entry.vue | 4 - .../reference/slot-default/foo.vue | 9 -- .../reference/slot-named/entry.vue | 4 - .../reference/slot-named/foo.vue | 7 - .../reference/v-bind-shorthand/child.vue | 6 - .../reference/v-bind-shorthand/entry.vue | 13 -- vitest.config.ts | 5 +- 10 files changed, 324 insertions(+), 45 deletions(-) create mode 100644 packages/language-server/tests/__snapshots__/completions.spec.ts.snap create mode 100644 packages/language-server/tests/__snapshots__/references.spec.ts.snap create mode 100644 packages/language-server/tests/references.spec.ts delete mode 100644 test-workspace/language-service/reference/slot-default/entry.vue delete mode 100644 test-workspace/language-service/reference/slot-default/foo.vue delete mode 100644 test-workspace/language-service/reference/slot-named/entry.vue delete mode 100644 test-workspace/language-service/reference/slot-named/foo.vue delete mode 100644 test-workspace/language-service/reference/v-bind-shorthand/child.vue delete mode 100644 test-workspace/language-service/reference/v-bind-shorthand/entry.vue diff --git a/packages/language-server/tests/__snapshots__/completions.spec.ts.snap b/packages/language-server/tests/__snapshots__/completions.spec.ts.snap new file mode 100644 index 0000000000..f2b3dd1bf6 --- /dev/null +++ b/packages/language-server/tests/__snapshots__/completions.spec.ts.snap @@ -0,0 +1,124 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`Completions > #2454 1`] = ` +" + + + + " +`; + +exports[`Completions > #2511 1`] = ` +" + + " +`; + +exports[`Completions > #3658 1`] = ` +" + + " +`; + +exports[`Completions > #4639 1`] = ` +" + + " +`; + +exports[`Completions > $event argument 1`] = `""`; + +exports[`Completions > + " +`; + +exports[`Completions > Alias path 1`] = ` +" + + " +`; + +exports[`Completions > Component auto import 1`] = ` +" + + + + " +`; + +exports[`Completions > Directives 1`] = `""`; + +exports[`Completions > Directives 2`] = `""`; + +exports[`Completions > Directives 3`] = `""`; + +exports[`Completions > Directives 4`] = `""`; + +exports[`Completions > Relative path 1`] = ` +" + + " +`; + +exports[`Completions > Slot name 1`] = ` +" + + + + " +`; + +exports[`Completions > core#8811 1`] = ` +" + + + + " +`; diff --git a/packages/language-server/tests/__snapshots__/references.spec.ts.snap b/packages/language-server/tests/__snapshots__/references.spec.ts.snap new file mode 100644 index 0000000000..b741277166 --- /dev/null +++ b/packages/language-server/tests/__snapshots__/references.spec.ts.snap @@ -0,0 +1,91 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`Definitions > Default slot 1`] = `"tsconfigProject/foo.vue"`; + +exports[`Definitions > Default slot 2`] = ` +{ + "end": { + "character": 16, + "line": 7, + }, + "start": { + "character": 5, + "line": 7, + }, +} +`; + +exports[`Definitions > Default slot 3`] = `"tsconfigProject/fixture.vue"`; + +exports[`Definitions > Default slot 4`] = ` +{ + "end": { + "character": 9, + "line": 2, + }, + "start": { + "character": 5, + "line": 2, + }, +} +`; + +exports[`Definitions > Named slot 1`] = `"tsconfigProject/foo.vue"`; + +exports[`Definitions > Named slot 2`] = ` +{ + "end": { + "character": 17, + "line": 6, + }, + "start": { + "character": 14, + "line": 6, + }, +} +`; + +exports[`Definitions > Named slot 3`] = `"tsconfigProject/fixture.vue"`; + +exports[`Definitions > Named slot 4`] = ` +{ + "end": { + "character": 19, + "line": 2, + }, + "start": { + "character": 16, + "line": 2, + }, +} +`; + +exports[`Definitions > v-bind shorthand 1`] = `"tsconfigProject/fixture.vue"`; + +exports[`Definitions > v-bind shorthand 2`] = ` +{ + "end": { + "character": 13, + "line": 6, + }, + "start": { + "character": 10, + "line": 6, + }, +} +`; + +exports[`Definitions > v-bind shorthand 3`] = `"tsconfigProject/fixture.vue"`; + +exports[`Definitions > v-bind shorthand 4`] = ` +{ + "end": { + "character": 12, + "line": 2, + }, + "start": { + "character": 9, + "line": 2, + }, +} +`; diff --git a/packages/language-server/tests/references.spec.ts b/packages/language-server/tests/references.spec.ts new file mode 100644 index 0000000000..34225abf5d --- /dev/null +++ b/packages/language-server/tests/references.spec.ts @@ -0,0 +1,106 @@ +import { TextDocument } from '@volar/language-server'; +import * as path from 'path'; +import { afterEach, describe, expect, it } from 'vitest'; +import { URI } from 'vscode-uri'; +import { getLanguageServer, testWorkspacePath } from './server.js'; + +describe('Definitions', async () => { + + it('Default slot', async () => { + await ensureGlobalTypesHolder('tsconfigProject'); + await openDocument('tsconfigProject/foo.vue', 'vue', ` + + + + `); + await assertReferences('tsconfigProject/fixture.vue', 'vue', ` + + `); + }); + + it('Named slot', async () => { + await ensureGlobalTypesHolder('tsconfigProject'); + await openDocument('tsconfigProject/foo.vue', 'vue', ` + + + + `); + await assertReferences('tsconfigProject/fixture.vue', 'vue', ` + + `); + }); + + it('v-bind shorthand', async () => { + await ensureGlobalTypesHolder('tsconfigProject'); + await assertReferences('tsconfigProject/fixture.vue', 'vue', ` + + + + `); + }); + + const openedDocuments: TextDocument[] = []; + + afterEach(async () => { + const server = await getLanguageServer(); + for (const document of openedDocuments) { + await server.closeTextDocument(document.uri); + } + openedDocuments.length = 0; + }); + + /** + * @deprecated Remove this when #4717 fixed. + */ + async function ensureGlobalTypesHolder(folderName: string) { + const document = await openDocument(`${folderName}/globalTypesHolder.vue`, 'vue', ''); + const server = await getLanguageServer(); + await server.sendDocumentDiagnosticRequest(document.uri); + } + + async function assertReferences(fileName: string, languageId: string, content: string) { + const offset = content.indexOf('|'); + content = content.slice(0, offset) + content.slice(offset + 1); + + const server = await getLanguageServer(); + let document = await openDocument(fileName, languageId, content); + + const position = document.positionAt(offset); + const references = await server.sendReferencesRequest(document.uri, position, { includeDeclaration: false }); + expect(references).toBeDefined(); + expect(references!.length).greaterThan(0); + + for (const loc of references!) { + expect(path.relative(testWorkspacePath, URI.parse(loc.uri).fsPath)).toMatchSnapshot(); + expect(loc.range).toMatchSnapshot(); + } + } + + async function openDocument(fileName: string, languageId: string, content: string) { + const server = await getLanguageServer(); + const uri = URI.file(`${testWorkspacePath}/${fileName}`); + const document = await server.openInMemoryDocument(uri.toString(), languageId, content); + if (openedDocuments.every(d => d.uri !== document.uri)) { + openedDocuments.push(document); + } + return document; + } +}); diff --git a/test-workspace/language-service/reference/slot-default/entry.vue b/test-workspace/language-service/reference/slot-default/entry.vue deleted file mode 100644 index a09b7689aa..0000000000 --- a/test-workspace/language-service/reference/slot-default/entry.vue +++ /dev/null @@ -1,4 +0,0 @@ - diff --git a/test-workspace/language-service/reference/slot-default/foo.vue b/test-workspace/language-service/reference/slot-default/foo.vue deleted file mode 100644 index 91ed6dfe33..0000000000 --- a/test-workspace/language-service/reference/slot-default/foo.vue +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/test-workspace/language-service/reference/slot-named/entry.vue b/test-workspace/language-service/reference/slot-named/entry.vue deleted file mode 100644 index b4f17d8bc7..0000000000 --- a/test-workspace/language-service/reference/slot-named/entry.vue +++ /dev/null @@ -1,4 +0,0 @@ - diff --git a/test-workspace/language-service/reference/slot-named/foo.vue b/test-workspace/language-service/reference/slot-named/foo.vue deleted file mode 100644 index 0add3c2b7a..0000000000 --- a/test-workspace/language-service/reference/slot-named/foo.vue +++ /dev/null @@ -1,7 +0,0 @@ - - - diff --git a/test-workspace/language-service/reference/v-bind-shorthand/child.vue b/test-workspace/language-service/reference/v-bind-shorthand/child.vue deleted file mode 100644 index 51b389e379..0000000000 --- a/test-workspace/language-service/reference/v-bind-shorthand/child.vue +++ /dev/null @@ -1,6 +0,0 @@ - diff --git a/test-workspace/language-service/reference/v-bind-shorthand/entry.vue b/test-workspace/language-service/reference/v-bind-shorthand/entry.vue deleted file mode 100644 index 5f9a43593c..0000000000 --- a/test-workspace/language-service/reference/v-bind-shorthand/entry.vue +++ /dev/null @@ -1,13 +0,0 @@ - - - diff --git a/vitest.config.ts b/vitest.config.ts index 7f9fdf64dd..c18a84c391 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -2,9 +2,10 @@ import { defineConfig } from 'vitest/config'; export default defineConfig({ test: { + include: ['packages/language-server/tests/**/*.spec.ts'], poolOptions: { - threads: { - singleThread: true, + forks: { + singleFork: true, isolate: false, }, },