From 5c0e54574a3b6bb8ebe7cb01df0acb1450db66a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Wed, 9 Jan 2019 19:38:04 +0100 Subject: [PATCH] tests: Add a test for rust-lang/rust#57462 and go-to-definition in general. --- tests/tests.rs | 149 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 2 deletions(-) diff --git a/tests/tests.rs b/tests/tests.rs index 50f3303c4ca..b5e06b64264 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use serde_json::{self, json}; +use serde_json::{self, json, Value as JsonValue}; use std::io::Write; use std::time::Duration; @@ -648,7 +648,7 @@ fn cmd_test_complete_self_crate_name() { std::thread::sleep(Duration::from_millis(50)); continue; } - }; + } assert_eq!(json["result"][0]["detail"], "pub fn function() -> usize"); rls.shutdown(rls_timeout()); @@ -1364,3 +1364,148 @@ fn cmd_lens_run() { rls.shutdown(rls_timeout()); } + +#[test] +fn test_find_definitions() { + const SRC: &str = r#" + struct Foo { + } + + impl Foo { + fn new() { + } + } + + fn main() { + Foo::new(); + } + "#; + + let p = project("simple_workspace") + .file("Cargo.toml", &basic_bin_manifest("bar")) + .file("src/main.rs", SRC) + .build(); + + let root_path = p.root(); + let mut rls = p.spawn_rls(); + + rls.request( + 0, + "initialize", + Some(json!({ + "rootPath": root_path, + "capabilities": {}, + "initializationOptions": { + "settings": { + "rust": { + "racer_completion": false + } + } + } + })), + ) + .unwrap(); + + let _ = rls.wait_until_json_id(0, rls_timeout()); + + let uri = format!("file://{}/src/main.rs", root_path.display()); + + let mut results = vec![]; + let mut request_id = 1; + for (line_index, line) in SRC.lines().enumerate() { + for i in 0..line.len() { + rls.request( + request_id, + "textDocument/definition", + Some(json!({ + "position": { + "character": i, + "line": line_index + }, + "textDocument": { + "uri": uri, + "version": 1 + } + })), + ) + .unwrap(); + + let json = rls.wait_until_json_id(request_id, rls_timeout()); + let result = json["result"].as_array().unwrap(); + + request_id += 1; + + if result.is_empty() { + continue; + } + + results.push((line_index, i, result.clone())); + } + } + + rls.shutdown(rls_timeout()); + + // Foo + let foo_definition: JsonValue = json!({ + "uri": uri, + "range": { + "start": { + "line": 1, + "character": 15, + }, + "end": { + "line": 1, + "character": 18, + } + } + }); + + // Foo::new + let foo_new_definition: JsonValue = json!({ + "uri": uri, + "range": { + "start": { + "line": 5, + "character": 15, + }, + "end": { + "line": 5, + "character": 18, + } + } + }); + + + // main + let main_definition: JsonValue = json!({ + "uri": uri, + "range": { + "start": { + "line": 9, + "character": 11, + }, + "end": { + "line": 9, + "character": 15, + } + } + }); + + let expected = [ + (9, 11, vec![main_definition.clone()]), + (9, 12, vec![main_definition.clone()]), + (9, 13, vec![main_definition.clone()]), + (9, 14, vec![main_definition.clone()]), + (9, 15, vec![main_definition.clone()]), + (10, 12, vec![foo_definition.clone()]), + (10, 13, vec![foo_definition.clone()]), + (10, 14, vec![foo_definition.clone()]), + (10, 15, vec![foo_definition.clone()]), + (10, 17, vec![foo_new_definition.clone()]), + (10, 18, vec![foo_new_definition.clone()]), + (10, 19, vec![foo_new_definition.clone()]), + (10, 20, vec![foo_new_definition.clone()]), + ]; + + assert_eq!(results, expected); +}