Skip to content

Commit d10b236

Browse files
kinto0facebook-github-bot
authored andcommitted
factor out test_go_to_def
Summary: simplification: will be used in a future diff. - requests is still fairly confusing - open to feedback. making it a map might make it harder to use so I'm keeping it like this for now Reviewed By: SamChou19815 Differential Revision: D75471774 fbshipit-source-id: b9b8b370213f6975b9509a35876b38238288a935
1 parent c5af2a8 commit d10b236

File tree

1 file changed

+75
-97
lines changed

1 file changed

+75
-97
lines changed

pyrefly/lib/test/lsp/lsp_interaction.rs

Lines changed: 75 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
use std::iter::once;
9+
810
use lsp_server::Message;
911
use lsp_server::Notification;
1012
use lsp_server::Request;
@@ -66,112 +68,88 @@ fn test_initialize_with_python_path() {
6668
});
6769
}
6870

69-
fn test_go_to_def(root: &TempDir, workspace_folders: Option<Vec<(String, Url)>>) {
71+
fn test_go_to_def(
72+
root: &TempDir,
73+
workspace_folders: Option<Vec<(String, Url)>>,
74+
// request file name, relative to root
75+
request_file_name: &str,
76+
// (line, character, response_file_name (relative to root), response_line_start, response_character_start, response_line_end, response_character_end)
77+
requests: Vec<(u32, u32, String, u32, u32, u32, u32)>,
78+
) {
7079
run_test_lsp(TestCase {
71-
messages_from_language_client: vec![
72-
Message::from(build_did_open_notification(root.path().join("foo.py"))),
73-
Message::from(Request {
74-
id: RequestId::from(2),
75-
method: "textDocument/definition".to_owned(),
76-
params: serde_json::json!({
77-
"textDocument": {
78-
"uri": Url::from_file_path(root.path().join("foo.py")).unwrap().to_string()
79-
},
80-
"position": {
81-
"line": 6,
82-
"character": 16
83-
}
84-
}),
85-
}),
86-
Message::from(Request {
87-
id: RequestId::from(3),
88-
method: "textDocument/definition".to_owned(),
89-
params: serde_json::json!({
90-
"textDocument": {
91-
"uri": Url::from_file_path(root.path().join("foo.py")).unwrap().to_string()
92-
},
93-
"position": {
94-
"line": 8,
95-
"character": 9
96-
}
97-
}),
98-
}),
99-
Message::from(Request {
100-
id: RequestId::from(4),
101-
method: "textDocument/definition".to_owned(),
102-
params: serde_json::json!({
103-
"textDocument": {
104-
"uri": Url::from_file_path(root.path().join("foo.py")).unwrap().to_string()
105-
},
106-
"position": {
107-
"line": 9,
108-
"character": 7
109-
}
110-
}),
111-
}),
112-
],
113-
expected_messages_from_language_server: vec![
114-
Message::Response(Response {
115-
id: RequestId::from(2),
116-
result: Some(serde_json::json!({
117-
"uri": Url::from_file_path(root.path().join("bar.py")).unwrap().to_string(),
118-
"range": {
119-
"start": {
120-
"line": 6,
121-
"character": 6
80+
messages_from_language_client: once(Message::from(build_did_open_notification(
81+
root.path().join(request_file_name),
82+
))).chain(
83+
requests.iter().enumerate().map(
84+
|(i, (request_line, request_character, _response_file_name, _response_line_start, _response_character_start, _response_line_end, _response_character_end))| {
85+
Message::from(Request {
86+
id: RequestId::from((2 + i) as i32),
87+
method: "textDocument/definition".to_owned(),
88+
params: serde_json::json!({
89+
"textDocument": {
90+
"uri": Url::from_file_path(root.path().join(request_file_name)).unwrap().to_string()
12291
},
123-
"end": {
124-
"line": 6,
125-
"character": 9
92+
"position": {
93+
"line": request_line,
94+
"character": request_character
12695
}
127-
}
128-
})),
129-
error: None,
130-
}),
131-
Message::Response(Response {
132-
id: RequestId::from(3),
133-
result: Some(serde_json::json!({
134-
"uri": Url::from_file_path(root.path().join("bar.py")).unwrap().to_string(),
135-
"range": {
136-
"start": {
137-
"line": 7,
138-
"character": 4
139-
},
140-
"end": {
141-
"line": 7,
142-
"character": 7
143-
}
144-
}
145-
})),
146-
error: None,
147-
}),
148-
Message::Response(Response {
149-
id: RequestId::from(4),
150-
result: Some(serde_json::json!({
151-
"uri": Url::from_file_path(root.path().join("bar.py")).unwrap().to_string(),
152-
"range": {
153-
"start": {
154-
"line": 6,
155-
"character": 6
156-
},
157-
"end": {
158-
"line": 6,
159-
"character": 9
96+
}),
97+
})
98+
})).collect(),
99+
expected_messages_from_language_server: requests.iter().enumerate().map(
100+
|(
101+
i,
102+
(
103+
_request_line,
104+
_request_character,
105+
response_file_name,
106+
response_line_start,
107+
response_character_start,
108+
response_line_end,
109+
response_character_end,
110+
),
111+
)| {
112+
Message::Response(Response {
113+
id: RequestId::from((2 + i) as i32),
114+
result: Some(serde_json::json!({
115+
"uri": Url::from_file_path(root.path().join(response_file_name)).unwrap().to_string(),
116+
"range": {
117+
"start": {
118+
"line": response_line_start,
119+
"character": response_character_start
120+
},
121+
"end": {
122+
"line": response_line_end,
123+
"character": response_character_end
124+
}
160125
}
161-
}
162-
})),
163-
error: None,
164-
}),
165-
],
126+
})),
127+
error: None,
128+
})
129+
},
130+
).collect(),
166131
workspace_folders,
167132
..Default::default()
168133
});
169134
}
170135

136+
fn test_go_to_def_basic(root: &TempDir, workspace_folders: Option<Vec<(String, Url)>>) {
137+
test_go_to_def(
138+
root,
139+
workspace_folders,
140+
"foo.py",
141+
vec![
142+
(6, 16, "bar.py".to_owned(), 6, 6, 6, 9),
143+
(8, 9, "bar.py".to_owned(), 7, 4, 7, 7),
144+
(9, 7, "bar.py".to_owned(), 6, 6, 6, 9),
145+
],
146+
);
147+
}
148+
171149
#[test]
172150
fn test_go_to_def_single_root() {
173151
let root = get_test_files_root();
174-
test_go_to_def(
152+
test_go_to_def_basic(
175153
&root,
176154
Some(vec![(
177155
"test".to_owned(),
@@ -183,19 +161,19 @@ fn test_go_to_def_single_root() {
183161
#[test]
184162
fn test_go_to_def_no_root() {
185163
let root = get_test_files_root();
186-
test_go_to_def(&root, Some(vec![]));
164+
test_go_to_def_basic(&root, Some(vec![]));
187165
}
188166

189167
#[test]
190168
fn test_go_to_def_no_root_uses_upwards_search() {
191169
let root = get_test_files_root();
192-
test_go_to_def(&root, Some(vec![]));
170+
test_go_to_def_basic(&root, Some(vec![]));
193171
}
194172

195173
#[test]
196174
fn test_go_to_def_no_folder_capability() {
197175
let root = get_test_files_root();
198-
test_go_to_def(&root, None);
176+
test_go_to_def_basic(&root, None);
199177
}
200178

201179
#[test]

0 commit comments

Comments
 (0)