Skip to content

Commit b4c3536

Browse files
Add test for doc comments unindent fix
1 parent 06fe278 commit b4c3536

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
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+
}
220

321
#[test]
422
fn should_unindent() {
5-
let s = " line1\n line2".to_string();
6-
let r = unindent(&s);
7-
assert_eq!(r, "line1\nline2");
23+
run_test(" line1\n line2", "line1\nline2");
824
}
925

1026
#[test]
1127
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\nline2");
28+
run_test(" line1\n\n line2", "line1\n\nline2");
1529
}
1630

1731
#[test]
1832
fn 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\nline2");
45+
run_test("line1\n line2", "line1\nline2");
3646
}
3747

3848
#[test]
3949
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");
4351
}
4452

4553
#[test]
4654
fn should_unindent_tabs() {
47-
let s = "\tline1\n\tline2".to_string();
48-
let r = unindent(&s);
49-
assert_eq!(r, "line1\nline2");
55+
run_test("\tline1\n\tline2", "line1\nline2");
5056
}
5157

5258
#[test]
5359
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\nline2");
57-
58-
let s = " \tline1\n \tline2".to_string();
59-
let r = unindent(&s);
60-
assert_eq!(r, "line1\nline2");
60+
run_test("\t line1\n\t line2", "line1\nline2");
61+
run_test(" \tline1\n \tline2", "line1\nline2");
6162
}
6263

6364
#[test]
6465
fn should_not_trim() {
65-
let s = "\t line1 \n\t line2".to_string();
66-
let r = unindent(&s);
67-
assert_eq!(r, "line1 \nline2");
68-
69-
let s = " \tline1 \n \tline2".to_string();
70-
let r = unindent(&s);
71-
assert_eq!(r, "line1 \nline2");
66+
run_test("\t line1 \n\t line2", "line1 \nline2");
67+
run_test(" \tline1 \n \tline2", "line1 \nline2");
7268
}

src/test/rustdoc/unindent.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![crate_name = "foo"]
2+
3+
// @has foo/struct.Example.html
4+
// @matches - '//pre[@class="rust rust-example-rendered"]' \
5+
// '(?m)let example = Example::new\(\)\n \.first\(\)\n \.second\(\)\n \.build\(\);\Z'
6+
/// ```rust
7+
/// let example = Example::new()
8+
/// .first()
9+
#[cfg_attr(not(feature = "one"), doc = " .second()")]
10+
/// .build();
11+
/// ```
12+
pub struct Example;
13+
14+
// @has foo/struct.F.html
15+
// @matches - '//pre[@class="rust rust-example-rendered"]' \
16+
// '(?m)let example = Example::new\(\)\n \.first\(\)\n \.another\(\)\n \.build\(\);\Z'
17+
///```rust
18+
///let example = Example::new()
19+
/// .first()
20+
#[cfg_attr(not(feature = "one"), doc = " .another()")]
21+
/// .build();
22+
/// ```
23+
pub struct F;

0 commit comments

Comments
 (0)