rust/crates/rust-analyzer/tests/heavy_tests/main.rs

628 lines
15 KiB
Rust
Raw Normal View History

2018-09-01 19:21:11 +02:00
mod support;
use std::{collections::HashMap, time::Instant};
2019-01-10 22:37:10 +01:00
2019-01-14 11:55:56 +01:00
use lsp_types::{
CodeActionContext, DidOpenTextDocumentParams, DocumentFormattingParams, FormattingOptions,
PartialResultParams, Position, Range, TextDocumentItem, TextDocumentPositionParams,
WorkDoneProgressParams,
2019-01-03 14:43:47 +01:00
};
2020-02-18 12:33:16 +01:00
use rust_analyzer::req::{
CodeActionParams, CodeActionRequest, Completion, CompletionParams, DidOpenTextDocument,
Formatting, GotoDefinition, OnEnter, Runnables, RunnablesParams,
2019-01-03 14:43:47 +01:00
};
use serde_json::json;
use tempfile::TempDir;
2019-12-07 12:46:36 +01:00
use test_utils::skip_slow_tests;
2018-09-01 19:21:11 +02:00
use crate::support::{project, Project};
2018-09-01 19:21:11 +02:00
const PROFILE: &str = "";
2019-05-29 09:33:09 +02:00
// const PROFILE: &'static str = "*@3>100";
2018-09-02 13:46:15 +02:00
2019-01-10 18:13:08 +01:00
#[test]
fn completes_items_from_standard_library() {
2019-12-07 12:46:36 +01:00
if skip_slow_tests() {
return;
}
2019-01-10 22:37:10 +01:00
let project_start = Instant::now();
2019-08-19 14:41:18 +02:00
let server = Project::with_fixture(
2019-01-10 18:13:08 +01:00
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
"#,
2019-08-19 14:41:18 +02:00
)
.with_sysroot(true)
.server();
server.wait_until_workspace_is_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 {
2019-07-07 20:13:13 +02:00
text_document_position: TextDocumentPositionParams::new(
server.doc_id("src/lib.rs"),
Position::new(0, 23),
),
2019-01-10 22:37:10 +01:00
context: None,
partial_result_params: PartialResultParams::default(),
work_done_progress_params: WorkDoneProgressParams::default(),
2019-01-10 22:37:10 +01:00
});
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() {
2019-12-07 12:46:36 +01:00
if skip_slow_tests() {
return;
}
let server = project(
r"
2018-09-01 19:21:11 +02:00
//- lib.rs
#[test]
fn foo() {
}
",
);
server.wait_until_workspace_is_loaded();
2018-09-01 19:21:11 +02:00
server.request::<Runnables>(
2019-02-08 12:49:43 +01:00
RunnablesParams { text_document: server.doc_id("lib.rs"), position: None },
json!([
2018-09-01 19:21:11 +02:00
{
2020-03-09 22:06:45 +01:00
"args": [ "test" ],
"extraArgs": [ "foo", "--nocapture" ],
2018-09-01 19:21:11 +02:00
"bin": "cargo",
"env": { "RUST_BACKTRACE": "short" },
"cwd": null,
2018-09-01 19:21:11 +02:00
"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"
],
2020-03-09 22:06:45 +01:00
"extraArgs": [],
2018-10-25 12:42:53 +02:00
"bin": "cargo",
"env": {},
"cwd": null,
2018-10-25 12:42:53 +02:00
"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() {
2019-12-07 12:46:36 +01:00
if skip_slow_tests() {
return;
}
let code = r#"
//- foo/Cargo.toml
2018-09-02 15:36:03 +02:00
[package]
name = "foo"
version = "0.0.0"
//- foo/src/lib.rs
2018-09-02 15:36:03 +02:00
pub fn foo() {}
//- foo/tests/spam.rs
2018-09-02 15:36:03 +02:00
#[test]
fn test_eggs() {}
//- bar/Cargo.toml
[package]
name = "bar"
version = "0.0.0"
//- bar/src/main.rs
fn main() {}
"#;
let server = Project::with_fixture(code).root("foo").root("bar").server();
server.wait_until_workspace_is_loaded();
2018-09-02 15:36:03 +02:00
server.request::<Runnables>(
RunnablesParams { text_document: server.doc_id("foo/tests/spam.rs"), position: None },
json!([
2018-09-02 15:36:03 +02:00
{
2020-03-09 22:06:45 +01:00
"args": [ "test", "--package", "foo", "--test", "spam" ],
"extraArgs": [ "test_eggs", "--exact", "--nocapture" ],
2018-09-02 15:36:03 +02:00
"bin": "cargo",
"env": { "RUST_BACKTRACE": "short" },
"label": "test test_eggs",
"range": {
"end": { "character": 17, "line": 1 },
"start": { "character": 0, "line": 0 }
},
"cwd": server.path().join("foo")
2018-10-25 12:42:53 +02:00
},
{
"args": [
"check",
"--package",
"foo",
"--test",
"spam"
],
2020-03-09 22:06:45 +01:00
"extraArgs": [],
2018-10-25 12:42:53 +02:00
"bin": "cargo",
"env": {},
"cwd": server.path().join("foo"),
2018-10-25 12:42:53 +02:00
"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() {
2019-12-07 12:46:36 +01:00
if skip_slow_tests() {
return;
}
let server = project(
r#"
//- Cargo.toml
[package]
name = "foo"
version = "0.0.0"
//- src/lib.rs
mod bar;
fn main() {
}
pub use std::collections::HashMap;
"#,
);
server.wait_until_workspace_is_loaded();
server.request::<Formatting>(
DocumentFormattingParams {
text_document: server.doc_id("src/lib.rs"),
options: FormattingOptions {
tab_size: 4,
insert_spaces: false,
insert_final_newline: None,
trim_final_newlines: None,
trim_trailing_whitespace: None,
properties: HashMap::new(),
},
work_done_progress_params: WorkDoneProgressParams::default(),
},
json!([
{
"newText": r#"mod bar;
fn main() {}
pub use std::collections::HashMap;
"#,
"range": {
"end": {
"character": 0,
2019-08-20 17:53:59 +02:00
"line": 7
},
"start": {
"character": 0,
"line": 0
}
}
}
]),
);
}
#[test]
fn test_format_document_2018() {
2019-12-07 12:46:36 +01:00
if skip_slow_tests() {
return;
}
let server = project(
r#"
//- Cargo.toml
[package]
name = "foo"
version = "0.0.0"
edition = "2018"
//- src/lib.rs
mod bar;
async fn test() {
}
fn main() {
}
pub use std::collections::HashMap;
"#,
);
server.wait_until_workspace_is_loaded();
server.request::<Formatting>(
DocumentFormattingParams {
text_document: server.doc_id("src/lib.rs"),
options: FormattingOptions {
tab_size: 4,
insert_spaces: false,
properties: HashMap::new(),
insert_final_newline: None,
trim_final_newlines: None,
trim_trailing_whitespace: None,
},
work_done_progress_params: WorkDoneProgressParams::default(),
},
json!([
{
"newText": r#"mod bar;
async fn test() {}
fn main() {}
pub use std::collections::HashMap;
"#,
"range": {
"end": {
"character": 0,
"line": 10
},
"start": {
"character": 0,
"line": 0
}
}
}
]),
);
}
2019-12-07 12:46:36 +01:00
2018-12-06 21:26:23 +01:00
#[test]
fn test_missing_module_code_action() {
2019-12-07 12:46:36 +01:00
if skip_slow_tests() {
return;
}
2018-12-06 21:26:23 +01:00
let server = project(
r#"
//- Cargo.toml
[package]
name = "foo"
version = "0.0.0"
//- src/lib.rs
mod bar;
fn main() {}
"#,
);
server.wait_until_workspace_is_loaded();
2019-02-08 12:49:43 +01:00
let empty_context = || CodeActionContext { diagnostics: Vec::new(), only: None };
server.request::<CodeActionRequest>(
CodeActionParams {
text_document: server.doc_id("src/lib.rs"),
range: Range::new(Position::new(0, 4), Position::new(0, 7)),
context: empty_context(),
partial_result_params: PartialResultParams::default(),
work_done_progress_params: WorkDoneProgressParams::default(),
},
json!([
{
"command": {
"arguments": [
{
"cursorPosition": null,
"label": "create module",
"workspaceEdit": {
"documentChanges": [
{
"kind": "create",
"uri": "file:///[..]/src/bar.rs"
}
]
}
}
],
"command": "rust-analyzer.applySourceChange",
"title": "create module"
},
"title": "create module"
}
]),
);
server.request::<CodeActionRequest>(
CodeActionParams {
text_document: server.doc_id("src/lib.rs"),
range: Range::new(Position::new(2, 4), Position::new(2, 7)),
context: empty_context(),
partial_result_params: PartialResultParams::default(),
work_done_progress_params: WorkDoneProgressParams::default(),
},
json!([]),
);
}
#[test]
fn test_missing_module_code_action_in_json_project() {
2019-12-07 12:46:36 +01:00
if skip_slow_tests() {
return;
}
let tmp_dir = TempDir::new().unwrap();
let path = tmp_dir.path();
let project = json!({
"roots": [path],
"crates": [ {
"root_module": path.join("src/lib.rs"),
"deps": [],
"edition": "2015",
"atom_cfgs": [],
"key_value_cfgs": {}
} ]
});
let code = format!(
r#"
//- rust-project.json
{PROJECT}
//- src/lib.rs
mod bar;
fn main() {{}}
"#,
PROJECT = project.to_string(),
);
let server = Project::with_fixture(&code).tmp_dir(tmp_dir).server();
server.wait_until_workspace_is_loaded();
let empty_context = || CodeActionContext { diagnostics: Vec::new(), only: None };
2018-12-06 21:26:23 +01:00
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(),
partial_result_params: PartialResultParams::default(),
work_done_progress_params: WorkDoneProgressParams::default(),
2018-12-06 21:26:23 +01:00
},
json!([
2019-02-24 11:53:35 +01:00
{
"command": {
2018-12-06 21:26:23 +01:00
"arguments": [
{
"cursorPosition": null,
2019-02-24 11:53:35 +01:00
"label": "create module",
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
}
]
2019-02-24 11:53:35 +01:00
}
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"
2019-02-24 11:53:35 +01:00
},
"title": "create module"
}
2018-12-06 21:26:23 +01:00
]),
);
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(),
partial_result_params: PartialResultParams::default(),
work_done_progress_params: WorkDoneProgressParams::default(),
2018-12-06 21:26:23 +01:00
},
json!([]),
);
}
2019-05-27 13:20:11 +02:00
#[test]
fn diagnostics_dont_block_typing() {
2019-12-07 12:46:36 +01:00
if skip_slow_tests() {
return;
}
2019-05-27 13:20:11 +02:00
let librs: String = (0..10).map(|i| format!("mod m{};", i)).collect();
let libs: String = (0..10).map(|i| format!("//- src/m{}.rs\nfn foo() {{}}\n\n", i)).collect();
2019-08-19 14:41:18 +02:00
let server = Project::with_fixture(&format!(
2019-05-27 13:20:11 +02:00
r#"
//- Cargo.toml
[package]
name = "foo"
version = "0.0.0"
//- src/lib.rs
{}
{}
fn main() {{}}
"#,
librs, libs
2019-08-19 14:41:18 +02:00
))
.with_sysroot(true)
.server();
2019-05-27 13:20:11 +02:00
server.wait_until_workspace_is_loaded();
for i in 0..10 {
server.notification::<DidOpenTextDocument>(DidOpenTextDocumentParams {
text_document: TextDocumentItem {
uri: server.doc_id(&format!("src/m{}.rs", i)).uri,
language_id: "rust".to_string(),
version: 0,
text: "/// Docs\nfn foo() {}".to_string(),
},
});
}
let start = std::time::Instant::now();
server.request::<OnEnter>(
TextDocumentPositionParams {
text_document: server.doc_id("src/m0.rs"),
position: Position { line: 0, character: 5 },
},
json!({
"cursorPosition": {
"position": { "character": 4, "line": 1 },
"textDocument": { "uri": "file:///[..]src/m0.rs" }
},
"label": "on enter",
"workspaceEdit": {
"documentChanges": [
{
"edits": [
{
"newText": "\n/// ",
"range": {
"end": { "character": 5, "line": 0 },
"start": { "character": 5, "line": 0 }
}
}
],
"textDocument": { "uri": "file:///[..]src/m0.rs", "version": null }
}
]
}
}),
);
let elapsed = start.elapsed();
assert!(elapsed.as_millis() < 2000, "typing enter took {:?}", elapsed);
2019-05-27 13:20:11 +02:00
}
2019-08-20 17:26:07 +02:00
#[test]
fn preserves_dos_line_endings() {
2019-12-07 12:46:36 +01:00
if skip_slow_tests() {
return;
}
2019-08-20 17:26:07 +02:00
let server = Project::with_fixture(
2019-08-20 17:53:59 +02:00
&"
2019-08-20 17:26:07 +02:00
//- Cargo.toml
[package]
2019-08-20 17:53:59 +02:00
name = \"foo\"
version = \"0.0.0\"
2019-08-20 17:26:07 +02:00
//- src/main.rs
/// Some Docs\r\nfn main() {}
2019-08-20 17:53:59 +02:00
",
2019-08-20 17:26:07 +02:00
)
.server();
server.request::<OnEnter>(
TextDocumentPositionParams {
text_document: server.doc_id("src/main.rs"),
position: Position { line: 0, character: 8 },
},
json!({
"cursorPosition": {
"position": { "line": 1, "character": 4 },
"textDocument": { "uri": "file:///[..]src/main.rs" }
},
"label": "on enter",
"workspaceEdit": {
"documentChanges": [
{
"edits": [
{
"newText": "\r\n/// ",
"range": {
"end": { "line": 0, "character": 8 },
"start": { "line": 0, "character": 8 }
}
}
],
"textDocument": { "uri": "file:///[..]src/main.rs", "version": null }
}
]
}
}),
);
}
#[test]
fn resolve_include_concat_env() {
if skip_slow_tests() {
return;
}
let server = Project::with_fixture(
r###"
//- Cargo.toml
[package]
name = "foo"
version = "0.0.0"
//- build.rs
use std::{env, fs, path::Path};
fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("hello.rs");
fs::write(
&dest_path,
r#"pub fn message() -> &'static str { "Hello, World!" }"#,
)
.unwrap();
println!("cargo:rerun-if-changed=build.rs");
}
//- src/main.rs
include!(concat!(env!("OUT_DIR"), "/hello.rs"));
fn main() { message(); }
"###,
)
.with_config(|config| {
config.cargo_features.load_out_dirs_from_check = true;
})
.server();
server.wait_until_workspace_is_loaded();
let res = server.send_request::<GotoDefinition>(TextDocumentPositionParams::new(
server.doc_id("src/main.rs"),
Position::new(2, 15),
));
assert!(format!("{}", res).contains("hello.rs"));
}