Skip to content

Commit e39ab06

Browse files
so1vejohnsoncodehk
andauthored
test: add tests for reference (#3546)
Co-authored-by: Johnson Chu <[email protected]>
1 parent 8034286 commit e39ab06

File tree

7 files changed

+102
-3
lines changed

7 files changed

+102
-3
lines changed

.vscode/settings.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,5 @@
1616
"[jsonc]": {
1717
"editor.defaultFormatter": "vscode.json-language-features"
1818
},
19-
"[vue]": {
20-
"editor.defaultFormatter": "Vue.volar"
21-
},
2219
"vue.server.path": "./packages/vue-language-server/bin/vue-language-server.js",
2320
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import './complete';
22
import './findDefinition';
33
import './rename';
4+
import './reference';
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { describe, expect, it } from 'vitest';
2+
import * as path from 'path';
3+
import { tester } from './utils/createTester';
4+
import { TextDocument } from 'vscode-languageserver-textdocument';
5+
import * as fs from 'fs';
6+
7+
const baseDir = path.resolve(__dirname, '../../vue-test-workspace/reference');
8+
const testDirs = fs.readdirSync(baseDir);
9+
10+
for (const dirName of testDirs) {
11+
12+
describe(`find reference: ${dirName}`, async () => {
13+
14+
const dir = path.join(baseDir, dirName);
15+
const inputFiles = readFiles(dir);
16+
17+
for (const file in inputFiles) {
18+
19+
const filePath = path.join(dir, file);
20+
const uri = tester.fileNameToUri(filePath);
21+
const fileText = inputFiles[file];
22+
const document = TextDocument.create('', '', 0, fileText);
23+
const actions = findActions(fileText);
24+
25+
for (const action of actions) {
26+
27+
const position = document.positionAt(action.offset);
28+
29+
position.line--;
30+
31+
const location = `${filePath}:${position.line + 1}:${position.character + 1}`;
32+
33+
it(`${location} => count: ${action.count}`, async () => {
34+
35+
const locations = await tester.languageService.findReferences(
36+
uri,
37+
position,
38+
);
39+
40+
expect(locations).toBeDefined();
41+
42+
expect(locations?.length).toBe(action.count);
43+
});
44+
}
45+
}
46+
});
47+
}
48+
49+
function readFiles(dir: string) {
50+
51+
const filesText: Record<string, string> = {};
52+
const files = fs.readdirSync(dir);
53+
54+
for (const file of files) {
55+
const filePath = path.join(dir, file);
56+
filesText[file] = fs.readFileSync(filePath, 'utf8');
57+
}
58+
59+
return filesText;
60+
}
61+
62+
const referenceReg = /(\^*)reference:\s*([\S]*)/g;
63+
64+
function findActions(text: string) {
65+
66+
return [...text.matchAll(referenceReg)].map(flag => {
67+
68+
const offset = flag.index!;
69+
// The definition itself is also counted
70+
const count = Number(flag[2]) + 1;
71+
72+
return {
73+
offset,
74+
count,
75+
};
76+
});
77+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<template>
2+
<slot></slot>
3+
<!-- ^reference: 1 -->
4+
</template>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script setup lang="ts">
2+
import Entry from './entry.vue';
3+
</script>
4+
5+
<template>
6+
<Entry>
7+
<div></div>
8+
</Entry>
9+
</template>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<template>
2+
<slot name="foo"></slot>
3+
<!-- ^reference: 1 -->
4+
</template>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script setup lang="ts">
2+
import Entry from './entry.vue';
3+
</script>
4+
5+
<template>
6+
<Entry #foo></Entry>
7+
</template>

0 commit comments

Comments
 (0)