Skip to content

doc-test: In Markdown tests, Use all of <h1> to <h6> as the test name #44867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ parameter if so.
"##,

E0154: r##"
## Note: this error code is no longer emitted by the compiler.
#### Note: this error code is no longer emitted by the compiler.

Imports (`use` statements) are not allowed after non-item statements, such as
variable declarations and expression statements.
Expand Down Expand Up @@ -79,7 +79,7 @@ https://doc.rust-lang.org/reference.html#statements
"##,

E0251: r##"
## Note: this error code is no longer emitted by the compiler.
#### Note: this error code is no longer emitted by the compiler.

Two items of the same name cannot be imported without rebinding one of the
items under a new local name.
Expand Down Expand Up @@ -268,7 +268,7 @@ fn main() {
"##,

E0256: r##"
## Note: this error code is no longer emitted by the compiler.
#### Note: this error code is no longer emitted by the compiler.

You can't import a type or module when the name of the item being imported is
the same as another type or submodule defined in the module.
Expand Down
74 changes: 47 additions & 27 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,33 @@ pub struct Collector {
pub tests: Vec<testing::TestDescAndFn>,
// to be removed when hoedown will be definitely gone
pub old_tests: HashMap<String, Vec<String>>,

// The name of the test displayed to the user, separated by `::`.
//
// In tests from Rust source, this is the path to the item
// e.g. `["std", "vec", "Vec", "push"]`.
//
// In tests from a markdown file, this is the titles of all headers (h1~h6)
// of the sections that contain the code block, e.g. if the markdown file is
// written as:
//
// ``````markdown
// # Title
//
// ## Subtitle
//
// ```rust
// assert!(true);
// ```
// ``````
//
// the `names` vector of that test will be `["Title", "Subtitle"]`.
names: Vec<String>,

cfgs: Vec<String>,
libs: SearchPaths,
externs: Externs,
cnt: usize,
use_headers: bool,
current_header: Option<String>,
cratename: String,
opts: TestOptions,
maybe_sysroot: Option<PathBuf>,
Expand All @@ -436,9 +456,7 @@ impl Collector {
cfgs,
libs,
externs,
cnt: 0,
use_headers,
current_header: None,
cratename,
opts,
maybe_sysroot,
Expand All @@ -450,28 +468,12 @@ impl Collector {
}

fn generate_name(&self, line: usize, filename: &str) -> String {
if self.use_headers {
if let Some(ref header) = self.current_header {
format!("{} - {} (line {})", filename, header, line)
} else {
format!("{} - (line {})", filename, line)
}
} else {
format!("{} - {} (line {})", filename, self.names.join("::"), line)
}
format!("{} - {} (line {})", filename, self.names.join("::"), line)
}

// to be removed once hoedown is gone
fn generate_name_beginning(&self, filename: &str) -> String {
if self.use_headers {
if let Some(ref header) = self.current_header {
format!("{} - {} (line", filename, header)
} else {
format!("{} - (line", filename)
}
} else {
format!("{} - {} (line", filename, self.names.join("::"))
}
format!("{} - {} (line", filename, self.names.join("::"))
}

pub fn add_old_test(&mut self, test: String, filename: String) {
Expand Down Expand Up @@ -580,7 +582,7 @@ impl Collector {
}

pub fn register_header(&mut self, name: &str, level: u32) {
if self.use_headers && level == 1 {
if self.use_headers {
// we use these headings as test names, so it's good if
// they're valid identifiers.
let name = name.chars().enumerate().map(|(i, c)| {
Expand All @@ -592,9 +594,28 @@ impl Collector {
}
}).collect::<String>();

// new header => reset count.
self.cnt = 0;
self.current_header = Some(name);
// Here we try to efficiently assemble the header titles into the
// test name in the form of `h1::h2::h3::h4::h5::h6`.
//
// Suppose originally `self.names` contains `[h1, h2, h3]`...
let level = level as usize;
if level <= self.names.len() {
// ... Consider `level == 2`. All headers in the lower levels
// are irrelevant in this new level. So we should reset
// `self.names` to contain headers until <h2>, and replace that
// slot with the new name: `[h1, name]`.
self.names.truncate(level);
self.names[level - 1] = name;
} else {
// ... On the other hand, consider `level == 5`. This means we
// need to extend `self.names` to contain five headers. We fill
// in the missing level (<h4>) with `_`. Thus `self.names` will
// become `[h1, h2, h3, "_", name]`.
if level - 1 > self.names.len() {
self.names.resize(level - 1, "_".to_owned());
}
self.names.push(name);
}
}
}
}
Expand Down Expand Up @@ -625,7 +646,6 @@ impl<'a, 'hir> HirCollector<'a, 'hir> {
attrs.collapse_doc_comments();
attrs.unindent_doc_comments();
if let Some(doc) = attrs.doc_value() {
self.collector.cnt = 0;
if self.collector.render_type == RenderType::Pulldown {
markdown::old_find_testable_code(doc, self.collector,
attrs.span.unwrap_or(DUMMY_SP));
Expand Down