Skip to content

Commit 8f8f293

Browse files
committed
Add basic indexer tests
1 parent bff2232 commit 8f8f293

5 files changed

+316
-0
lines changed

crates/ark/src/lsp/indexer.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,96 @@ fn index_comment(_path: &Path, contents: &Rope, node: &Node) -> anyhow::Result<O
340340
data: IndexEntryData::Section { level, title },
341341
}))
342342
}
343+
#[cfg(test)]
344+
mod tests {
345+
use std::path::PathBuf;
346+
347+
use insta::assert_debug_snapshot;
348+
349+
use super::*;
350+
use crate::lsp::documents::Document;
351+
352+
macro_rules! test_index {
353+
($code:expr) => {
354+
let doc = Document::new($code, None);
355+
let path = PathBuf::from("/path/to/file.R");
356+
let root = doc.ast.root_node();
357+
let mut cursor = root.walk();
358+
359+
let mut entries = vec![];
360+
for node in root.children(&mut cursor) {
361+
if let Ok(Some(entry)) = index_node(&path, &doc.contents, &node) {
362+
entries.push(entry);
363+
}
364+
}
365+
assert_debug_snapshot!(entries);
366+
};
367+
}
368+
369+
// Note that unlike document symbols whose ranges cover the whole entity
370+
// they represent, the range of workspace symbols only cover the identifers
371+
372+
#[test]
373+
fn test_index_function() {
374+
test_index!(
375+
r#"
376+
my_function <- function(a, b = 1) {
377+
a + b
378+
379+
# These are not indexed as workspace symbol
380+
inner <- function() {
381+
2
382+
}
383+
inner_var <- 3
384+
}
385+
386+
my_variable <- 1
387+
"#
388+
);
389+
}
390+
391+
#[test]
392+
fn test_index_variable() {
393+
test_index!(
394+
r#"
395+
x <- 10
396+
y = "hello"
397+
"#
398+
);
399+
}
400+
401+
#[test]
402+
fn test_index_s7_methods() {
403+
test_index!(
404+
r#"
405+
Class <- new_class("Class")
406+
generic <- new_generic("generic", "arg",
407+
function(arg) {
408+
S7_dispatch()
409+
}
410+
)
411+
method(generic, Class) <- function(arg) {
412+
NULL
413+
}
414+
"#
415+
);
416+
}
417+
418+
#[test]
419+
fn test_index_comment_section() {
420+
test_index!(
421+
r#"
422+
# Section 1 ----
423+
x <- 10
424+
425+
## Subsection ======
426+
y <- 20
427+
428+
x <- function() {
429+
# This inner section is not indexed ----
430+
}
431+
432+
"#
433+
);
434+
}
435+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
source: crates/ark/src/lsp/indexer.rs
3+
expression: entries
4+
---
5+
[
6+
IndexEntry {
7+
key: "Section 1",
8+
range: Range {
9+
start: Position {
10+
line: 1,
11+
character: 0,
12+
},
13+
end: Position {
14+
line: 1,
15+
character: 16,
16+
},
17+
},
18+
data: Section {
19+
level: 1,
20+
title: "Section 1",
21+
},
22+
},
23+
IndexEntry {
24+
key: "x",
25+
range: Range {
26+
start: Position {
27+
line: 2,
28+
character: 0,
29+
},
30+
end: Position {
31+
line: 2,
32+
character: 1,
33+
},
34+
},
35+
data: Variable {
36+
name: "x",
37+
},
38+
},
39+
IndexEntry {
40+
key: "Subsection",
41+
range: Range {
42+
start: Position {
43+
line: 4,
44+
character: 0,
45+
},
46+
end: Position {
47+
line: 4,
48+
character: 20,
49+
},
50+
},
51+
data: Section {
52+
level: 2,
53+
title: "Subsection",
54+
},
55+
},
56+
IndexEntry {
57+
key: "y",
58+
range: Range {
59+
start: Position {
60+
line: 5,
61+
character: 0,
62+
},
63+
end: Position {
64+
line: 5,
65+
character: 1,
66+
},
67+
},
68+
data: Variable {
69+
name: "y",
70+
},
71+
},
72+
IndexEntry {
73+
key: "x",
74+
range: Range {
75+
start: Position {
76+
line: 7,
77+
character: 0,
78+
},
79+
end: Position {
80+
line: 7,
81+
character: 1,
82+
},
83+
},
84+
data: Function {
85+
name: "x",
86+
arguments: [],
87+
},
88+
},
89+
]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
source: crates/ark/src/lsp/indexer.rs
3+
expression: entries
4+
---
5+
[
6+
IndexEntry {
7+
key: "my_function",
8+
range: Range {
9+
start: Position {
10+
line: 1,
11+
character: 0,
12+
},
13+
end: Position {
14+
line: 1,
15+
character: 11,
16+
},
17+
},
18+
data: Function {
19+
name: "my_function",
20+
arguments: [
21+
"a",
22+
"b",
23+
],
24+
},
25+
},
26+
IndexEntry {
27+
key: "my_variable",
28+
range: Range {
29+
start: Position {
30+
line: 11,
31+
character: 0,
32+
},
33+
end: Position {
34+
line: 11,
35+
character: 11,
36+
},
37+
},
38+
data: Variable {
39+
name: "my_variable",
40+
},
41+
},
42+
]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
source: crates/ark/src/lsp/indexer.rs
3+
expression: entries
4+
---
5+
[
6+
IndexEntry {
7+
key: "Class",
8+
range: Range {
9+
start: Position {
10+
line: 1,
11+
character: 0,
12+
},
13+
end: Position {
14+
line: 1,
15+
character: 5,
16+
},
17+
},
18+
data: Variable {
19+
name: "Class",
20+
},
21+
},
22+
IndexEntry {
23+
key: "generic",
24+
range: Range {
25+
start: Position {
26+
line: 2,
27+
character: 0,
28+
},
29+
end: Position {
30+
line: 2,
31+
character: 7,
32+
},
33+
},
34+
data: Variable {
35+
name: "generic",
36+
},
37+
},
38+
IndexEntry {
39+
key: "method(generic, Class)",
40+
range: Range {
41+
start: Position {
42+
line: 7,
43+
character: 0,
44+
},
45+
end: Position {
46+
line: 7,
47+
character: 22,
48+
},
49+
},
50+
data: Variable {
51+
name: "method(generic, Class)",
52+
},
53+
},
54+
]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
source: crates/ark/src/lsp/indexer.rs
3+
expression: entries
4+
---
5+
[
6+
IndexEntry {
7+
key: "x",
8+
range: Range {
9+
start: Position {
10+
line: 1,
11+
character: 0,
12+
},
13+
end: Position {
14+
line: 1,
15+
character: 1,
16+
},
17+
},
18+
data: Variable {
19+
name: "x",
20+
},
21+
},
22+
IndexEntry {
23+
key: "y",
24+
range: Range {
25+
start: Position {
26+
line: 2,
27+
character: 0,
28+
},
29+
end: Position {
30+
line: 2,
31+
character: 1,
32+
},
33+
},
34+
data: Variable {
35+
name: "y",
36+
},
37+
},
38+
]

0 commit comments

Comments
 (0)