5
5
* LICENSE file in the root directory of this source tree.
6
6
*/
7
7
8
+ use std:: iter:: once;
9
+
8
10
use lsp_server:: Message ;
9
11
use lsp_server:: Notification ;
10
12
use lsp_server:: Request ;
@@ -66,112 +68,88 @@ fn test_initialize_with_python_path() {
66
68
} ) ;
67
69
}
68
70
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
+ ) {
70
79
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( )
122
91
} ,
123
- "end " : {
124
- "line" : 6 ,
125
- "character" : 9
92
+ "position " : {
93
+ "line" : request_line ,
94
+ "character" : request_character
126
95
}
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
+ }
160
125
}
161
- }
162
- } ) ) ,
163
- error : None ,
164
- } ) ,
165
- ] ,
126
+ } ) ) ,
127
+ error : None ,
128
+ } )
129
+ } ,
130
+ ) . collect ( ) ,
166
131
workspace_folders,
167
132
..Default :: default ( )
168
133
} ) ;
169
134
}
170
135
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
+
171
149
#[ test]
172
150
fn test_go_to_def_single_root ( ) {
173
151
let root = get_test_files_root ( ) ;
174
- test_go_to_def (
152
+ test_go_to_def_basic (
175
153
& root,
176
154
Some ( vec ! [ (
177
155
"test" . to_owned( ) ,
@@ -183,19 +161,19 @@ fn test_go_to_def_single_root() {
183
161
#[ test]
184
162
fn test_go_to_def_no_root ( ) {
185
163
let root = get_test_files_root ( ) ;
186
- test_go_to_def ( & root, Some ( vec ! [ ] ) ) ;
164
+ test_go_to_def_basic ( & root, Some ( vec ! [ ] ) ) ;
187
165
}
188
166
189
167
#[ test]
190
168
fn test_go_to_def_no_root_uses_upwards_search ( ) {
191
169
let root = get_test_files_root ( ) ;
192
- test_go_to_def ( & root, Some ( vec ! [ ] ) ) ;
170
+ test_go_to_def_basic ( & root, Some ( vec ! [ ] ) ) ;
193
171
}
194
172
195
173
#[ test]
196
174
fn test_go_to_def_no_folder_capability ( ) {
197
175
let root = get_test_files_root ( ) ;
198
- test_go_to_def ( & root, None ) ;
176
+ test_go_to_def_basic ( & root, None ) ;
199
177
}
200
178
201
179
#[ test]
0 commit comments