Skip to content

Commit

Permalink
Rewrite inlay hints tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Aug 24, 2024
1 parent 117ce52 commit 1c864d3
Show file tree
Hide file tree
Showing 16 changed files with 352 additions and 346 deletions.
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@
},
"pnpm": {
"overrides": {
"@volar/kit": "https://pkg.pr.new/volarjs/volar.js/@volar/kit@01441c3",
"@volar/language-core": "https://pkg.pr.new/volarjs/volar.js/@volar/language-core@01441c3",
"@volar/language-server": "https://pkg.pr.new/volarjs/volar.js/@volar/language-server@01441c3",
"@volar/language-service": "https://pkg.pr.new/volarjs/volar.js/@volar/language-service@01441c3",
"@volar/source-map": "https://pkg.pr.new/volarjs/volar.js/@volar/source-map@01441c3",
"@volar/typescript": "https://pkg.pr.new/volarjs/volar.js/@volar/typescript@01441c3",
"@volar/vscode": "https://pkg.pr.new/volarjs/volar.js/@volar/vscode@01441c3",
"@volar/kit": "https://pkg.pr.new/volarjs/volar.js/@volar/kit@87ad0b2",
"@volar/language-core": "https://pkg.pr.new/volarjs/volar.js/@volar/language-core@87ad0b2",
"@volar/language-server": "https://pkg.pr.new/volarjs/volar.js/@volar/language-server@87ad0b2",
"@volar/language-service": "https://pkg.pr.new/volarjs/volar.js/@volar/language-service@87ad0b2",
"@volar/source-map": "https://pkg.pr.new/volarjs/volar.js/@volar/source-map@87ad0b2",
"@volar/test-utils": "https://pkg.pr.new/volarjs/volar.js/@volar/test-utils@87ad0b2",
"@volar/typescript": "https://pkg.pr.new/volarjs/volar.js/@volar/typescript@87ad0b2",
"@volar/vscode": "https://pkg.pr.new/volarjs/volar.js/@volar/vscode@87ad0b2",
"volar-service-typescript": "https://pkg.pr.new/volarjs/services/volar-service-typescript@177b9ed",
"inquirer": "9.2.23"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Definitions > Destructured props 1`] = `
"
<script setup lang="ts">
import { watch } from 'vue';
const { foo, bar, ...props } = defineProps<{
foo: string;
bar: string;
baz: string;
}>();
type foo = foo[string] & typeof [props.]foo;
interface foo extends (typeof [props.]foo) {
foo: string;
foo(foo: string): void;
foo: (foo: string) => void;
}
const obj = {
foo: [props.]foo,
[[props.]foo]: '',
foo[: props.foo],
foo(foo) { },
foo: function (foo) { },
get bar() { return this.foo; },
set bar(val) { this.foo = val; }
};
function func(foo) { }
class cls {
foo: string = [props.]foo;
constructor(foo) { }
}
for (const char of [props.]foo) { }
try { } catch (foo) { }
watch(() => [props.]foo, (foo) => {
console.log(foo, [props.]bar, props.baz);
});
</script>
"
`;

exports[`Definitions > Inline handler leading 1`] = `
"
<script setup lang="ts">
let a = 0;
</script>
<template>
<div @click="[$event =>]a = 1"></div>
</template>
"
`;
exports[`Definitions > Missing props 1`] = `
"
<script setup lang="ts">
import Foo from './foo.vue';
</script>
<template>
<Foo[foo!]></Foo>
</template>
"
`;

exports[`Definitions > Options wrapper 1`] = `
"
<script>
export default [(await import('vue')).defineComponent(]{}[)];
</script>
"
`;
1 change: 1 addition & 0 deletions packages/language-server/tests/completions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ describe('Completions', async () => {

async function assertCompletion(fileName: string, languageId: string, content: string, itemLabel: string) {
const offset = content.indexOf('|');
expect(offset).toBeGreaterThanOrEqual(0);
content = content.slice(0, offset) + content.slice(offset + 1);

const server = await getLanguageServer();
Expand Down
1 change: 1 addition & 0 deletions packages/language-server/tests/definitions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('Definitions', async () => {

async function assertDefinition(fileName: string, languageId: string, content: string) {
const offset = content.indexOf('|');
expect(offset).toBeGreaterThanOrEqual(0);
content = content.slice(0, offset) + content.slice(offset + 1);

const server = await getLanguageServer();
Expand Down
143 changes: 143 additions & 0 deletions packages/language-server/tests/inlayHints.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { TextDocument } from '@volar/language-server';
import { afterEach, describe, expect, it } from 'vitest';
import { URI } from 'vscode-uri';
import { getLanguageServer, testWorkspacePath } from './server.js';

describe('Definitions', async () => {

it('Inline handler leading', async () => {
await ensureGlobalTypesHolder('tsconfigProject');
await assertInlayHints('tsconfigProject/fixture.vue', 'vue', `
<script setup lang="ts">
let a = 0;
</script>
<template>
<div @click="a = 1"></div>
</template>
`);
});

it('Missing props', async () => {
await ensureGlobalTypesHolder('tsconfigProject');
openDocument('tsconfigProject/foo.vue', 'vue', `
<script setup lang="ts">
defineProps<{
foo: number;
}>();
</script>
`);
await assertInlayHints('tsconfigProject/fixture.vue', 'vue', `
<script setup lang="ts">
import Foo from './foo.vue';
</script>
<template>
<Foo></Foo>
</template>
`);
});

it('Options wrapper', async () => {
await ensureGlobalTypesHolder('tsconfigProject');
await assertInlayHints('tsconfigProject/fixture.vue', 'vue', `
<script>
export default {};
</script>
`);
});

it('Destructured props', async () => {
await ensureGlobalTypesHolder('tsconfigProject');
await assertInlayHints('tsconfigProject/fixture.vue', 'vue', `
<script setup lang="ts">
import { watch } from 'vue';
const { foo, bar, ...props } = defineProps<{
foo: string;
bar: string;
baz: string;
}>();
type foo = foo[string] & typeof foo;
interface foo extends (typeof foo) {
foo: string;
foo(foo: string): void;
foo: (foo: string) => void;
}
const obj = {
foo: foo,
[foo]: '',
foo,
foo(foo) { },
foo: function (foo) { },
get bar() { return this.foo; },
set bar(val) { this.foo = val; }
};
function func(foo) { }
class cls {
foo: string = foo;
constructor(foo) { }
}
for (const char of foo) { }
try { } catch (foo) { }
watch(() => foo, (foo) => {
console.log(foo, bar, props.baz);
});
</script>
`);
});

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 assertInlayHints(fileName: string, languageId: string, content: string) {
const server = await getLanguageServer();
let document = await openDocument(fileName, languageId, content);

const inlayHints = await server.sendInlayHintRequest(document.uri, { start: document.positionAt(0), end: document.positionAt(content.length) });
expect(inlayHints).toBeDefined();
expect(inlayHints!.length).greaterThan(0);

let text = document.getText();
for (const hint of inlayHints!.sort((a, b) => document.offsetAt(b.position) - document.offsetAt(a.position))) {
const offset = document.offsetAt(hint.position);
text = text.slice(0, offset) + '[' + hint.label + ']' + text.slice(offset);
}

expect(text).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;
}
});
1 change: 1 addition & 0 deletions packages/language-server/tests/references.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('Definitions', async () => {

async function assertReferences(fileName: string, languageId: string, content: string) {
const offset = content.indexOf('|');
expect(offset).toBeGreaterThanOrEqual(0);
content = content.slice(0, offset) + content.slice(offset + 1);

const server = await getLanguageServer();
Expand Down
13 changes: 13 additions & 0 deletions packages/language-server/tests/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export async function getLanguageServer() {
if (!serverHandle) {
serverHandle = startLanguageServer(require.resolve('../bin/vue-language-server.js'), testWorkspacePath);
serverHandle.connection.onNotification('textDocument/publishDiagnostics', () => { });
serverHandle.connection.onRequest('workspace/configuration', ({ items }) => {
return items.map(({ section }) => {

Check failure on line 15 in packages/language-server/tests/server.ts

View workflow job for this annotation

GitHub Actions / build (18, macos-latest)

Binding element 'section' implicitly has an 'any' type.

Check failure on line 15 in packages/language-server/tests/server.ts

View workflow job for this annotation

GitHub Actions / build

Binding element 'section' implicitly has an 'any' type.

Check failure on line 15 in packages/language-server/tests/server.ts

View workflow job for this annotation

GitHub Actions / build (18, ubuntu-latest)

Binding element 'section' implicitly has an 'any' type.
if (section.startsWith('vue.inlayHints.')) {
return true;
}
return null;
});
});

await serverHandle.initialize(
URI.file(testWorkspacePath).toString(),
Expand All @@ -22,6 +30,11 @@ export async function getLanguageServer() {
vue: {
hybridMode: false,
},
},
{
workspace: {
configuration: true,
},
}
);
}
Expand Down
80 changes: 0 additions & 80 deletions packages/language-service/tests/inlayHint.ts

This file was deleted.

Loading

0 comments on commit 1c864d3

Please sign in to comment.