1
1
use super :: * ;
2
+ use rustc_span:: source_map:: DUMMY_SP ;
3
+
4
+ fn create_doc_fragment ( s : & str ) -> Vec < DocFragment > {
5
+ vec ! [ DocFragment {
6
+ line: 0 ,
7
+ span: DUMMY_SP ,
8
+ parent_module: None ,
9
+ doc: s. to_string( ) ,
10
+ kind: DocFragmentKind :: SugaredDoc ,
11
+ } ]
12
+ }
13
+
14
+ #[ track_caller]
15
+ fn run_test ( input : & str , expected : & str ) {
16
+ let mut s = create_doc_fragment ( input) ;
17
+ unindent_fragments ( & mut s) ;
18
+ assert_eq ! ( s[ 0 ] . doc, expected) ;
19
+ }
2
20
3
21
#[ test]
4
22
fn should_unindent ( ) {
5
- let s = " line1\n line2" . to_string ( ) ;
6
- let r = unindent ( & s) ;
7
- assert_eq ! ( r, "line1\n line2" ) ;
23
+ run_test ( " line1\n line2" , "line1\n line2" ) ;
8
24
}
9
25
10
26
#[ test]
11
27
fn should_unindent_multiple_paragraphs ( ) {
12
- let s = " line1\n \n line2" . to_string ( ) ;
13
- let r = unindent ( & s) ;
14
- assert_eq ! ( r, "line1\n \n line2" ) ;
28
+ run_test ( " line1\n \n line2" , "line1\n \n line2" ) ;
15
29
}
16
30
17
31
#[ test]
18
32
fn should_leave_multiple_indent_levels ( ) {
19
33
// Line 2 is indented another level beyond the
20
34
// base indentation and should be preserved
21
- let s = " line1\n \n line2" . to_string ( ) ;
22
- let r = unindent ( & s) ;
23
- assert_eq ! ( r, "line1\n \n line2" ) ;
35
+ run_test ( " line1\n \n line2" , "line1\n \n line2" ) ;
24
36
}
25
37
26
38
#[ test]
@@ -30,43 +42,27 @@ fn should_ignore_first_line_indent() {
30
42
//
31
43
// #[doc = "Start way over here
32
44
// and continue here"]
33
- let s = "line1\n line2" . to_string ( ) ;
34
- let r = unindent ( & s) ;
35
- assert_eq ! ( r, "line1\n line2" ) ;
45
+ run_test ( "line1\n line2" , "line1\n line2" ) ;
36
46
}
37
47
38
48
#[ test]
39
49
fn should_not_ignore_first_line_indent_in_a_single_line_para ( ) {
40
- let s = "line1\n \n line2" . to_string ( ) ;
41
- let r = unindent ( & s) ;
42
- assert_eq ! ( r, "line1\n \n line2" ) ;
50
+ run_test ( "line1\n \n line2" , "line1\n \n line2" ) ;
43
51
}
44
52
45
53
#[ test]
46
54
fn should_unindent_tabs ( ) {
47
- let s = "\t line1\n \t line2" . to_string ( ) ;
48
- let r = unindent ( & s) ;
49
- assert_eq ! ( r, "line1\n line2" ) ;
55
+ run_test ( "\t line1\n \t line2" , "line1\n line2" ) ;
50
56
}
51
57
52
58
#[ test]
53
59
fn should_trim_mixed_indentation ( ) {
54
- let s = "\t line1\n \t line2" . to_string ( ) ;
55
- let r = unindent ( & s) ;
56
- assert_eq ! ( r, "line1\n line2" ) ;
57
-
58
- let s = " \t line1\n \t line2" . to_string ( ) ;
59
- let r = unindent ( & s) ;
60
- assert_eq ! ( r, "line1\n line2" ) ;
60
+ run_test ( "\t line1\n \t line2" , "line1\n line2" ) ;
61
+ run_test ( " \t line1\n \t line2" , "line1\n line2" ) ;
61
62
}
62
63
63
64
#[ test]
64
65
fn should_not_trim ( ) {
65
- let s = "\t line1 \n \t line2" . to_string ( ) ;
66
- let r = unindent ( & s) ;
67
- assert_eq ! ( r, "line1 \n line2" ) ;
68
-
69
- let s = " \t line1 \n \t line2" . to_string ( ) ;
70
- let r = unindent ( & s) ;
71
- assert_eq ! ( r, "line1 \n line2" ) ;
66
+ run_test ( "\t line1 \n \t line2" , "line1 \n line2" ) ;
67
+ run_test ( " \t line1 \n \t line2" , "line1 \n line2" ) ;
72
68
}
0 commit comments