1
+ use std:: path:: PathBuf ;
2
+
1
3
use super :: { make_test, GlobalTestOptions } ;
2
4
use rustc_span:: edition:: DEFAULT_EDITION ;
3
5
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
+
4
17
#[ test]
5
18
fn make_test_basic ( ) {
6
19
//basic use: wraps with `fn main`, adds `#![allow(unused)]`
7
- let opts = GlobalTestOptions :: default ( ) ;
20
+ let opts = default_global_opts ( "" ) ;
8
21
let input = "assert_eq!(2+2, 4);" ;
9
22
let expected = "#![allow(unused)]
10
23
fn main() {
@@ -19,7 +32,7 @@ assert_eq!(2+2, 4);
19
32
fn make_test_crate_name_no_use ( ) {
20
33
// If you give a crate name but *don't* use it within the test, it won't bother inserting
21
34
// the `extern crate` statement.
22
- let opts = GlobalTestOptions :: default ( ) ;
35
+ let opts = default_global_opts ( "asdf" ) ;
23
36
let input = "assert_eq!(2+2, 4);" ;
24
37
let expected = "#![allow(unused)]
25
38
fn main() {
@@ -34,7 +47,7 @@ assert_eq!(2+2, 4);
34
47
fn make_test_crate_name ( ) {
35
48
// If you give a crate name and use it within the test, it will insert an `extern crate`
36
49
// statement before `fn main`.
37
- let opts = GlobalTestOptions :: default ( ) ;
50
+ let opts = default_global_opts ( "asdf" ) ;
38
51
let input = "use asdf::qwop;
39
52
assert_eq!(2+2, 4);" ;
40
53
let expected = "#![allow(unused)]
@@ -53,8 +66,7 @@ assert_eq!(2+2, 4);
53
66
fn make_test_no_crate_inject ( ) {
54
67
// Even if you do use the crate within the test, setting `opts.no_crate_inject` will skip
55
68
// 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" ) } ;
58
70
let input = "use asdf::qwop;
59
71
assert_eq!(2+2, 4);" ;
60
72
let expected = "#![allow(unused)]
@@ -72,7 +84,7 @@ fn make_test_ignore_std() {
72
84
// Even if you include a crate name, and use it in the doctest, we still won't include an
73
85
// `extern crate` statement if the crate is "std" -- that's included already by the
74
86
// compiler!
75
- let opts = GlobalTestOptions :: default ( ) ;
87
+ let opts = default_global_opts ( "std" ) ;
76
88
let input = "use std::*;
77
89
assert_eq!(2+2, 4);" ;
78
90
let expected = "#![allow(unused)]
@@ -89,7 +101,7 @@ assert_eq!(2+2, 4);
89
101
fn make_test_manual_extern_crate ( ) {
90
102
// When you manually include an `extern crate` statement in your doctest, `make_test`
91
103
// assumes you've included one for your own crate too.
92
- let opts = GlobalTestOptions :: default ( ) ;
104
+ let opts = default_global_opts ( "asdf" ) ;
93
105
let input = "extern crate asdf;
94
106
use asdf::qwop;
95
107
assert_eq!(2+2, 4);" ;
@@ -106,7 +118,7 @@ assert_eq!(2+2, 4);
106
118
107
119
#[ test]
108
120
fn make_test_manual_extern_crate_with_macro_use ( ) {
109
- let opts = GlobalTestOptions :: default ( ) ;
121
+ let opts = default_global_opts ( "asdf" ) ;
110
122
let input = "#[macro_use] extern crate asdf;
111
123
use asdf::qwop;
112
124
assert_eq!(2+2, 4);" ;
@@ -125,7 +137,7 @@ assert_eq!(2+2, 4);
125
137
fn make_test_opts_attrs ( ) {
126
138
// If you supplied some doctest attributes with `#![doc(test(attr(...)))]`, it will use
127
139
// those instead of the stock `#![allow(unused)]`.
128
- let mut opts = GlobalTestOptions :: default ( ) ;
140
+ let mut opts = default_global_opts ( "asdf" ) ;
129
141
opts. attrs . push ( "feature(sick_rad)" . to_string ( ) ) ;
130
142
let input = "use asdf::qwop;
131
143
assert_eq!(2+2, 4);" ;
@@ -159,7 +171,7 @@ assert_eq!(2+2, 4);
159
171
fn make_test_crate_attrs ( ) {
160
172
// Including inner attributes in your doctest will apply them to the whole "crate", pasting
161
173
// them outside the generated main function.
162
- let opts = GlobalTestOptions :: default ( ) ;
174
+ let opts = default_global_opts ( "" ) ;
163
175
let input = "#![feature(sick_rad)]
164
176
assert_eq!(2+2, 4);" ;
165
177
let expected = "#![allow(unused)]
@@ -175,7 +187,7 @@ assert_eq!(2+2, 4);
175
187
#[ test]
176
188
fn make_test_with_main ( ) {
177
189
// Including your own `fn main` wrapper lets the test use it verbatim.
178
- let opts = GlobalTestOptions :: default ( ) ;
190
+ let opts = default_global_opts ( "" ) ;
179
191
let input = "fn main() {
180
192
assert_eq!(2+2, 4);
181
193
}" ;
@@ -191,7 +203,7 @@ fn main() {
191
203
#[ test]
192
204
fn make_test_fake_main ( ) {
193
205
// ... but putting it in a comment will still provide a wrapper.
194
- let opts = GlobalTestOptions :: default ( ) ;
206
+ let opts = default_global_opts ( "" ) ;
195
207
let input = "//Ceci n'est pas une `fn main`
196
208
assert_eq!(2+2, 4);" ;
197
209
let expected = "#![allow(unused)]
@@ -207,7 +219,7 @@ assert_eq!(2+2, 4);
207
219
#[ test]
208
220
fn make_test_dont_insert_main ( ) {
209
221
// 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 ( "" ) ;
211
223
let input = "//Ceci n'est pas une `fn main`
212
224
assert_eq!(2+2, 4);" ;
213
225
let expected = "#![allow(unused)]
@@ -219,8 +231,8 @@ assert_eq!(2+2, 4);"
219
231
}
220
232
221
233
#[ 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 ( "" ) ;
224
236
225
237
let input = "// fn main
226
238
assert_eq!(2+2, 4);" ;
@@ -234,6 +246,11 @@ assert_eq!(2+2, 4);
234
246
235
247
let ( output, len, _) = make_test ( input, None , false , & opts, DEFAULT_EDITION , None ) ;
236
248
assert_eq ! ( ( output, len) , ( expected, 2 ) ) ;
249
+ }
250
+
251
+ #[ test]
252
+ fn make_test_issues_33731 ( ) {
253
+ let opts = default_global_opts ( "asdf" ) ;
237
254
238
255
let input = "extern crate hella_qwop;
239
256
assert_eq!(asdf::foo, 4);" ;
@@ -253,7 +270,7 @@ assert_eq!(asdf::foo, 4);
253
270
254
271
#[ test]
255
272
fn make_test_main_in_macro ( ) {
256
- let opts = GlobalTestOptions :: default ( ) ;
273
+ let opts = default_global_opts ( "my_crate" ) ;
257
274
let input = "#[macro_use] extern crate my_crate;
258
275
test_wrapper! {
259
276
fn main() {}
@@ -272,7 +289,7 @@ test_wrapper! {
272
289
#[ test]
273
290
fn make_test_returns_result ( ) {
274
291
// creates an inner function and unwraps it
275
- let opts = GlobalTestOptions :: default ( ) ;
292
+ let opts = default_global_opts ( "" ) ;
276
293
let input = "use std::io;
277
294
let mut input = String::new();
278
295
io::stdin().read_line(&mut input)?;
@@ -292,7 +309,7 @@ Ok::<(), io:Error>(())
292
309
#[ test]
293
310
fn make_test_named_wrapper ( ) {
294
311
// creates an inner function with a specific name
295
- let opts = GlobalTestOptions :: default ( ) ;
312
+ let opts = default_global_opts ( "" ) ;
296
313
let input = "assert_eq!(2+2, 4);" ;
297
314
let expected = "#![allow(unused)]
298
315
fn main() { #[allow(non_snake_case)] fn _doctest_main__some_unique_name() {
@@ -307,8 +324,7 @@ assert_eq!(2+2, 4);
307
324
#[ test]
308
325
fn make_test_insert_extra_space ( ) {
309
326
// 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 ( "" ) } ;
312
328
let input = "use std::*;
313
329
assert_eq!(2+2, 4);
314
330
eprintln!(\" hello anan\" );
@@ -327,8 +343,7 @@ fn main() {
327
343
#[ test]
328
344
fn make_test_insert_extra_space_fn_main ( ) {
329
345
// 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 ( "" ) } ;
332
347
let input = "use std::*;
333
348
fn main() {
334
349
assert_eq!(2+2, 4);
0 commit comments