diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-06 20:57:04 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-06 20:57:04 +0000 |
commit | 66f656134f349f943905cfe44f5005263dc635f9 (patch) | |
tree | 513b56d0c4757f37fa318b964ac1f630e246680b /crates/ra_lsp_server | |
parent | 8e60e751cbcfa47c7bed788dfe2ab5cebfcb78b3 (diff) | |
parent | 29793e7de978c9f0fa2d46bad624fc49d2506b11 (diff) |
Merge #261
261: Add heavy test for code actions r=matklad a=flodiebold
Here's the test for the code actions; I didn't find anything fitting on crates.io ([assert-json-diff](https://crates.io/crates/assert-json-diff) looks kind of nice, but doesn't have anything like the wildcards), so I copied the cargo code as you suggested.
Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/ra_lsp_server')
-rw-r--r-- | crates/ra_lsp_server/tests/heavy_tests/main.rs | 71 | ||||
-rw-r--r-- | crates/ra_lsp_server/tests/heavy_tests/support.rs | 27 |
2 files changed, 80 insertions, 18 deletions
diff --git a/crates/ra_lsp_server/tests/heavy_tests/main.rs b/crates/ra_lsp_server/tests/heavy_tests/main.rs index cbc0c8844..26f5e3f20 100644 --- a/crates/ra_lsp_server/tests/heavy_tests/main.rs +++ b/crates/ra_lsp_server/tests/heavy_tests/main.rs | |||
@@ -1,6 +1,10 @@ | |||
1 | mod support; | 1 | mod support; |
2 | 2 | ||
3 | use ra_lsp_server::req::{Runnables, RunnablesParams}; | 3 | use serde_json::json; |
4 | |||
5 | use ra_lsp_server::req::{Runnables, RunnablesParams, CodeActionRequest, CodeActionParams}; | ||
6 | |||
7 | use languageserver_types::{Position, Range, CodeActionContext}; | ||
4 | 8 | ||
5 | use crate::support::project; | 9 | use crate::support::project; |
6 | 10 | ||
@@ -21,7 +25,7 @@ fn foo() { | |||
21 | text_document: server.doc_id("lib.rs"), | 25 | text_document: server.doc_id("lib.rs"), |
22 | position: None, | 26 | position: None, |
23 | }, | 27 | }, |
24 | r#"[ | 28 | json!([ |
25 | { | 29 | { |
26 | "args": [ "test", "--", "foo", "--nocapture" ], | 30 | "args": [ "test", "--", "foo", "--nocapture" ], |
27 | "bin": "cargo", | 31 | "bin": "cargo", |
@@ -51,7 +55,7 @@ fn foo() { | |||
51 | } | 55 | } |
52 | } | 56 | } |
53 | } | 57 | } |
54 | ]"#, | 58 | ]), |
55 | ); | 59 | ); |
56 | } | 60 | } |
57 | 61 | ||
@@ -78,7 +82,7 @@ fn test_eggs() {} | |||
78 | text_document: server.doc_id("tests/spam.rs"), | 82 | text_document: server.doc_id("tests/spam.rs"), |
79 | position: None, | 83 | position: None, |
80 | }, | 84 | }, |
81 | r#"[ | 85 | json!([ |
82 | { | 86 | { |
83 | "args": [ "test", "--package", "foo", "--test", "spam", "--", "test_eggs", "--nocapture" ], | 87 | "args": [ "test", "--package", "foo", "--test", "spam", "--", "test_eggs", "--nocapture" ], |
84 | "bin": "cargo", | 88 | "bin": "cargo", |
@@ -111,6 +115,63 @@ fn test_eggs() {} | |||
111 | } | 115 | } |
112 | } | 116 | } |
113 | } | 117 | } |
114 | ]"# | 118 | ]) |
119 | ); | ||
120 | } | ||
121 | |||
122 | #[test] | ||
123 | fn test_missing_module_code_action() { | ||
124 | let server = project( | ||
125 | r#" | ||
126 | //- Cargo.toml | ||
127 | [package] | ||
128 | name = "foo" | ||
129 | version = "0.0.0" | ||
130 | |||
131 | //- src/lib.rs | ||
132 | mod bar; | ||
133 | |||
134 | fn main() {} | ||
135 | "#, | ||
136 | ); | ||
137 | server.wait_for_feedback("workspace loaded"); | ||
138 | let empty_context = || CodeActionContext { | ||
139 | diagnostics: Vec::new(), | ||
140 | only: None, | ||
141 | }; | ||
142 | server.request::<CodeActionRequest>( | ||
143 | CodeActionParams { | ||
144 | text_document: server.doc_id("src/lib.rs"), | ||
145 | range: Range::new(Position::new(0, 0), Position::new(0, 7)), | ||
146 | context: empty_context(), | ||
147 | }, | ||
148 | json!([ | ||
149 | { | ||
150 | "arguments": [ | ||
151 | { | ||
152 | "cursorPosition": null, | ||
153 | "fileSystemEdits": [ | ||
154 | { | ||
155 | "type": "createFile", | ||
156 | "uri": "file:///[..]/src/bar.rs" | ||
157 | } | ||
158 | ], | ||
159 | "label": "create module", | ||
160 | "sourceFileEdits": [] | ||
161 | } | ||
162 | ], | ||
163 | "command": "ra-lsp.applySourceChange", | ||
164 | "title": "create module" | ||
165 | } | ||
166 | ]), | ||
167 | ); | ||
168 | |||
169 | server.request::<CodeActionRequest>( | ||
170 | CodeActionParams { | ||
171 | text_document: server.doc_id("src/lib.rs"), | ||
172 | range: Range::new(Position::new(2, 0), Position::new(2, 7)), | ||
173 | context: empty_context(), | ||
174 | }, | ||
175 | json!([]), | ||
115 | ); | 176 | ); |
116 | } | 177 | } |
diff --git a/crates/ra_lsp_server/tests/heavy_tests/support.rs b/crates/ra_lsp_server/tests/heavy_tests/support.rs index 019048a3a..4b75be3ee 100644 --- a/crates/ra_lsp_server/tests/heavy_tests/support.rs +++ b/crates/ra_lsp_server/tests/heavy_tests/support.rs | |||
@@ -15,9 +15,9 @@ use languageserver_types::{ | |||
15 | DidOpenTextDocumentParams, TextDocumentIdentifier, TextDocumentItem, Url, | 15 | DidOpenTextDocumentParams, TextDocumentIdentifier, TextDocumentItem, Url, |
16 | }; | 16 | }; |
17 | use serde::Serialize; | 17 | use serde::Serialize; |
18 | use serde_json::{from_str, to_string_pretty, Value}; | 18 | use serde_json::{to_string_pretty, Value}; |
19 | use tempdir::TempDir; | 19 | use tempdir::TempDir; |
20 | use test_utils::parse_fixture; | 20 | use test_utils::{parse_fixture, find_mismatch}; |
21 | 21 | ||
22 | use ra_lsp_server::{ | 22 | use ra_lsp_server::{ |
23 | main_loop, req, | 23 | main_loop, req, |
@@ -88,23 +88,24 @@ impl Server { | |||
88 | } | 88 | } |
89 | } | 89 | } |
90 | 90 | ||
91 | pub fn request<R>(&self, params: R::Params, expected_resp: &str) | 91 | pub fn request<R>(&self, params: R::Params, expected_resp: Value) |
92 | where | 92 | where |
93 | R: Request, | 93 | R: Request, |
94 | R::Params: Serialize, | 94 | R::Params: Serialize, |
95 | { | 95 | { |
96 | let id = self.req_id.get(); | 96 | let id = self.req_id.get(); |
97 | self.req_id.set(id + 1); | 97 | self.req_id.set(id + 1); |
98 | let expected_resp: Value = from_str(expected_resp).unwrap(); | ||
99 | let actual = self.send_request::<R>(id, params); | 98 | let actual = self.send_request::<R>(id, params); |
100 | assert_eq!( | 99 | match find_mismatch(&expected_resp, &actual) { |
101 | expected_resp, | 100 | Some((expected_part, actual_part)) => panic!( |
102 | actual, | 101 | "JSON mismatch\nExpected:\n{}\nWas:\n{}\nExpected part:\n{}\nActual part:\n{}\n", |
103 | "Expected:\n{}\n\ | 102 | to_string_pretty(&expected_resp).unwrap(), |
104 | Actual:\n{}\n", | 103 | to_string_pretty(&actual).unwrap(), |
105 | to_string_pretty(&expected_resp).unwrap(), | 104 | to_string_pretty(expected_part).unwrap(), |
106 | to_string_pretty(&actual).unwrap(), | 105 | to_string_pretty(actual_part).unwrap(), |
107 | ); | 106 | ), |
107 | None => {} | ||
108 | } | ||
108 | } | 109 | } |
109 | 110 | ||
110 | fn send_request<R>(&self, id: u64, params: R::Params) -> Value | 111 | fn send_request<R>(&self, id: u64, params: R::Params) -> Value |
@@ -139,7 +140,7 @@ impl Server { | |||
139 | pub fn wait_for_feedback_n(&self, feedback: &str, n: usize) { | 140 | pub fn wait_for_feedback_n(&self, feedback: &str, n: usize) { |
140 | let f = |msg: &RawMessage| match msg { | 141 | let f = |msg: &RawMessage| match msg { |
141 | RawMessage::Notification(n) if n.method == "internalFeedback" => { | 142 | RawMessage::Notification(n) if n.method == "internalFeedback" => { |
142 | return n.clone().cast::<req::InternalFeedback>().unwrap() == feedback | 143 | return n.clone().cast::<req::InternalFeedback>().unwrap() == feedback; |
143 | } | 144 | } |
144 | _ => false, | 145 | _ => false, |
145 | }; | 146 | }; |