Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit e9e643b

Browse files
committed
tests: Add a test for rust-lang/rust#57462 and go-to-definition without racer in general.
1 parent d7c2eb8 commit e9e643b

File tree

1 file changed

+182
-1
lines changed

1 file changed

+182
-1
lines changed

tests/tests.rs

Lines changed: 182 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use serde_json::{self, json};
1+
use serde_json::{self, json, Value as JsonValue};
22

33
use std::io::Write;
44
use std::time::Duration;
@@ -1354,3 +1354,184 @@ fn cmd_lens_run() {
13541354

13551355
rls.shutdown(rls_timeout());
13561356
}
1357+
1358+
#[test]
1359+
fn test_find_definitions() {
1360+
const SRC: &str = r#"
1361+
struct Foo {
1362+
}
1363+
1364+
impl Foo {
1365+
fn new() {
1366+
}
1367+
}
1368+
1369+
fn main() {
1370+
Foo::new();
1371+
}
1372+
"#;
1373+
1374+
let p = project("simple_workspace")
1375+
.file("Cargo.toml", &basic_bin_manifest("bar"))
1376+
.file("src/main.rs", SRC)
1377+
.build();
1378+
1379+
let root_path = p.root();
1380+
let mut rls = p.spawn_rls();
1381+
1382+
rls.request(
1383+
0,
1384+
"initialize",
1385+
Some(json!({
1386+
"rootPath": root_path,
1387+
"capabilities": {},
1388+
"initializationOptions": {
1389+
"settings": {
1390+
"rust": {
1391+
"racer_completion": false
1392+
}
1393+
}
1394+
}
1395+
})),
1396+
)
1397+
.unwrap();
1398+
1399+
rls.wait_until_done_indexing(rls_timeout());
1400+
1401+
let uri = format!("file://{}/src/main.rs", root_path.display());
1402+
1403+
let mut results = vec![];
1404+
let mut request_id = 1;
1405+
for (line_index, line) in SRC.lines().enumerate() {
1406+
for i in 0..line.len() {
1407+
rls.request(
1408+
request_id,
1409+
"textDocument/definition",
1410+
Some(json!({
1411+
"position": {
1412+
"character": i,
1413+
"line": line_index
1414+
},
1415+
"textDocument": {
1416+
"uri": uri,
1417+
"version": 1
1418+
}
1419+
})),
1420+
)
1421+
.unwrap();
1422+
1423+
let json = rls.wait_until_json_id(request_id, rls_timeout());
1424+
let result = json["result"].as_array().unwrap();
1425+
1426+
request_id += 1;
1427+
1428+
if result.is_empty() {
1429+
continue;
1430+
}
1431+
1432+
results.push((
1433+
line_index,
1434+
i,
1435+
result
1436+
.iter()
1437+
.map(|definition| definition["range"].clone())
1438+
.collect::<Vec<_>>(),
1439+
));
1440+
}
1441+
}
1442+
1443+
rls.shutdown(rls_timeout());
1444+
1445+
// Foo
1446+
let foo_definition: JsonValue = json!({
1447+
"start": {
1448+
"line": 1,
1449+
"character": 15,
1450+
},
1451+
"end": {
1452+
"line": 1,
1453+
"character": 18,
1454+
}
1455+
});
1456+
1457+
// Foo::new
1458+
let foo_new_definition: JsonValue = json!({
1459+
"start": {
1460+
"line": 5,
1461+
"character": 15,
1462+
},
1463+
"end": {
1464+
"line": 5,
1465+
"character": 18,
1466+
}
1467+
});
1468+
1469+
1470+
// main
1471+
let main_definition: JsonValue = json!({
1472+
"start": {
1473+
"line": 9,
1474+
"character": 11,
1475+
},
1476+
"end": {
1477+
"line": 9,
1478+
"character": 15,
1479+
}
1480+
});
1481+
1482+
let expected = [
1483+
// struct Foo
1484+
(1, 15, vec![foo_definition.clone()]),
1485+
(1, 16, vec![foo_definition.clone()]),
1486+
(1, 17, vec![foo_definition.clone()]),
1487+
(1, 18, vec![foo_definition.clone()]),
1488+
// impl Foo
1489+
(4, 13, vec![foo_definition.clone()]),
1490+
(4, 14, vec![foo_definition.clone()]),
1491+
(4, 15, vec![foo_definition.clone()]),
1492+
(4, 16, vec![foo_definition.clone()]),
1493+
1494+
// fn new
1495+
(5, 15, vec![foo_new_definition.clone()]),
1496+
(5, 16, vec![foo_new_definition.clone()]),
1497+
(5, 17, vec![foo_new_definition.clone()]),
1498+
(5, 18, vec![foo_new_definition.clone()]),
1499+
1500+
// fn main
1501+
(9, 11, vec![main_definition.clone()]),
1502+
(9, 12, vec![main_definition.clone()]),
1503+
(9, 13, vec![main_definition.clone()]),
1504+
(9, 14, vec![main_definition.clone()]),
1505+
(9, 15, vec![main_definition.clone()]),
1506+
1507+
// Foo::new()
1508+
(10, 12, vec![foo_definition.clone()]),
1509+
(10, 13, vec![foo_definition.clone()]),
1510+
(10, 14, vec![foo_definition.clone()]),
1511+
(10, 15, vec![foo_definition.clone()]),
1512+
(10, 17, vec![foo_new_definition.clone()]),
1513+
(10, 18, vec![foo_new_definition.clone()]),
1514+
(10, 19, vec![foo_new_definition.clone()]),
1515+
(10, 20, vec![foo_new_definition.clone()]),
1516+
];
1517+
1518+
if results.len() != expected.len() {
1519+
panic!(
1520+
"Got different amount of completions than expected: {} vs. {}: {:#?}",
1521+
results.len(),
1522+
expected.len(),
1523+
results
1524+
)
1525+
}
1526+
1527+
for (i, (actual, expected)) in results.iter().zip(expected.iter()).enumerate() {
1528+
if actual != expected {
1529+
panic!(
1530+
"Found different definition at index {}. Got {:#?}, expected {:#?}",
1531+
i,
1532+
actual,
1533+
expected
1534+
)
1535+
}
1536+
}
1537+
}

0 commit comments

Comments
 (0)