diff --git a/library/test/src/types.rs b/library/test/src/types.rs index c3be3466cb9..802cab989c6 100644 --- a/library/test/src/types.rs +++ b/library/test/src/types.rs @@ -250,3 +250,37 @@ pub struct TestDescAndFn { pub desc: TestDesc, pub testfn: TestFn, } + +impl TestDescAndFn { + pub const fn new_doctest( + test_name: &'static str, + ignore: bool, + source_file: &'static str, + start_line: usize, + no_run: bool, + should_panic: bool, + testfn: TestFn, + ) -> Self { + Self { + desc: TestDesc { + name: StaticTestName(test_name), + ignore, + ignore_message: None, + source_file, + start_line, + start_col: 0, + end_line: 0, + end_col: 0, + compile_fail: false, + no_run, + should_panic: if should_panic { + options::ShouldPanic::Yes + } else { + options::ShouldPanic::No + }, + test_type: TestType::DocTest, + }, + testfn, + } + } +} diff --git a/src/librustdoc/doctest/runner.rs b/src/librustdoc/doctest/runner.rs index 72ebe33bd0b..55673fd562e 100644 --- a/src/librustdoc/doctest/runner.rs +++ b/src/librustdoc/doctest/runner.rs @@ -235,36 +235,18 @@ fn main() {returns_result} {{ output, " #[rustc_test_marker = {test_name:?}] -pub const TEST: test::TestDescAndFn = test::TestDescAndFn {{ - desc: test::TestDesc {{ - name: test::StaticTestName({test_name:?}), - ignore: {ignore}, - ignore_message: None, - source_file: {file:?}, - start_line: {line}, - start_col: 0, - end_line: 0, - end_col: 0, - compile_fail: false, - no_run: {no_run}, - should_panic: test::ShouldPanic::{should_panic}, - test_type: test::TestType::DocTest, - }}, - testfn: test::StaticTestFn( - #[coverage(off)] - || {{{runner}}}, - ) -}}; +pub const TEST: test::TestDescAndFn = test::TestDescAndFn::new_doctest( +{test_name:?}, {ignore}, {file:?}, {line}, {no_run}, {should_panic}, +test::StaticTestFn( + #[coverage(off)] + || {{{runner}}}, +)); }}", test_name = scraped_test.name, file = scraped_test.path(), line = scraped_test.line, no_run = scraped_test.langstr.no_run, - should_panic = if !scraped_test.langstr.no_run && scraped_test.langstr.should_panic { - "Yes" - } else { - "No" - }, + should_panic = !scraped_test.langstr.no_run && scraped_test.langstr.should_panic, // Setting `no_run` to `true` in `TestDesc` still makes the test run, so we simply // don't give it the function to run. runner = if not_running {