-
-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathextract-annotations.test.ts
More file actions
60 lines (49 loc) · 1.98 KB
/
extract-annotations.test.ts
File metadata and controls
60 lines (49 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { expect, test } from "vitest"
import { splitAnnotationsAndCode } from "./extract-annotations.js"
async function t(comment: string) {
const code = `// ${comment} \nvar xyz = "https://example.com"`
const { annotations } = await splitAnnotationsAndCode(code, "javascript", "!")
return annotations[0]
}
test("extracts basic annotation name", async () => {
const annotation = await t("!foo bar")
expect(annotation.name).toEqual("foo")
})
test("extracts name with parentheses range", async () => {
const annotation = await t("!foo(1) bar")
expect(annotation.name).toEqual("foo")
})
test("extracts name with brackets range", async () => {
const annotation = await t("!foo[1] bar")
expect(annotation.name).toEqual("foo")
})
test("extracts name with simple regex", async () => {
const annotation = await t("!foo[/x/] bar")
expect(annotation.name).toEqual("foo")
})
test("extracts name with regex flags", async () => {
const annotation = await t("!foo[/x/gmi] bar")
expect(annotation.name).toEqual("foo")
})
test("extracts name with regex containing brackets", async () => {
const annotation = await t(`!foo[/xyz[a-z]*/g] bar`)
expect(annotation.name).toEqual("foo")
})
test("extracts name with regex containing parentheses", async () => {
const annotation = await t(`!foo(/(xyz[w]*)/g) bar`)
expect(annotation.name).toEqual("foo")
})
test("extracts name with regex containing nested parentheses", async () => {
const annotation = await t(`!foo(/((xyz)[w]*)/g) bar`)
expect(annotation.name).toEqual("foo")
})
test("extracts name with regex containing escaped slashes", async () => {
const annotation = await t(`!foo[/https?:\\/\\//g] bar`)
expect(annotation.name).toEqual("foo")
})
test("extracts name with complex regex pattern", async () => {
const code = `// !tooltip[/#\\[program\\]/] example \n#[program]`
const { annotations } = await splitAnnotationsAndCode(code, "rust", "!")
const annotation = annotations[0]
expect(annotation.name).toEqual("tooltip")
})