rust/crates/ra_lsp_server/tests/heavy_tests/main.rs

264 lines
5.9 KiB
Rust
Raw Normal View History

2018-09-01 19:21:11 +02:00
mod support;
2019-01-10 22:37:10 +01:00
use std::{
collections::HashMap,
time::Instant,
};
2019-01-14 11:55:56 +01:00
use lsp_types::{
2019-01-03 14:43:47 +01:00
CodeActionContext, DocumentFormattingParams, FormattingOptions, Position, Range,
};
use ra_lsp_server::req::{
2019-01-10 18:13:08 +01:00
CodeActionParams, CodeActionRequest, Formatting, Runnables, RunnablesParams, CompletionParams, Completion,
2019-01-03 14:43:47 +01:00
};
use serde_json::json;
2018-09-01 19:21:11 +02:00
2018-10-15 19:15:53 +02:00
use crate::support::project;
2018-09-01 19:21:11 +02:00
2018-09-02 15:36:03 +02:00
const LOG: &'static str = "";
2018-09-02 13:46:15 +02:00
2019-01-10 18:13:08 +01:00
#[test]
fn completes_items_from_standard_library() {
2019-01-10 22:37:10 +01:00
let project_start = Instant::now();
2019-01-10 18:13:08 +01:00
let server = project(
r#"
//- Cargo.toml
[package]
name = "foo"
version = "0.0.0"
//- src/lib.rs
2019-01-10 22:37:10 +01:00
use std::collections::Spam;
2019-01-10 18:13:08 +01:00
"#,
);
server.wait_for_feedback("workspace loaded");
2019-01-10 22:37:10 +01:00
eprintln!("loading took {:?}", project_start.elapsed());
let completion_start = Instant::now();
let res = server.send_request::<Completion>(CompletionParams {
text_document: server.doc_id("src/lib.rs"),
context: None,
position: Position::new(0, 23),
});
assert!(format!("{}", res).contains("HashMap"));
eprintln!("completion took {:?}", completion_start.elapsed());
2019-01-10 18:13:08 +01:00
}
2018-09-01 19:21:11 +02:00
#[test]
2018-09-02 15:36:03 +02:00
fn test_runnables_no_project() {
let server = project(
r"
2018-09-01 19:21:11 +02:00
//- lib.rs
#[test]
fn foo() {
}
",
);
2018-12-19 13:40:42 +01:00
server.wait_for_feedback("workspace loaded");
2018-09-01 19:21:11 +02:00
server.request::<Runnables>(
RunnablesParams {
text_document: server.doc_id("lib.rs"),
position: None,
},
json!([
2018-09-01 19:21:11 +02:00
{
"args": [ "test", "--", "foo", "--nocapture" ],
"bin": "cargo",
"env": { "RUST_BACKTRACE": "short" },
"label": "test foo",
"range": {
"end": { "character": 1, "line": 2 },
"start": { "character": 0, "line": 0 }
}
2018-10-25 12:42:53 +02:00
},
{
"args": [
"check",
"--all"
],
"bin": "cargo",
"env": {},
"label": "cargo check --all",
"range": {
"end": {
"character": 0,
"line": 0
},
"start": {
"character": 0,
"line": 0
}
}
2018-09-01 19:21:11 +02:00
}
]),
2018-09-01 19:21:11 +02:00
);
}
2018-09-02 13:46:15 +02:00
2018-09-02 15:36:03 +02:00
#[test]
fn test_runnables_project() {
let server = project(
r#"
2018-09-02 15:36:03 +02:00
//- Cargo.toml
[package]
name = "foo"
version = "0.0.0"
//- src/lib.rs
pub fn foo() {}
//- tests/spam.rs
#[test]
fn test_eggs() {}
"#,
);
2018-09-03 22:32:42 +02:00
server.wait_for_feedback("workspace loaded");
2018-09-02 15:36:03 +02:00
server.request::<Runnables>(
RunnablesParams {
text_document: server.doc_id("tests/spam.rs"),
position: None,
},
json!([
2018-09-02 15:36:03 +02:00
{
"args": [ "test", "--package", "foo", "--test", "spam", "--", "test_eggs", "--nocapture" ],
"bin": "cargo",
"env": { "RUST_BACKTRACE": "short" },
"label": "test test_eggs",
"range": {
"end": { "character": 17, "line": 1 },
"start": { "character": 0, "line": 0 }
}
2018-10-25 12:42:53 +02:00
},
{
"args": [
"check",
"--package",
"foo",
"--test",
"spam"
],
"bin": "cargo",
"env": {},
"label": "cargo check -p foo",
"range": {
"end": {
"character": 0,
"line": 0
},
"start": {
"character": 0,
"line": 0
}
}
2018-09-02 15:36:03 +02:00
}
])
2018-09-02 15:36:03 +02:00
);
}
2018-12-06 21:26:23 +01:00
#[test]
fn test_format_document() {
let server = project(
r#"
[package]
name = "foo"
version = "0.0.0"
//- src/lib.rs
mod bar;
fn main() {
}
pub use std::collections::HashMap;
"#,
);
server.wait_for_feedback("workspace loaded");
server.request::<Formatting>(
DocumentFormattingParams {
text_document: server.doc_id("src/lib.rs"),
options: FormattingOptions {
tab_size: 4,
insert_spaces: false,
properties: HashMap::new(),
},
},
json!([
{
"newText": r#"mod bar;
fn main() {}
pub use std::collections::HashMap;
"#,
"range": {
"end": {
"character": 0,
"line": 6
},
"start": {
"character": 0,
"line": 0
}
}
}
]),
);
}
2018-12-06 21:26:23 +01:00
#[test]
fn test_missing_module_code_action() {
let server = project(
r#"
//- Cargo.toml
[package]
name = "foo"
version = "0.0.0"
//- src/lib.rs
mod bar;
fn main() {}
"#,
);
server.wait_for_feedback("workspace loaded");
let empty_context = || CodeActionContext {
diagnostics: Vec::new(),
only: None,
};
server.request::<CodeActionRequest>(
CodeActionParams {
text_document: server.doc_id("src/lib.rs"),
2018-12-27 10:54:59 +01:00
range: Range::new(Position::new(0, 4), Position::new(0, 7)),
2018-12-06 21:26:23 +01:00
context: empty_context(),
},
json!([
{
"arguments": [
{
"cursorPosition": null,
2019-01-03 14:43:47 +01:00
"workspaceEdit": {
"documentChanges": [
{
"kind": "create",
2018-12-06 21:26:23 +01:00
"uri": "file:///[..]/src/bar.rs"
2019-01-03 14:43:47 +01:00
}
]
},
"label": "create module"
2018-12-06 21:26:23 +01:00
}
],
2019-01-28 12:43:07 +01:00
"command": "rust-analyzer.applySourceChange",
2018-12-06 21:26:23 +01:00
"title": "create module"
}
]),
);
server.request::<CodeActionRequest>(
CodeActionParams {
text_document: server.doc_id("src/lib.rs"),
2018-12-27 10:54:59 +01:00
range: Range::new(Position::new(2, 4), Position::new(2, 7)),
2018-12-06 21:26:23 +01:00
context: empty_context(),
},
json!([]),
);
}