Skip to content

Commit fb7008c

Browse files
committed
Add tests
1 parent 72d5675 commit fb7008c

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

src/librustdoc/html/markdown.rs

+27
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ mod tests {
585585
fn issue_17736() {
586586
let markdown = "# title";
587587
format!("{}", Markdown(markdown));
588+
reset_ids();
588589
}
589590

590591
#[test]
@@ -609,6 +610,32 @@ mod tests {
609610
<em><code>baz</code></em> ❤ #qux</a></h4>");
610611
}
611612

613+
#[test]
614+
fn test_header_ids_multiple_blocks() {
615+
fn t(input: &str, expect: &str) {
616+
let output = format!("{}", Markdown(input));
617+
assert_eq!(output, expect);
618+
}
619+
620+
let test = || {
621+
t("# Example", "\n<h1 id='example' class='section-header'>\
622+
<a href='#example'>Example</a></h1>");
623+
t("# Panics", "\n<h1 id='panics' class='section-header'>\
624+
<a href='#panics'>Panics</a></h1>");
625+
t("# Example", "\n<h1 id='example-1' class='section-header'>\
626+
<a href='#example-1'>Example</a></h1>");
627+
t("# Main", "\n<h1 id='main-1' class='section-header'>\
628+
<a href='#main-1'>Main</a></h1>");
629+
t("# Example", "\n<h1 id='example-2' class='section-header'>\
630+
<a href='#example-2'>Example</a></h1>");
631+
t("# Panics", "\n<h1 id='panics-1' class='section-header'>\
632+
<a href='#panics-1'>Panics</a></h1>");
633+
};
634+
test();
635+
reset_ids();
636+
test();
637+
}
638+
612639
#[test]
613640
fn test_plain_summary_line() {
614641
fn t(input: &str, expect: &str) {

src/librustdoc/html/render.rs

+19
Original file line numberDiff line numberDiff line change
@@ -2708,3 +2708,22 @@ fn get_index_type_name(clean_type: &clean::Type) -> Option<String> {
27082708
pub fn cache() -> Arc<Cache> {
27092709
CACHE_KEY.with(|c| c.borrow().clone())
27102710
}
2711+
2712+
#[cfg(test)]
2713+
#[test]
2714+
fn test_unique_id() {
2715+
let input = ["foo", "examples", "examples", "method.into_iter","examples",
2716+
"method.into_iter", "foo", "main", "search", "methods",
2717+
"examples", "method.into_iter", "assoc_type.Item", "assoc_type.Item"];
2718+
let expected = ["foo", "examples", "examples-1", "method.into_iter", "examples-2",
2719+
"method.into_iter-1", "foo-1", "main-1", "search-1", "methods-1",
2720+
"examples-3", "method.into_iter-2", "assoc_type.Item", "assoc_type.Item-1"];
2721+
2722+
let test = || {
2723+
let actual: Vec<String> = input.iter().map(|s| derive_id(s.to_string())).collect();
2724+
assert_eq!(&actual[..], expected);
2725+
};
2726+
test();
2727+
reset_ids();
2728+
test();
2729+
}

src/test/rustdoc/issue-25001.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// @has issue_25001/struct.Foo.html
12+
pub struct Foo<T>(T);
13+
14+
pub trait Bar {
15+
type Item;
16+
17+
fn quux(self);
18+
}
19+
20+
impl<T> Foo<T> {
21+
// @has - '//*[@id="method.pass"]//code' 'fn pass()'
22+
pub fn pass() {}
23+
}
24+
impl<T> Foo<T> {
25+
// @has - '//*[@id="method.pass-1"]//code' 'fn pass() -> usize'
26+
pub fn pass() -> usize { 42 }
27+
}
28+
impl<T> Foo<T> {
29+
// @has - '//*[@id="method.pass-2"]//code' 'fn pass() -> isize'
30+
pub fn pass() -> isize { 42 }
31+
}
32+
33+
impl<T> Bar for Foo<T> {
34+
// @has - '//*[@id="assoc_type.Item"]//code' 'type Item = T'
35+
type Item=T;
36+
37+
// @has - '//*[@id="method.quux"]//code' 'fn quux(self)'
38+
fn quux(self) {}
39+
}
40+
impl<'a, T> Bar for &'a Foo<T> {
41+
// @has - '//*[@id="assoc_type.Item-1"]//code' "type Item = &'a T"
42+
type Item=&'a T;
43+
44+
// @has - '//*[@id="method.quux-1"]//code' 'fn quux(self)'
45+
fn quux(self) {}
46+
}
47+
impl<'a, T> Bar for &'a mut Foo<T> {
48+
// @has - '//*[@id="assoc_type.Item-2"]//code' "type Item = &'a mut T"
49+
type Item=&'a mut T;
50+
51+
// @has - '//*[@id="method.quux-2"]//code' 'fn quux(self)'
52+
fn quux(self) {}
53+
}

src/test/rustdoc/issue-29449.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// @has issue_29449/struct.Foo.html
12+
pub struct Foo;
13+
14+
impl Foo {
15+
// @has - '//*[@id="examples"]//a' 'Examples'
16+
// @has - '//*[@id="panics"]//a' 'Panics'
17+
/// # Examples
18+
/// # Panics
19+
pub fn bar() {}
20+
21+
// @has - '//*[@id="examples-1"]//a' 'Examples'
22+
/// # Examples
23+
pub fn bar_1() {}
24+
25+
// @has - '//*[@id="examples-2"]//a' 'Examples'
26+
// @has - '//*[@id="panics-1"]//a' 'Panics'
27+
/// # Examples
28+
/// # Panics
29+
pub fn bar_2() {}
30+
}

0 commit comments

Comments
 (0)