@@ -184,6 +184,7 @@ fn check_struct_shorthand_initialization(
184
184
#[ cfg( test) ]
185
185
mod tests {
186
186
use insta:: assert_debug_snapshot_matches;
187
+ use join_to_string:: join;
187
188
use ra_syntax:: SourceFile ;
188
189
use test_utils:: assert_eq_text;
189
190
@@ -228,9 +229,30 @@ mod tests {
228
229
let edit = fix. source_file_edits . pop ( ) . unwrap ( ) . edit ;
229
230
let target_file_contents = analysis. file_text ( file_position. file_id ) . unwrap ( ) ;
230
231
let actual = edit. apply ( & target_file_contents) ;
231
- assert_eq_text ! ( after, & actual) ;
232
+
233
+ // Strip indent and empty lines from `after`, to match the behaviour of
234
+ // `parse_fixture` called from `analysis_and_position`.
235
+ let margin = fixture
236
+ . lines ( )
237
+ . filter ( |it| it. trim_start ( ) . starts_with ( "//-" ) )
238
+ . map ( |it| it. len ( ) - it. trim_start ( ) . len ( ) )
239
+ . next ( )
240
+ . expect ( "empty fixture" ) ;
241
+ let after = join ( after. lines ( ) . filter_map ( |line| {
242
+ if line. len ( ) > margin {
243
+ Some ( & line[ margin..] )
244
+ } else {
245
+ None
246
+ }
247
+ } ) )
248
+ . separator ( "\n " )
249
+ . suffix ( "\n " )
250
+ . to_string ( ) ;
251
+
252
+ assert_eq_text ! ( & after, & actual) ;
232
253
assert ! (
233
- diagnostic. range. start( ) <= file_position. offset && diagnostic. range. end( ) >= file_position. offset,
254
+ diagnostic. range. start( ) <= file_position. offset
255
+ && diagnostic. range. end( ) >= file_position. offset,
234
256
"diagnostic range {} does not touch cursor position {}" ,
235
257
diagnostic. range,
236
258
file_position. offset
@@ -281,17 +303,16 @@ mod tests {
281
303
pub enum Result<T, E> { Ok(T), Err(E) }
282
304
}
283
305
"# ;
284
- // The formatting here is a bit odd due to how the parse_fixture function works in test_utils -
285
- // it strips empty lines and leading whitespace. The important part of this test is that the final
286
- // `x / y` expr is now wrapped in `Ok(..)`
287
- let after = r#"use std::{string::String, result::Result::{self, Ok, Err}};
288
- fn div(x: i32, y: i32) -> Result<i32, String> {
289
- if y == 0 {
290
- return Err("div by zero".into());
291
- }
292
- Ok(x / y)
293
- }
294
- "# ;
306
+ let after = r#"
307
+ use std::{string::String, result::Result::{self, Ok, Err}};
308
+
309
+ fn div(x: i32, y: i32) -> Result<i32, String> {
310
+ if y == 0 {
311
+ return Err("div by zero".into());
312
+ }
313
+ Ok(x / y)
314
+ }
315
+ "# ;
295
316
check_apply_diagnostic_fix_from_position ( before, after) ;
296
317
}
297
318
@@ -313,17 +334,16 @@ fn div(x: i32, y: i32) -> Result<i32, String> {
313
334
pub enum Result<T, E> { Ok(T), Err(E) }
314
335
}
315
336
"# ;
316
- // The formatting here is a bit odd due to how the parse_fixture function works in test_utils -
317
- // it strips empty lines and leading whitespace. The important part of this test is that the final
318
- // expr is now wrapped in `Ok(..)`
319
- let after = r#"use std::result::Result::{self, Ok, Err};
320
- fn div<T>(x: T) -> Result<T, i32> {
321
- if x == 0 {
322
- return Err(7);
323
- }
324
- Ok(x)
325
- }
326
- "# ;
337
+ let after = r#"
338
+ use std::result::Result::{self, Ok, Err};
339
+
340
+ fn div<T>(x: T) -> Result<T, i32> {
341
+ if x == 0 {
342
+ return Err(7);
343
+ }
344
+ Ok(x)
345
+ }
346
+ "# ;
327
347
check_apply_diagnostic_fix_from_position ( before, after) ;
328
348
}
329
349
@@ -350,18 +370,17 @@ fn div<T>(x: T) -> Result<T, i32> {
350
370
pub enum Result<T, E> { Ok(T), Err(E) }
351
371
}
352
372
"# ;
353
- // The formatting here is a bit odd due to how the parse_fixture function works in test_utils -
354
- // it strips empty lines and leading whitespace. The important part of this test is that the final
355
- // `x / y` expr is now wrapped in `Ok(..)`
356
- let after = r#"use std::{string::String, result::Result::{self, Ok, Err}};
357
- type MyResult<T> = Result<T, String>;
358
- fn div(x: i32, y: i32) -> MyResult<i32> {
359
- if y == 0 {
360
- return Err("div by zero".into());
361
- }
362
- Ok(x / y)
363
- }
364
- "# ;
373
+ let after = r#"
374
+ use std::{string::String, result::Result::{self, Ok, Err}};
375
+
376
+ type MyResult<T> = Result<T, String>;
377
+ fn div(x: i32, y: i32) -> MyResult<i32> {
378
+ if y == 0 {
379
+ return Err("div by zero".into());
380
+ }
381
+ Ok(x / y)
382
+ }
383
+ "# ;
365
384
check_apply_diagnostic_fix_from_position ( before, after) ;
366
385
}
367
386
0 commit comments