33//! This module contains the logic to extract doctests and output a JSON containing this
44//! information.
55
6+ use rustc_span:: edition:: Edition ;
67use serde:: Serialize ;
78
89use super :: make:: DocTestWrapResult ;
@@ -35,15 +36,24 @@ impl ExtractedDocTests {
3536 options : & RustdocOptions ,
3637 ) {
3738 let edition = scraped_test. edition ( options) ;
39+ self . add_test_with_edition ( scraped_test, opts, edition)
40+ }
3841
42+ /// This method is used by unit tests to not have to provide a `RustdocOptions`.
43+ pub ( crate ) fn add_test_with_edition (
44+ & mut self ,
45+ scraped_test : ScrapedDocTest ,
46+ opts : & super :: GlobalTestOptions ,
47+ edition : Edition ,
48+ ) {
3949 let ScrapedDocTest { filename, line, langstr, text, name, .. } = scraped_test;
4050
4151 let doctest = BuildDocTestBuilder :: new ( & text)
4252 . crate_name ( & opts. crate_name )
4353 . edition ( edition)
4454 . lang_str ( & langstr)
4555 . build ( None ) ;
46- let ( wrapper , _size) = doctest. generate_unique_doctest (
56+ let ( wrapped , _size) = doctest. generate_unique_doctest (
4757 & text,
4858 langstr. test_harness ,
4959 opts,
@@ -53,7 +63,7 @@ impl ExtractedDocTests {
5363 file : filename. prefer_remapped_unconditionaly ( ) . to_string ( ) ,
5464 line,
5565 doctest_attributes : langstr. into ( ) ,
56- doctest_code : match wrapper {
66+ doctest_code : match wrapped {
5767 DocTestWrapResult :: Valid { crate_level_code, wrapper, code } => Some ( DocTest {
5868 crate_level : crate_level_code,
5969 code,
@@ -69,6 +79,11 @@ impl ExtractedDocTests {
6979 name,
7080 } ) ;
7181 }
82+
83+ #[ cfg( test) ]
84+ pub ( crate ) fn doctests ( & self ) -> & [ ExtractedDocTest ] {
85+ & self . doctests
86+ }
7287}
7388
7489#[ derive( Serialize ) ]
@@ -82,7 +97,12 @@ pub(crate) struct WrapperInfo {
8297pub ( crate ) struct DocTest {
8398 crate_level : String ,
8499 code : String ,
85- wrapper : Option < WrapperInfo > ,
100+ /// This field can be `None` if one of the following conditions is true:
101+ ///
102+ /// * The doctest's codeblock has the `test_harness` attribute.
103+ /// * The doctest has a `main` function.
104+ /// * The doctest has the `![no_std]` attribute.
105+ pub ( crate ) wrapper : Option < WrapperInfo > ,
86106}
87107
88108#[ derive( Serialize ) ]
@@ -92,7 +112,7 @@ pub(crate) struct ExtractedDocTest {
92112 doctest_attributes : LangString ,
93113 original_code : String ,
94114 /// `None` if the code syntax is invalid.
95- doctest_code : Option < DocTest > ,
115+ pub ( crate ) doctest_code : Option < DocTest > ,
96116 name : String ,
97117}
98118
0 commit comments