11use 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+ }
220
321#[ test]
422fn 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" ) ;
824}
925
1026#[ test]
1127fn 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" ) ;
1529}
1630
1731#[ test]
1832fn should_leave_multiple_indent_levels ( ) {
1933 // Line 2 is indented another level beyond the
2034 // 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" ) ;
2436}
2537
2638#[ test]
@@ -30,43 +42,27 @@ fn should_ignore_first_line_indent() {
3042 //
3143 // #[doc = "Start way over here
3244 // 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" ) ;
3646}
3747
3848#[ test]
3949fn 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" ) ;
4351}
4452
4553#[ test]
4654fn 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" ) ;
5056}
5157
5258#[ test]
5359fn 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" ) ;
6162}
6263
6364#[ test]
6465fn 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" ) ;
7268}
0 commit comments