|
1 |
| -use serde_json::{self, json}; |
| 1 | +use serde_json::{self, json, Value as JsonValue}; |
2 | 2 |
|
3 | 3 | use std::io::Write;
|
4 | 4 | use std::time::Duration;
|
@@ -1354,3 +1354,186 @@ fn cmd_lens_run() {
|
1354 | 1354 |
|
1355 | 1355 | rls.shutdown(rls_timeout());
|
1356 | 1356 | }
|
| 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((line_index, i, result.clone())); |
| 1433 | + } |
| 1434 | + } |
| 1435 | + |
| 1436 | + rls.shutdown(rls_timeout()); |
| 1437 | + |
| 1438 | + // Foo |
| 1439 | + let foo_definition: JsonValue = json!({ |
| 1440 | + "uri": uri, |
| 1441 | + "range": { |
| 1442 | + "start": { |
| 1443 | + "line": 1, |
| 1444 | + "character": 15, |
| 1445 | + }, |
| 1446 | + "end": { |
| 1447 | + "line": 1, |
| 1448 | + "character": 18, |
| 1449 | + } |
| 1450 | + } |
| 1451 | + }); |
| 1452 | + |
| 1453 | + // Foo::new |
| 1454 | + let foo_new_definition: JsonValue = json!({ |
| 1455 | + "uri": uri, |
| 1456 | + "range": { |
| 1457 | + "start": { |
| 1458 | + "line": 5, |
| 1459 | + "character": 15, |
| 1460 | + }, |
| 1461 | + "end": { |
| 1462 | + "line": 5, |
| 1463 | + "character": 18, |
| 1464 | + } |
| 1465 | + } |
| 1466 | + }); |
| 1467 | + |
| 1468 | + |
| 1469 | + // main |
| 1470 | + let main_definition: JsonValue = json!({ |
| 1471 | + "uri": uri, |
| 1472 | + "range": { |
| 1473 | + "start": { |
| 1474 | + "line": 9, |
| 1475 | + "character": 11, |
| 1476 | + }, |
| 1477 | + "end": { |
| 1478 | + "line": 9, |
| 1479 | + "character": 15, |
| 1480 | + } |
| 1481 | + } |
| 1482 | + }); |
| 1483 | + |
| 1484 | + let expected = [ |
| 1485 | + // struct Foo |
| 1486 | + (1, 15, vec![foo_definition.clone()]), |
| 1487 | + (1, 16, vec![foo_definition.clone()]), |
| 1488 | + (1, 17, vec![foo_definition.clone()]), |
| 1489 | + (1, 18, vec![foo_definition.clone()]), |
| 1490 | + // impl Foo |
| 1491 | + (4, 13, vec![foo_definition.clone()]), |
| 1492 | + (4, 14, vec![foo_definition.clone()]), |
| 1493 | + (4, 15, vec![foo_definition.clone()]), |
| 1494 | + (4, 16, vec![foo_definition.clone()]), |
| 1495 | + |
| 1496 | + // fn new |
| 1497 | + (5, 15, vec![foo_new_definition.clone()]), |
| 1498 | + (5, 16, vec![foo_new_definition.clone()]), |
| 1499 | + (5, 17, vec![foo_new_definition.clone()]), |
| 1500 | + (5, 18, vec![foo_new_definition.clone()]), |
| 1501 | + |
| 1502 | + // fn main |
| 1503 | + (9, 11, vec![main_definition.clone()]), |
| 1504 | + (9, 12, vec![main_definition.clone()]), |
| 1505 | + (9, 13, vec![main_definition.clone()]), |
| 1506 | + (9, 14, vec![main_definition.clone()]), |
| 1507 | + (9, 15, vec![main_definition.clone()]), |
| 1508 | + |
| 1509 | + // Foo::new() |
| 1510 | + (10, 12, vec![foo_definition.clone()]), |
| 1511 | + (10, 13, vec![foo_definition.clone()]), |
| 1512 | + (10, 14, vec![foo_definition.clone()]), |
| 1513 | + (10, 15, vec![foo_definition.clone()]), |
| 1514 | + (10, 17, vec![foo_new_definition.clone()]), |
| 1515 | + (10, 18, vec![foo_new_definition.clone()]), |
| 1516 | + (10, 19, vec![foo_new_definition.clone()]), |
| 1517 | + (10, 20, vec![foo_new_definition.clone()]), |
| 1518 | + ]; |
| 1519 | + |
| 1520 | + if results.len() != expected.len() { |
| 1521 | + panic!( |
| 1522 | + "Got different amount of completions than expected: {} vs. {}: {:#?}", |
| 1523 | + results.len(), |
| 1524 | + expected.len(), |
| 1525 | + results |
| 1526 | + ) |
| 1527 | + } |
| 1528 | + |
| 1529 | + for (i, (actual, expected)) in results.iter().zip(expected.iter()).enumerate() { |
| 1530 | + if actual != expected { |
| 1531 | + panic!( |
| 1532 | + "Found different definition at index {}. Got {:#?}, expected {:#?}", |
| 1533 | + i, |
| 1534 | + actual, |
| 1535 | + expected |
| 1536 | + ) |
| 1537 | + } |
| 1538 | + } |
| 1539 | +} |
0 commit comments