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

Commit

Permalink
tests: Add a test for rust-lang/rust#57462 and go-to-definition in ge…
Browse files Browse the repository at this point in the history
…neral.
  • Loading branch information
emilio committed Jan 9, 2019
1 parent ea7a0c3 commit 5c0e545
Showing 1 changed file with 147 additions and 2 deletions.
149 changes: 147 additions & 2 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}

0 comments on commit 5c0e545

Please sign in to comment.