Skip to content

Commit 01b3f38

Browse files
committed
Fix broken rustdoc unit tests
1 parent dea33f4 commit 01b3f38

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

src/librustdoc/doctest/tests.rs

+37-22
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
use std::path::PathBuf;
2+
13
use super::{make_test, GlobalTestOptions};
24
use rustc_span::edition::DEFAULT_EDITION;
35

6+
/// Default [`GlobalTestOptions`] for these unit tests.
7+
fn default_global_opts(crate_name: impl Into<String>) -> GlobalTestOptions {
8+
GlobalTestOptions {
9+
crate_name: crate_name.into(),
10+
no_crate_inject: false,
11+
insert_indent_space: false,
12+
attrs: vec![],
13+
args_file: PathBuf::new(),
14+
}
15+
}
16+
417
#[test]
518
fn make_test_basic() {
619
//basic use: wraps with `fn main`, adds `#![allow(unused)]`
7-
let opts = GlobalTestOptions::default();
20+
let opts = default_global_opts("");
821
let input = "assert_eq!(2+2, 4);";
922
let expected = "#![allow(unused)]
1023
fn main() {
@@ -19,7 +32,7 @@ assert_eq!(2+2, 4);
1932
fn make_test_crate_name_no_use() {
2033
// If you give a crate name but *don't* use it within the test, it won't bother inserting
2134
// the `extern crate` statement.
22-
let opts = GlobalTestOptions::default();
35+
let opts = default_global_opts("asdf");
2336
let input = "assert_eq!(2+2, 4);";
2437
let expected = "#![allow(unused)]
2538
fn main() {
@@ -34,7 +47,7 @@ assert_eq!(2+2, 4);
3447
fn make_test_crate_name() {
3548
// If you give a crate name and use it within the test, it will insert an `extern crate`
3649
// statement before `fn main`.
37-
let opts = GlobalTestOptions::default();
50+
let opts = default_global_opts("asdf");
3851
let input = "use asdf::qwop;
3952
assert_eq!(2+2, 4);";
4053
let expected = "#![allow(unused)]
@@ -53,8 +66,7 @@ assert_eq!(2+2, 4);
5366
fn make_test_no_crate_inject() {
5467
// Even if you do use the crate within the test, setting `opts.no_crate_inject` will skip
5568
// adding it anyway.
56-
let opts =
57-
GlobalTestOptions { no_crate_inject: true, attrs: vec![], insert_indent_space: false };
69+
let opts = GlobalTestOptions { no_crate_inject: true, ..default_global_opts("asdf") };
5870
let input = "use asdf::qwop;
5971
assert_eq!(2+2, 4);";
6072
let expected = "#![allow(unused)]
@@ -72,7 +84,7 @@ fn make_test_ignore_std() {
7284
// Even if you include a crate name, and use it in the doctest, we still won't include an
7385
// `extern crate` statement if the crate is "std" -- that's included already by the
7486
// compiler!
75-
let opts = GlobalTestOptions::default();
87+
let opts = default_global_opts("std");
7688
let input = "use std::*;
7789
assert_eq!(2+2, 4);";
7890
let expected = "#![allow(unused)]
@@ -89,7 +101,7 @@ assert_eq!(2+2, 4);
89101
fn make_test_manual_extern_crate() {
90102
// When you manually include an `extern crate` statement in your doctest, `make_test`
91103
// assumes you've included one for your own crate too.
92-
let opts = GlobalTestOptions::default();
104+
let opts = default_global_opts("asdf");
93105
let input = "extern crate asdf;
94106
use asdf::qwop;
95107
assert_eq!(2+2, 4);";
@@ -106,7 +118,7 @@ assert_eq!(2+2, 4);
106118

107119
#[test]
108120
fn make_test_manual_extern_crate_with_macro_use() {
109-
let opts = GlobalTestOptions::default();
121+
let opts = default_global_opts("asdf");
110122
let input = "#[macro_use] extern crate asdf;
111123
use asdf::qwop;
112124
assert_eq!(2+2, 4);";
@@ -125,7 +137,7 @@ assert_eq!(2+2, 4);
125137
fn make_test_opts_attrs() {
126138
// If you supplied some doctest attributes with `#![doc(test(attr(...)))]`, it will use
127139
// those instead of the stock `#![allow(unused)]`.
128-
let mut opts = GlobalTestOptions::default();
140+
let mut opts = default_global_opts("asdf");
129141
opts.attrs.push("feature(sick_rad)".to_string());
130142
let input = "use asdf::qwop;
131143
assert_eq!(2+2, 4);";
@@ -159,7 +171,7 @@ assert_eq!(2+2, 4);
159171
fn make_test_crate_attrs() {
160172
// Including inner attributes in your doctest will apply them to the whole "crate", pasting
161173
// them outside the generated main function.
162-
let opts = GlobalTestOptions::default();
174+
let opts = default_global_opts("");
163175
let input = "#![feature(sick_rad)]
164176
assert_eq!(2+2, 4);";
165177
let expected = "#![allow(unused)]
@@ -175,7 +187,7 @@ assert_eq!(2+2, 4);
175187
#[test]
176188
fn make_test_with_main() {
177189
// Including your own `fn main` wrapper lets the test use it verbatim.
178-
let opts = GlobalTestOptions::default();
190+
let opts = default_global_opts("");
179191
let input = "fn main() {
180192
assert_eq!(2+2, 4);
181193
}";
@@ -191,7 +203,7 @@ fn main() {
191203
#[test]
192204
fn make_test_fake_main() {
193205
// ... but putting it in a comment will still provide a wrapper.
194-
let opts = GlobalTestOptions::default();
206+
let opts = default_global_opts("");
195207
let input = "//Ceci n'est pas une `fn main`
196208
assert_eq!(2+2, 4);";
197209
let expected = "#![allow(unused)]
@@ -207,7 +219,7 @@ assert_eq!(2+2, 4);
207219
#[test]
208220
fn make_test_dont_insert_main() {
209221
// Even with that, if you set `dont_insert_main`, it won't create the `fn main` wrapper.
210-
let opts = GlobalTestOptions::default();
222+
let opts = default_global_opts("");
211223
let input = "//Ceci n'est pas une `fn main`
212224
assert_eq!(2+2, 4);";
213225
let expected = "#![allow(unused)]
@@ -219,8 +231,8 @@ assert_eq!(2+2, 4);"
219231
}
220232

221233
#[test]
222-
fn make_test_issues_21299_33731() {
223-
let opts = GlobalTestOptions::default();
234+
fn make_test_issues_21299() {
235+
let opts = default_global_opts("");
224236

225237
let input = "// fn main
226238
assert_eq!(2+2, 4);";
@@ -234,6 +246,11 @@ assert_eq!(2+2, 4);
234246

235247
let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
236248
assert_eq!((output, len), (expected, 2));
249+
}
250+
251+
#[test]
252+
fn make_test_issues_33731() {
253+
let opts = default_global_opts("asdf");
237254

238255
let input = "extern crate hella_qwop;
239256
assert_eq!(asdf::foo, 4);";
@@ -253,7 +270,7 @@ assert_eq!(asdf::foo, 4);
253270

254271
#[test]
255272
fn make_test_main_in_macro() {
256-
let opts = GlobalTestOptions::default();
273+
let opts = default_global_opts("my_crate");
257274
let input = "#[macro_use] extern crate my_crate;
258275
test_wrapper! {
259276
fn main() {}
@@ -272,7 +289,7 @@ test_wrapper! {
272289
#[test]
273290
fn make_test_returns_result() {
274291
// creates an inner function and unwraps it
275-
let opts = GlobalTestOptions::default();
292+
let opts = default_global_opts("");
276293
let input = "use std::io;
277294
let mut input = String::new();
278295
io::stdin().read_line(&mut input)?;
@@ -292,7 +309,7 @@ Ok::<(), io:Error>(())
292309
#[test]
293310
fn make_test_named_wrapper() {
294311
// creates an inner function with a specific name
295-
let opts = GlobalTestOptions::default();
312+
let opts = default_global_opts("");
296313
let input = "assert_eq!(2+2, 4);";
297314
let expected = "#![allow(unused)]
298315
fn main() { #[allow(non_snake_case)] fn _doctest_main__some_unique_name() {
@@ -307,8 +324,7 @@ assert_eq!(2+2, 4);
307324
#[test]
308325
fn make_test_insert_extra_space() {
309326
// will insert indent spaces in the code block if `insert_indent_space` is true
310-
let opts =
311-
GlobalTestOptions { no_crate_inject: false, attrs: vec![], insert_indent_space: true };
327+
let opts = GlobalTestOptions { insert_indent_space: true, ..default_global_opts("") };
312328
let input = "use std::*;
313329
assert_eq!(2+2, 4);
314330
eprintln!(\"hello anan\");
@@ -327,8 +343,7 @@ fn main() {
327343
#[test]
328344
fn make_test_insert_extra_space_fn_main() {
329345
// if input already has a fn main, it should insert a space before it
330-
let opts =
331-
GlobalTestOptions { no_crate_inject: false, attrs: vec![], insert_indent_space: true };
346+
let opts = GlobalTestOptions { insert_indent_space: true, ..default_global_opts("") };
332347
let input = "use std::*;
333348
fn main() {
334349
assert_eq!(2+2, 4);

0 commit comments

Comments
 (0)