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,7 +36,16 @@ 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, global_crate_attrs, .. } =
4050 scraped_test;
4151
@@ -45,7 +55,7 @@ impl ExtractedDocTests {
4555 . edition ( edition)
4656 . lang_str ( & langstr)
4757 . build ( None ) ;
48- let ( wrapper , _size) = doctest. generate_unique_doctest (
58+ let ( wrapped , _size) = doctest. generate_unique_doctest (
4959 & text,
5060 langstr. test_harness ,
5161 opts,
@@ -55,7 +65,7 @@ impl ExtractedDocTests {
5565 file : filename. prefer_remapped_unconditionaly ( ) . to_string ( ) ,
5666 line,
5767 doctest_attributes : langstr. into ( ) ,
58- doctest_code : match wrapper {
68+ doctest_code : match wrapped {
5969 DocTestWrapResult :: Valid { crate_level_code, wrapper, code } => Some ( DocTest {
6070 crate_level : crate_level_code,
6171 code,
@@ -71,6 +81,11 @@ impl ExtractedDocTests {
7181 name,
7282 } ) ;
7383 }
84+
85+ #[ cfg( test) ]
86+ pub ( crate ) fn doctests ( & self ) -> & [ ExtractedDocTest ] {
87+ & self . doctests
88+ }
7489}
7590
7691#[ derive( Serialize ) ]
@@ -84,7 +99,12 @@ pub(crate) struct WrapperInfo {
8499pub ( crate ) struct DocTest {
85100 crate_level : String ,
86101 code : String ,
87- wrapper : Option < WrapperInfo > ,
102+ /// This field can be `None` if one of the following conditions is true:
103+ ///
104+ /// * The doctest's codeblock has the `test_harness` attribute.
105+ /// * The doctest has a `main` function.
106+ /// * The doctest has the `![no_std]` attribute.
107+ pub ( crate ) wrapper : Option < WrapperInfo > ,
88108}
89109
90110#[ derive( Serialize ) ]
@@ -94,7 +114,7 @@ pub(crate) struct ExtractedDocTest {
94114 doctest_attributes : LangString ,
95115 original_code : String ,
96116 /// `None` if the code syntax is invalid.
97- doctest_code : Option < DocTest > ,
117+ pub ( crate ) doctest_code : Option < DocTest > ,
98118 name : String ,
99119}
100120
0 commit comments