Add test for doc comments unindent fix
This commit is contained in:
parent
06fe278669
commit
b4c35368f4
2 changed files with 51 additions and 32 deletions
|
@ -1,26 +1,38 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use rustc_span::source_map::DUMMY_SP;
|
||||||
|
|
||||||
|
fn create_doc_fragment(s: &str) -> Vec<DocFragment> {
|
||||||
|
vec![DocFragment {
|
||||||
|
line: 0,
|
||||||
|
span: DUMMY_SP,
|
||||||
|
parent_module: None,
|
||||||
|
doc: s.to_string(),
|
||||||
|
kind: DocFragmentKind::SugaredDoc,
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
|
fn run_test(input: &str, expected: &str) {
|
||||||
|
let mut s = create_doc_fragment(input);
|
||||||
|
unindent_fragments(&mut s);
|
||||||
|
assert_eq!(s[0].doc, expected);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_unindent() {
|
fn should_unindent() {
|
||||||
let s = " line1\n line2".to_string();
|
run_test(" line1\n line2", "line1\nline2");
|
||||||
let r = unindent(&s);
|
|
||||||
assert_eq!(r, "line1\nline2");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_unindent_multiple_paragraphs() {
|
fn should_unindent_multiple_paragraphs() {
|
||||||
let s = " line1\n\n line2".to_string();
|
run_test(" line1\n\n line2", "line1\n\nline2");
|
||||||
let r = unindent(&s);
|
|
||||||
assert_eq!(r, "line1\n\nline2");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_leave_multiple_indent_levels() {
|
fn should_leave_multiple_indent_levels() {
|
||||||
// Line 2 is indented another level beyond the
|
// Line 2 is indented another level beyond the
|
||||||
// base indentation and should be preserved
|
// base indentation and should be preserved
|
||||||
let s = " line1\n\n line2".to_string();
|
run_test(" line1\n\n line2", "line1\n\n line2");
|
||||||
let r = unindent(&s);
|
|
||||||
assert_eq!(r, "line1\n\n line2");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -30,43 +42,27 @@ fn should_ignore_first_line_indent() {
|
||||||
//
|
//
|
||||||
// #[doc = "Start way over here
|
// #[doc = "Start way over here
|
||||||
// and continue here"]
|
// and continue here"]
|
||||||
let s = "line1\n line2".to_string();
|
run_test("line1\n line2", "line1\nline2");
|
||||||
let r = unindent(&s);
|
|
||||||
assert_eq!(r, "line1\nline2");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_not_ignore_first_line_indent_in_a_single_line_para() {
|
fn should_not_ignore_first_line_indent_in_a_single_line_para() {
|
||||||
let s = "line1\n\n line2".to_string();
|
run_test("line1\n\n line2", "line1\n\n line2");
|
||||||
let r = unindent(&s);
|
|
||||||
assert_eq!(r, "line1\n\n line2");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_unindent_tabs() {
|
fn should_unindent_tabs() {
|
||||||
let s = "\tline1\n\tline2".to_string();
|
run_test("\tline1\n\tline2", "line1\nline2");
|
||||||
let r = unindent(&s);
|
|
||||||
assert_eq!(r, "line1\nline2");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_trim_mixed_indentation() {
|
fn should_trim_mixed_indentation() {
|
||||||
let s = "\t line1\n\t line2".to_string();
|
run_test("\t line1\n\t line2", "line1\nline2");
|
||||||
let r = unindent(&s);
|
run_test(" \tline1\n \tline2", "line1\nline2");
|
||||||
assert_eq!(r, "line1\nline2");
|
|
||||||
|
|
||||||
let s = " \tline1\n \tline2".to_string();
|
|
||||||
let r = unindent(&s);
|
|
||||||
assert_eq!(r, "line1\nline2");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_not_trim() {
|
fn should_not_trim() {
|
||||||
let s = "\t line1 \n\t line2".to_string();
|
run_test("\t line1 \n\t line2", "line1 \nline2");
|
||||||
let r = unindent(&s);
|
run_test(" \tline1 \n \tline2", "line1 \nline2");
|
||||||
assert_eq!(r, "line1 \nline2");
|
|
||||||
|
|
||||||
let s = " \tline1 \n \tline2".to_string();
|
|
||||||
let r = unindent(&s);
|
|
||||||
assert_eq!(r, "line1 \nline2");
|
|
||||||
}
|
}
|
||||||
|
|
23
src/test/rustdoc/unindent.rs
Normal file
23
src/test/rustdoc/unindent.rs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#![crate_name = "foo"]
|
||||||
|
|
||||||
|
// @has foo/struct.Example.html
|
||||||
|
// @matches - '//pre[@class="rust rust-example-rendered"]' \
|
||||||
|
// '(?m)let example = Example::new\(\)\n \.first\(\)\n \.second\(\)\n \.build\(\);\Z'
|
||||||
|
/// ```rust
|
||||||
|
/// let example = Example::new()
|
||||||
|
/// .first()
|
||||||
|
#[cfg_attr(not(feature = "one"), doc = " .second()")]
|
||||||
|
/// .build();
|
||||||
|
/// ```
|
||||||
|
pub struct Example;
|
||||||
|
|
||||||
|
// @has foo/struct.F.html
|
||||||
|
// @matches - '//pre[@class="rust rust-example-rendered"]' \
|
||||||
|
// '(?m)let example = Example::new\(\)\n \.first\(\)\n \.another\(\)\n \.build\(\);\Z'
|
||||||
|
///```rust
|
||||||
|
///let example = Example::new()
|
||||||
|
/// .first()
|
||||||
|
#[cfg_attr(not(feature = "one"), doc = " .another()")]
|
||||||
|
/// .build();
|
||||||
|
/// ```
|
||||||
|
pub struct F;
|
Loading…
Add table
Add a link
Reference in a new issue