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

Commit bd003e4

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

File tree

1 file changed

+184
-1
lines changed

1 file changed

+184
-1
lines changed

tests/tests.rs

+184-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use serde_json::{self, json};
11+
use serde_json::{self, json, Value as JsonValue};
1212

1313
use std::io::Write;
1414
use std::time::Duration;
@@ -1364,3 +1364,186 @@ fn cmd_lens_run() {
13641364

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

0 commit comments

Comments
 (0)