3
3
//! This module contains the logic to extract doctests and output a JSON containing this
4
4
//! information.
5
5
6
+ use rustc_span:: edition:: Edition ;
6
7
use serde:: Serialize ;
7
8
8
9
use super :: make:: DocTestWrapResult ;
@@ -35,15 +36,24 @@ impl ExtractedDocTests {
35
36
options : & RustdocOptions ,
36
37
) {
37
38
let edition = scraped_test. edition ( options) ;
39
+ self . add_test_with_edition ( scraped_test, opts, edition)
40
+ }
38
41
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
+ ) {
39
49
let ScrapedDocTest { filename, line, langstr, text, name, .. } = scraped_test;
40
50
41
51
let doctest = BuildDocTestBuilder :: new ( & text)
42
52
. crate_name ( & opts. crate_name )
43
53
. edition ( edition)
44
54
. lang_str ( & langstr)
45
55
. build ( None ) ;
46
- let ( wrapper , _size) = doctest. generate_unique_doctest (
56
+ let ( wrapped , _size) = doctest. generate_unique_doctest (
47
57
& text,
48
58
langstr. test_harness ,
49
59
opts,
@@ -53,7 +63,7 @@ impl ExtractedDocTests {
53
63
file : filename. prefer_remapped_unconditionaly ( ) . to_string ( ) ,
54
64
line,
55
65
doctest_attributes : langstr. into ( ) ,
56
- doctest_code : match wrapper {
66
+ doctest_code : match wrapped {
57
67
DocTestWrapResult :: Valid { crate_level_code, wrapper, code } => Some ( DocTest {
58
68
crate_level : crate_level_code,
59
69
code,
@@ -69,6 +79,11 @@ impl ExtractedDocTests {
69
79
name,
70
80
} ) ;
71
81
}
82
+
83
+ #[ cfg( test) ]
84
+ pub ( crate ) fn doctests ( & self ) -> & [ ExtractedDocTest ] {
85
+ & self . doctests
86
+ }
72
87
}
73
88
74
89
#[ derive( Serialize ) ]
@@ -82,7 +97,12 @@ pub(crate) struct WrapperInfo {
82
97
pub ( crate ) struct DocTest {
83
98
crate_level : String ,
84
99
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 > ,
86
106
}
87
107
88
108
#[ derive( Serialize ) ]
@@ -92,7 +112,7 @@ pub(crate) struct ExtractedDocTest {
92
112
doctest_attributes : LangString ,
93
113
original_code : String ,
94
114
/// `None` if the code syntax is invalid.
95
- doctest_code : Option < DocTest > ,
115
+ pub ( crate ) doctest_code : Option < DocTest > ,
96
116
name : String ,
97
117
}
98
118
0 commit comments