]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/doctest/tests.rs
Add more proc-macro attribute tests
[rust.git] / src / librustdoc / doctest / tests.rs
1 use super::{make_test, TestOptions};
2 use rustc_span::edition::DEFAULT_EDITION;
3
4 #[test]
5 fn make_test_basic() {
6     //basic use: wraps with `fn main`, adds `#![allow(unused)]`
7     let opts = TestOptions::default();
8     let input = "assert_eq!(2+2, 4);";
9     let expected = "#![allow(unused)]
10 fn main() {
11 assert_eq!(2+2, 4);
12 }"
13     .to_string();
14     let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
15     assert_eq!((output, len), (expected, 2));
16 }
17
18 #[test]
19 fn make_test_crate_name_no_use() {
20     // If you give a crate name but *don't* use it within the test, it won't bother inserting
21     // the `extern crate` statement.
22     let opts = TestOptions::default();
23     let input = "assert_eq!(2+2, 4);";
24     let expected = "#![allow(unused)]
25 fn main() {
26 assert_eq!(2+2, 4);
27 }"
28     .to_string();
29     let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION, None);
30     assert_eq!((output, len), (expected, 2));
31 }
32
33 #[test]
34 fn make_test_crate_name() {
35     // If you give a crate name and use it within the test, it will insert an `extern crate`
36     // statement before `fn main`.
37     let opts = TestOptions::default();
38     let input = "use asdf::qwop;
39 assert_eq!(2+2, 4);";
40     let expected = "#![allow(unused)]
41 extern crate r#asdf;
42 fn main() {
43 use asdf::qwop;
44 assert_eq!(2+2, 4);
45 }"
46     .to_string();
47     let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION, None);
48     assert_eq!((output, len), (expected, 3));
49 }
50
51 #[test]
52 fn make_test_no_crate_inject() {
53     // Even if you do use the crate within the test, setting `opts.no_crate_inject` will skip
54     // adding it anyway.
55     let opts = TestOptions { no_crate_inject: true, display_warnings: false, attrs: vec![] };
56     let input = "use asdf::qwop;
57 assert_eq!(2+2, 4);";
58     let expected = "#![allow(unused)]
59 fn main() {
60 use asdf::qwop;
61 assert_eq!(2+2, 4);
62 }"
63     .to_string();
64     let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION, None);
65     assert_eq!((output, len), (expected, 2));
66 }
67
68 #[test]
69 fn make_test_ignore_std() {
70     // Even if you include a crate name, and use it in the doctest, we still won't include an
71     // `extern crate` statement if the crate is "std" -- that's included already by the
72     // compiler!
73     let opts = TestOptions::default();
74     let input = "use std::*;
75 assert_eq!(2+2, 4);";
76     let expected = "#![allow(unused)]
77 fn main() {
78 use std::*;
79 assert_eq!(2+2, 4);
80 }"
81     .to_string();
82     let (output, len, _) = make_test(input, Some("std"), false, &opts, DEFAULT_EDITION, None);
83     assert_eq!((output, len), (expected, 2));
84 }
85
86 #[test]
87 fn make_test_manual_extern_crate() {
88     // When you manually include an `extern crate` statement in your doctest, `make_test`
89     // assumes you've included one for your own crate too.
90     let opts = TestOptions::default();
91     let input = "extern crate asdf;
92 use asdf::qwop;
93 assert_eq!(2+2, 4);";
94     let expected = "#![allow(unused)]
95 extern crate asdf;
96 fn main() {
97 use asdf::qwop;
98 assert_eq!(2+2, 4);
99 }"
100     .to_string();
101     let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION, None);
102     assert_eq!((output, len), (expected, 2));
103 }
104
105 #[test]
106 fn make_test_manual_extern_crate_with_macro_use() {
107     let opts = TestOptions::default();
108     let input = "#[macro_use] extern crate asdf;
109 use asdf::qwop;
110 assert_eq!(2+2, 4);";
111     let expected = "#![allow(unused)]
112 #[macro_use] extern crate asdf;
113 fn main() {
114 use asdf::qwop;
115 assert_eq!(2+2, 4);
116 }"
117     .to_string();
118     let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION, None);
119     assert_eq!((output, len), (expected, 2));
120 }
121
122 #[test]
123 fn make_test_opts_attrs() {
124     // If you supplied some doctest attributes with `#![doc(test(attr(...)))]`, it will use
125     // those instead of the stock `#![allow(unused)]`.
126     let mut opts = TestOptions::default();
127     opts.attrs.push("feature(sick_rad)".to_string());
128     let input = "use asdf::qwop;
129 assert_eq!(2+2, 4);";
130     let expected = "#![feature(sick_rad)]
131 extern crate r#asdf;
132 fn main() {
133 use asdf::qwop;
134 assert_eq!(2+2, 4);
135 }"
136     .to_string();
137     let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION, None);
138     assert_eq!((output, len), (expected, 3));
139
140     // Adding more will also bump the returned line offset.
141     opts.attrs.push("feature(hella_dope)".to_string());
142     let expected = "#![feature(sick_rad)]
143 #![feature(hella_dope)]
144 extern crate r#asdf;
145 fn main() {
146 use asdf::qwop;
147 assert_eq!(2+2, 4);
148 }"
149     .to_string();
150     let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION, None);
151     assert_eq!((output, len), (expected, 4));
152 }
153
154 #[test]
155 fn make_test_crate_attrs() {
156     // Including inner attributes in your doctest will apply them to the whole "crate", pasting
157     // them outside the generated main function.
158     let opts = TestOptions::default();
159     let input = "#![feature(sick_rad)]
160 assert_eq!(2+2, 4);";
161     let expected = "#![allow(unused)]
162 #![feature(sick_rad)]
163 fn main() {
164 assert_eq!(2+2, 4);
165 }"
166     .to_string();
167     let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
168     assert_eq!((output, len), (expected, 2));
169 }
170
171 #[test]
172 fn make_test_with_main() {
173     // Including your own `fn main` wrapper lets the test use it verbatim.
174     let opts = TestOptions::default();
175     let input = "fn main() {
176     assert_eq!(2+2, 4);
177 }";
178     let expected = "#![allow(unused)]
179 fn main() {
180     assert_eq!(2+2, 4);
181 }"
182     .to_string();
183     let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
184     assert_eq!((output, len), (expected, 1));
185 }
186
187 #[test]
188 fn make_test_fake_main() {
189     // ... but putting it in a comment will still provide a wrapper.
190     let opts = TestOptions::default();
191     let input = "//Ceci n'est pas une `fn main`
192 assert_eq!(2+2, 4);";
193     let expected = "#![allow(unused)]
194 //Ceci n'est pas une `fn main`
195 fn main() {
196 assert_eq!(2+2, 4);
197 }"
198     .to_string();
199     let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
200     assert_eq!((output, len), (expected, 2));
201 }
202
203 #[test]
204 fn make_test_dont_insert_main() {
205     // Even with that, if you set `dont_insert_main`, it won't create the `fn main` wrapper.
206     let opts = TestOptions::default();
207     let input = "//Ceci n'est pas une `fn main`
208 assert_eq!(2+2, 4);";
209     let expected = "#![allow(unused)]
210 //Ceci n'est pas une `fn main`
211 assert_eq!(2+2, 4);"
212         .to_string();
213     let (output, len, _) = make_test(input, None, true, &opts, DEFAULT_EDITION, None);
214     assert_eq!((output, len), (expected, 1));
215 }
216
217 #[test]
218 fn make_test_display_warnings() {
219     // If the user is asking to display doctest warnings, suppress the default `allow(unused)`.
220     let mut opts = TestOptions::default();
221     opts.display_warnings = true;
222     let input = "assert_eq!(2+2, 4);";
223     let expected = "fn main() {
224 assert_eq!(2+2, 4);
225 }"
226     .to_string();
227     let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
228     assert_eq!((output, len), (expected, 1));
229 }
230
231 #[test]
232 fn make_test_issues_21299_33731() {
233     let opts = TestOptions::default();
234
235     let input = "// fn main
236 assert_eq!(2+2, 4);";
237
238     let expected = "#![allow(unused)]
239 // fn main
240 fn main() {
241 assert_eq!(2+2, 4);
242 }"
243     .to_string();
244
245     let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
246     assert_eq!((output, len), (expected, 2));
247
248     let input = "extern crate hella_qwop;
249 assert_eq!(asdf::foo, 4);";
250
251     let expected = "#![allow(unused)]
252 extern crate hella_qwop;
253 extern crate r#asdf;
254 fn main() {
255 assert_eq!(asdf::foo, 4);
256 }"
257     .to_string();
258
259     let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION, None);
260     assert_eq!((output, len), (expected, 3));
261 }
262
263 #[test]
264 fn make_test_main_in_macro() {
265     let opts = TestOptions::default();
266     let input = "#[macro_use] extern crate my_crate;
267 test_wrapper! {
268     fn main() {}
269 }";
270     let expected = "#![allow(unused)]
271 #[macro_use] extern crate my_crate;
272 test_wrapper! {
273     fn main() {}
274 }"
275     .to_string();
276
277     let (output, len, _) = make_test(input, Some("my_crate"), false, &opts, DEFAULT_EDITION, None);
278     assert_eq!((output, len), (expected, 1));
279 }
280
281 #[test]
282 fn make_test_returns_result() {
283     // creates an inner function and unwraps it
284     let opts = TestOptions::default();
285     let input = "use std::io;
286 let mut input = String::new();
287 io::stdin().read_line(&mut input)?;
288 Ok::<(), io:Error>(())";
289     let expected = "#![allow(unused)]
290 fn main() { fn _inner() -> Result<(), impl core::fmt::Debug> {
291 use std::io;
292 let mut input = String::new();
293 io::stdin().read_line(&mut input)?;
294 Ok::<(), io:Error>(())
295 } _inner().unwrap() }"
296         .to_string();
297     let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
298     assert_eq!((output, len), (expected, 2));
299 }
300
301 #[test]
302 fn make_test_named_wrapper() {
303     // creates an inner function with a specific name
304     let opts = TestOptions::default();
305     let input = "assert_eq!(2+2, 4);";
306     let expected = "#![allow(unused)]
307 fn main() { #[allow(non_snake_case)] fn _doctest_main__some_unique_name() {
308 assert_eq!(2+2, 4);
309 } _doctest_main__some_unique_name() }"
310         .to_string();
311     let (output, len, _) =
312         make_test(input, None, false, &opts, DEFAULT_EDITION, Some("_some_unique_name"));
313     assert_eq!((output, len), (expected, 2));
314 }