Update rustdoc tests

This commit is contained in:
Guillaume Gomez 2025-02-07 17:50:14 +01:00
parent 0395fc2242
commit 673fd23dff
10 changed files with 72 additions and 156 deletions

View file

@ -23,7 +23,7 @@ fn test_html_highlighting() {
let src = include_str!("fixtures/sample.rs");
let html = {
let mut out = Buffer::new();
write_code(&mut out, src, None, None);
write_code(&mut out, src, None, None, None);
format!("{STYLE}<pre><code>{}</code></pre>\n", out.into_inner())
};
expect_file!["fixtures/sample.html"].assert_eq(&html);
@ -37,7 +37,7 @@ fn test_dos_backline() {
println!(\"foo\");\r\n\
}\r\n";
let mut html = Buffer::new();
write_code(&mut html, src, None, None);
write_code(&mut html, src, None, None, None);
expect_file!["fixtures/dos_line.html"].assert_eq(&html.into_inner());
});
}
@ -51,7 +51,7 @@ let x = super::b::foo;
let y = Self::whatever;";
let mut html = Buffer::new();
write_code(&mut html, src, None, None);
write_code(&mut html, src, None, None, None);
expect_file!["fixtures/highlight.html"].assert_eq(&html.into_inner());
});
}
@ -61,7 +61,7 @@ fn test_union_highlighting() {
create_default_session_globals_then(|| {
let src = include_str!("fixtures/union.rs");
let mut html = Buffer::new();
write_code(&mut html, src, None, None);
write_code(&mut html, src, None, None, None);
expect_file!["fixtures/union.html"].assert_eq(&html.into_inner());
});
}
@ -78,7 +78,7 @@ let a = 4;";
decorations.insert("example2", vec![(22, 32)]);
let mut html = Buffer::new();
write_code(&mut html, src, None, Some(&DecorationInfo(decorations)));
write_code(&mut html, src, None, Some(&DecorationInfo(decorations)), None);
expect_file!["fixtures/decorations.html"].assert_eq(&html.into_inner());
});
}

View file

@ -1,6 +0,0 @@
// Small test to ensure the "src-line-numbers" element is only present once on
// the page.
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
click: "a.src"
wait-for: ".src-line-numbers"
assert-count: (".src-line-numbers", 1)

View file

@ -111,28 +111,6 @@ wait-for: "pre.example-line-numbers"
// Same check with scraped examples line numbers.
go-to: "file://" + |DOC_PATH| + "/scrape_examples/fn.test_many.html"
assert-css: (
".scraped-example .src-line-numbers > pre",
{
// There should not be a radius on the right of the line numbers.
"border-top-left-radius": "6px",
"border-bottom-left-radius": "6px",
"border-top-right-radius": "0px",
"border-bottom-right-radius": "0px",
},
ALL,
)
assert-css: (
".scraped-example .src-line-numbers",
{
// There should not be a radius on the right of the line numbers.
"border-top-left-radius": "6px",
"border-bottom-left-radius": "6px",
"border-top-right-radius": "0px",
"border-bottom-right-radius": "0px",
},
ALL,
)
assert-css: (
".scraped-example .rust",
{
@ -149,23 +127,15 @@ define-function: (
"check-padding",
[path, padding_bottom],
block {
assert-css: (|path| + " .src-line-numbers", {
assert-css: (|path| + " span[data-nosnippet]", {
"padding-top": "0px",
"padding-bottom": "0px",
"padding-left": "0px",
"padding-right": "0px",
}, ALL)
assert-css: (|path| + " .src-line-numbers > pre", {
"padding-top": "14px",
"padding-bottom": |padding_bottom|,
"padding-left": "0px",
"padding-right": "0px",
}, ALL)
assert-css: (|path| + " .src-line-numbers > pre > span", {
"padding-top": "0px",
"padding-bottom": "0px",
"padding-left": "8px",
"padding-right": "8px",
"padding-left": "4px",
"padding-right": "4px",
"margin-right": "20px",
"margin-left": "0px",
"margin-top": "0px",
"margin-bottom": "0px",
}, ALL)
},
)
@ -196,13 +166,13 @@ define-function: ("check-line-numbers-existence", [], block {
wait-for-local-storage-false: {"rustdoc-line-numbers": "true" }
assert-false: ".example-line-numbers"
// Line numbers should still be there.
assert: ".src-line-numbers"
assert-css: ("[data-nosnippet]", { "display": "inline-block"})
// Now disabling the setting.
click: "input#line-numbers"
wait-for-local-storage: {"rustdoc-line-numbers": "true" }
assert-false: ".example-line-numbers"
// Line numbers should still be there.
assert: ".src-line-numbers"
assert-css: ("[data-nosnippet]", { "display": "inline-block"})
// Closing settings menu.
click: "#settings-menu"
wait-for-css: ("#settings", {"display": "none"})
@ -214,18 +184,16 @@ call-function: ("check-line-numbers-existence", {})
// Now checking the line numbers in the source code page.
click: ".src"
assert-css: (".src-line-numbers", {
"padding-top": "20px",
"padding-bottom": "20px",
"padding-left": "4px",
"padding-right": "0px",
})
assert-css: (".src-line-numbers > a", {
assert-css: ("a[data-nosnippet]", {
"padding-top": "0px",
"padding-bottom": "0px",
"padding-left": "8px",
"padding-right": "8px",
})
"padding-left": "4px",
"padding-right": "4px",
"margin-top": "0px",
"margin-bottom": "0px",
"margin-left": "0px",
"margin-right": "20px",
}, ALL)
// Checking that turning off the line numbers setting won't remove line numbers.
call-function: ("check-line-numbers-existence", {})

View file

@ -8,7 +8,7 @@ define-function: (
block {
call-function: ("switch-theme", {"theme": |theme|})
assert-css: (
"body.src .example-wrap pre.rust a",
"body.src .example-wrap pre.rust a:not([data-nosnippet])",
{"background-color": |background_color|},
ALL,
)

View file

@ -4,52 +4,18 @@ go-to: "file://" + |DOC_PATH| + "/scrape_examples/fn.test.html"
// The next/prev buttons vertically scroll the code viewport between examples
move-cursor-to: ".scraped-example-list > .scraped-example"
store-property: (".scraped-example-list > .scraped-example .src-line-numbers", {
"scrollTop": initialScrollTop,
})
wait-for: ".scraped-example-list > .scraped-example .next"
store-value: (initialScrollTop, 250)
assert-property: (".scraped-example-list > .scraped-example .rust", {
"scrollTop": |initialScrollTop|,
})
}, NEAR)
focus: ".scraped-example-list > .scraped-example .next"
press-key: "Enter"
assert-property-false: (".scraped-example-list > .scraped-example .src-line-numbers", {
"scrollTop": |initialScrollTop|
}, NEAR)
assert-property-false: (".scraped-example-list > .scraped-example .rust", {
"scrollTop": |initialScrollTop|
}, NEAR)
focus: ".scraped-example-list > .scraped-example .prev"
press-key: "Enter"
assert-property: (".scraped-example-list > .scraped-example .src-line-numbers", {
"scrollTop": |initialScrollTop|
}, NEAR)
assert-property: (".scraped-example-list > .scraped-example .rust", {
"scrollTop": |initialScrollTop|
}, NEAR)
// The expand button increases the scrollHeight of the minimized code viewport
store-property: (".scraped-example-list > .scraped-example pre", {"offsetHeight": smallOffsetHeight})
assert-property: (".scraped-example-list > .scraped-example .src-line-numbers", {
"scrollHeight": |smallOffsetHeight|
}, NEAR)
assert-property: (".scraped-example-list > .scraped-example .rust", {
"scrollHeight": |smallOffsetHeight|
}, NEAR)
focus: ".scraped-example-list > .scraped-example .expand"
press-key: "Enter"
assert-property-false: (".scraped-example-list > .scraped-example .src-line-numbers", {
"offsetHeight": |smallOffsetHeight|
}, NEAR)
assert-property-false: (".scraped-example-list > .scraped-example .rust", {
"offsetHeight": |smallOffsetHeight|
}, NEAR)
store-property: (".scraped-example-list > .scraped-example .src-line-numbers", {
"offsetHeight": fullOffsetHeight,
})
assert-property: (".scraped-example-list > .scraped-example .rust", {
"offsetHeight": |fullOffsetHeight|,
"scrollHeight": |fullOffsetHeight|,
})
assert-property: (".scraped-example-list > .scraped-example .src-line-numbers", {
"scrollHeight": |fullOffsetHeight|
}, NEAR)

View file

@ -3,35 +3,38 @@ go-to: "file://" + |DOC_PATH| + "/scrape_examples/fn.test_many.html"
set-window-size: (1000, 1000)
// We move the mouse over the scraped example for the prev button to be generated.
move-cursor-to: ".scraped-example"
// Check that it's not zero.
assert-property-false: (
".more-scraped-examples .scraped-example .src-line-numbers",
".more-scraped-examples .scraped-example span[data-nosnippet]",
{"clientWidth": "0"}
)
// Check that examples with very long lines have the same width as ones that don't.
store-property: (
".more-scraped-examples .scraped-example:nth-child(2) .src-line-numbers",
".more-scraped-examples .scraped-example:nth-child(2) span[data-nosnippet]",
{"clientWidth": clientWidth},
)
assert-property: (
".more-scraped-examples .scraped-example:nth-child(3) .src-line-numbers",
".more-scraped-examples .scraped-example:nth-child(3) span[data-nosnippet]",
{"clientWidth": |clientWidth|}
)
assert-property: (
".more-scraped-examples .scraped-example:nth-child(4) .src-line-numbers",
".more-scraped-examples .scraped-example:nth-child(4) span[data-nosnippet]",
{"clientWidth": |clientWidth|}
)
assert-property: (
".more-scraped-examples .scraped-example:nth-child(5) .src-line-numbers",
".more-scraped-examples .scraped-example:nth-child(5) span[data-nosnippet]",
{"clientWidth": |clientWidth|}
)
assert-property: (
".more-scraped-examples .scraped-example:nth-child(6) .src-line-numbers",
".more-scraped-examples .scraped-example:nth-child(6) span[data-nosnippet]",
{"clientWidth": |clientWidth|}
)
@ -55,25 +58,6 @@ assert-size: (".more-scraped-examples .scraped-example .example-wrap", {
"width": |width|,
})
// Check that the expand button works and also that line number aligns with code.
move-cursor-to: ".scraped-example .rust"
click: ".scraped-example .button-holder .expand"
wait-for: ".scraped-example.expanded"
// They should have the same y position.
compare-elements-position: (
".scraped-example.expanded .src-line-numbers pre span",
".scraped-example.expanded .rust code",
["y"],
)
// And they should have the same height.
compare-elements-size: (
".scraped-example.expanded .src-line-numbers",
".scraped-example.expanded .rust",
["height"],
)
// Collapse code again.
click: ".scraped-example .button-holder .expand"
// Check that for both mobile and desktop sizes, the buttons in scraped examples are displayed
// correctly.
@ -98,7 +82,7 @@ define-function: (
[],
block {
// Title should be above the code.
store-position: (".scraped-example .example-wrap .src-line-numbers", {"x": x, "y": y})
store-position: (".scraped-example .example-wrap", {"x": x, "y": y})
store-size: (".scraped-example .scraped-example-title", { "height": title_height })
assert-position: (".scraped-example .scraped-example-title", {
@ -107,10 +91,13 @@ define-function: (
})
// Line numbers should be right beside the code.
compare-elements-position: (
".scraped-example .example-wrap .src-line-numbers",
".scraped-example .example-wrap .rust",
["y"],
compare-elements-position-near: (
".scraped-example .example-wrap span[data-nosnippet]",
// On the first line, the code starts with `fn main` so we have a keyword.
".scraped-example .example-wrap .rust span.kw",
// They're not exactly the same size but since they're on the same line,
// it's kinda the same.
{"y": 2},
)
}
)

View file

@ -8,13 +8,13 @@ set-window-size: (600, 800)
assert-property: ("html", {"scrollTop": "0"})
click: '//a[text() = "barbar" and @href="#5-7"]'
assert-property: ("html", {"scrollTop": "208"})
assert-property: ("html", {"scrollTop": "206"})
click: '//a[text() = "bar" and @href="#28-36"]'
assert-property: ("html", {"scrollTop": "239"})
click: '//a[normalize-space() = "sub_fn" and @href="#2-4"]'
assert-property: ("html", {"scrollTop": "136"})
assert-property: ("html", {"scrollTop": "134"})
// We now check that clicking on lines doesn't change the scroll
// Extra information: the "sub_fn" function header is on line 1.
click: '//*[@id="6"]'
assert-property: ("html", {"scrollTop": "136"})
assert-property: ("html", {"scrollTop": "134"})

View file

@ -2,7 +2,7 @@
go-to: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
set-window-size: (800, 1000)
// "scrollWidth" should be superior than "clientWidth".
assert-property: ("body", {"scrollWidth": 1776, "clientWidth": 800})
assert-property: ("body", {"scrollWidth": 1780, "clientWidth": 800})
// Both properties should be equal (ie, no scroll on the code block).
assert-property: (".example-wrap .rust", {"scrollWidth": 1662, "clientWidth": 1662})
assert-property: (".example-wrap .rust", {"scrollWidth": 1715, "clientWidth": 1715})

View file

@ -3,7 +3,7 @@ include: "utils.goml"
go-to: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
show-text: true
// Check that we can click on the line number.
click: ".src-line-numbers > a:nth-child(4)" // This is the anchor for line 4.
click: "//a[@data-nosnippet and text()='4']" // This is the anchor for line 4.
// Ensure that the page URL was updated.
assert-document-property: ({"URL": "lib.rs.html#4"}, ENDS_WITH)
assert-attribute: ("//*[@id='4']", {"class": "line-highlighted"})
@ -14,11 +14,11 @@ assert-attribute: ("//*[@id='4']", {"class": "line-highlighted"})
assert-css: ("//*[@id='4']", {"border-right-width": "0px"})
// We now check that the good anchors are highlighted
go-to: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#4-6"
assert-attribute-false: (".src-line-numbers > a:nth-child(3)", {"class": "line-highlighted"})
assert-attribute: (".src-line-numbers > a:nth-child(4)", {"class": "line-highlighted"})
assert-attribute: (".src-line-numbers > a:nth-child(5)", {"class": "line-highlighted"})
assert-attribute: (".src-line-numbers > a:nth-child(6)", {"class": "line-highlighted"})
assert-attribute-false: (".src-line-numbers > a:nth-child(7)", {"class": "line-highlighted"})
assert-attribute-false: ("//a[@data-nosnippet and text()='3']", {"class": "line-highlighted"})
assert-attribute: ("//a[@data-nosnippet and text()='4']", {"class": "line-highlighted"})
assert-attribute: ("//a[@data-nosnippet and text()='5']", {"class": "line-highlighted"})
assert-attribute: ("//a[@data-nosnippet and text()='6']", {"class": "line-highlighted"})
assert-attribute-false: ("//a[@data-nosnippet and text()='7']", {"class": "line-highlighted"})
define-function: (
"check-colors",
@ -26,12 +26,12 @@ define-function: (
block {
call-function: ("switch-theme", {"theme": |theme|})
assert-css: (
".src-line-numbers > a:not(.line-highlighted)",
"a[data-nosnippet]:not(.line-highlighted)",
{"color": |color|, "background-color": |background_color|},
ALL,
)
assert-css: (
".src-line-numbers > a.line-highlighted",
"a[data-nosnippet].line-highlighted",
{"color": |highlight_color|, "background-color": |highlight_background_color|},
ALL,
)
@ -61,37 +61,37 @@ call-function: ("check-colors", {
})
// This is to ensure that the content is correctly align with the line numbers.
compare-elements-position: ("//*[@id='1']", ".rust > code > span", ["y"])
compare-elements-position-near: ("//*[@id='1']", ".rust > code > span", {"y": 2})
// Check the `href` property so that users can treat anchors as links.
assert-property: (".src-line-numbers > a:nth-child(1)", {
assert-property: ("//a[@data-nosnippet and text()='1']", {
"href": |DOC_PATH| + "/src/test_docs/lib.rs.html#1"
}, ENDS_WITH)
assert-property: (".src-line-numbers > a:nth-child(2)", {
assert-property: ("//a[@data-nosnippet and text()='2']", {
"href": |DOC_PATH| + "/src/test_docs/lib.rs.html#2"
}, ENDS_WITH)
assert-property: (".src-line-numbers > a:nth-child(3)", {
assert-property: ("//a[@data-nosnippet and text()='3']", {
"href": |DOC_PATH| + "/src/test_docs/lib.rs.html#3"
}, ENDS_WITH)
assert-property: (".src-line-numbers > a:nth-child(4)", {
assert-property: ("//a[@data-nosnippet and text()='4']", {
"href": |DOC_PATH| + "/src/test_docs/lib.rs.html#4"
}, ENDS_WITH)
assert-property: (".src-line-numbers > a:nth-child(5)", {
assert-property: ("//a[@data-nosnippet and text()='5']", {
"href": |DOC_PATH| + "/src/test_docs/lib.rs.html#5"
}, ENDS_WITH)
assert-property: (".src-line-numbers > a:nth-child(6)", {
assert-property: ("//a[@data-nosnippet and text()='6']", {
"href": |DOC_PATH| + "/src/test_docs/lib.rs.html#6"
}, ENDS_WITH)
// Assert that the line numbers text is aligned to the right.
assert-css: (".src-line-numbers", {"text-align": "right"})
assert-css: ("a[data-nosnippet]", {"text-align": "right"}, ALL)
// Now let's check that clicking on something else than the line number doesn't
// do anything (and certainly not add a `#NaN` to the URL!).
go-to: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
// We use this assert-position to know where we will click.
assert-position: ("//*[@id='1']", {"x": 88, "y": 171})
// We click on the left of the "1" anchor but still in the "src-line-number" `<pre>`.
click: (163, 77)
assert-position: ("//*[@id='1']", {"x": 81, "y": 169})
// We click on the left of the "1" anchor but still in the `a[data-nosnippet]`.
click: (77, 163)
assert-document-property: ({"URL": "/lib.rs.html"}, ENDS_WITH)
// Checking the source code sidebar.

View file

@ -31,7 +31,8 @@ fn babar() {}
//@ has - '//pre[@class="rust"]//a/@href' '/struct.String.html'
//@ has - '//pre[@class="rust"]//a/@href' '/primitive.u32.html'
//@ has - '//pre[@class="rust"]//a/@href' '/primitive.str.html'
//@ count - '//pre[@class="rust"]//a[@href="#23"]' 5
// The 5 links to line 23 and the line 23 itself.
//@ count - '//pre[@class="rust"]//a[@href="#23"]' 6
//@ has - '//pre[@class="rust"]//a[@href="../../source_code/struct.SourceCode.html"]' \
// 'source_code::SourceCode'
pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar, f: source_code::SourceCode) {
@ -50,8 +51,8 @@ pub fn foo2<T: bar::sub::Trait, V: Trait>(t: &T, v: &V, b: bool) {}
pub trait AnotherTrait {}
pub trait WhyNot {}
//@ has - '//pre[@class="rust"]//a[@href="#50"]' 'AnotherTrait'
//@ has - '//pre[@class="rust"]//a[@href="#51"]' 'WhyNot'
//@ has - '//pre[@class="rust"]//a[@href="#51"]' 'AnotherTrait'
//@ has - '//pre[@class="rust"]//a[@href="#52"]' 'WhyNot'
pub fn foo3<T, V>(t: &T, v: &V)
where
T: AnotherTrait,
@ -60,7 +61,7 @@ where
pub trait AnotherTrait2 {}
//@ has - '//pre[@class="rust"]//a[@href="#61"]' 'AnotherTrait2'
//@ has - '//pre[@class="rust"]//a[@href="#62"]' 'AnotherTrait2'
pub fn foo4() {
let x: Vec<&dyn AnotherTrait2> = Vec::new();
}