1 use errors::{self, FatalError};
2 use errors::emitter::ColorConfig;
3 use rustc_data_structures::sync::Lrc;
5 use rustc_driver::{self, driver, target_features, Compilation};
6 use rustc_driver::driver::phase_2_configure_and_expand;
7 use rustc_metadata::cstore::CStore;
8 use rustc_metadata::dynamic_lib::DynamicLibrary;
10 use rustc::hir::intravisit;
11 use rustc::session::{self, CompileIncomplete, config};
12 use rustc::session::config::{OutputType, OutputTypes, Externs, CodegenOptions};
13 use rustc::session::search_paths::{SearchPath, PathKind};
15 use syntax::source_map::SourceMap;
16 use syntax::edition::Edition;
17 use syntax::feature_gate::UnstableFeatures;
18 use syntax::with_globals;
19 use syntax_pos::{BytePos, DUMMY_SP, Pos, Span, FileName};
20 use tempfile::Builder as TempFileBuilder;
24 use std::ffi::OsString;
25 use std::io::prelude::*;
27 use std::path::PathBuf;
28 use std::panic::{self, AssertUnwindSafe};
29 use std::process::Command;
31 use std::sync::{Arc, Mutex};
33 use clean::Attributes;
35 use html::markdown::{self, ErrorCodes, LangString};
37 #[derive(Clone, Default)]
38 pub struct TestOptions {
39 /// Whether to disable the default `extern crate my_crate;` when creating doctests.
40 pub no_crate_inject: bool,
41 /// Whether to emit compilation warnings when compiling doctests. Setting this will suppress
42 /// the default `#![allow(unused)]`.
43 pub display_warnings: bool,
44 /// Additional crate-level attributes to add to doctests.
45 pub attrs: Vec<String>,
48 pub fn run(mut options: Options) -> isize {
49 let input = config::Input::File(options.input.clone());
51 let sessopts = config::Options {
52 maybe_sysroot: options.maybe_sysroot.clone().or_else(
53 || Some(env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_path_buf())),
54 search_paths: options.libs.clone(),
55 crate_types: vec![config::CrateType::Dylib],
56 cg: options.codegen_options.clone(),
57 externs: options.externs.clone(),
58 unstable_features: UnstableFeatures::from_environment(),
59 lint_cap: Some(::rustc::lint::Level::Allow),
60 actually_rustdoc: true,
61 debugging_opts: config::DebuggingOptions {
62 ..config::basic_debugging_options()
64 edition: options.edition,
65 ..config::Options::default()
67 driver::spawn_thread_pool(sessopts, |sessopts| {
68 let source_map = Lrc::new(SourceMap::new(sessopts.file_path_mapping()));
70 errors::Handler::with_tty_emitter(ColorConfig::Auto,
72 Some(source_map.clone()));
74 let mut sess = session::build_session_(
75 sessopts, Some(options.input), handler, source_map.clone(),
77 let codegen_backend = rustc_driver::get_codegen_backend(&sess);
78 let cstore = CStore::new(codegen_backend.metadata_loader());
79 rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
81 let mut cfg = config::build_configuration(&sess,
82 config::parse_cfgspecs(options.cfgs.clone()));
83 target_features::add_configuration(&mut cfg, &sess, &*codegen_backend);
84 sess.parse_sess.config = cfg;
87 match driver::phase_1_parse_input(&driver::CompileController::basic(), &sess, &input) {
94 let driver::ExpansionResult { defs, mut hir_forest, .. } = {
95 phase_2_configure_and_expand(
103 ).expect("phase_2_configure_and_expand aborted in rustdoc!")
106 let crate_name = options.crate_name.unwrap_or_else(|| {
107 ::rustc_codegen_utils::link::find_crate_name(None, &hir_forest.krate().attrs, &input)
109 let mut opts = scrape_test_config(hir_forest.krate());
110 opts.display_warnings |= options.display_warnings;
111 let mut collector = Collector::new(
115 options.codegen_options,
119 options.maybe_sysroot,
124 options.persist_doctests,
128 let map = hir::map::map_crate(&sess, &cstore, &mut hir_forest, &defs);
129 let krate = map.krate();
130 let mut hir_collector = HirCollector {
132 collector: &mut collector,
134 codes: ErrorCodes::from(sess.opts.unstable_features.is_nightly_build()),
136 hir_collector.visit_testable("".to_string(), &krate.attrs, |this| {
137 intravisit::walk_crate(this, krate);
141 options.test_args.insert(0, "rustdoctest".to_string());
143 testing::test_main(&options.test_args,
144 collector.tests.into_iter().collect(),
145 testing::Options::new().display_output(options.display_warnings));
150 // Look for `#![doc(test(no_crate_inject))]`, used by crates in the std facade.
151 fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
152 use syntax::print::pprust;
154 let mut opts = TestOptions {
155 no_crate_inject: false,
156 display_warnings: false,
160 let test_attrs: Vec<_> = krate.attrs.iter()
161 .filter(|a| a.check_name("doc"))
162 .flat_map(|a| a.meta_item_list().unwrap_or_else(Vec::new))
163 .filter(|a| a.check_name("test"))
165 let attrs = test_attrs.iter().flat_map(|a| a.meta_item_list().unwrap_or(&[]));
168 if attr.check_name("no_crate_inject") {
169 opts.no_crate_inject = true;
171 if attr.check_name("attr") {
172 if let Some(l) = attr.meta_item_list() {
174 opts.attrs.push(pprust::meta_list_item_to_string(item));
183 fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
184 cfgs: Vec<String>, libs: Vec<SearchPath>,
185 cg: CodegenOptions, externs: Externs,
186 should_panic: bool, no_run: bool, as_test_harness: bool,
187 compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions,
188 maybe_sysroot: Option<PathBuf>, linker: Option<PathBuf>, edition: Edition,
189 persist_doctests: Option<PathBuf>) {
190 // The test harness wants its own `main` and top-level functions, so
191 // never wrap the test in `fn main() { ... }`.
192 let (test, line_offset) = make_test(test, Some(cratename), as_test_harness, opts);
193 // FIXME(#44940): if doctests ever support path remapping, then this filename
194 // needs to be the result of `SourceMap::span_to_unmapped_path`.
195 let path = match filename {
196 FileName::Real(path) => path.clone(),
197 _ => PathBuf::from(r"doctest.rs"),
200 let input = config::Input::Str {
201 name: FileName::DocTest(path, line as isize - line_offset as isize),
204 let outputs = OutputTypes::new(&[(OutputType::Exe, None)]);
206 let sessopts = config::Options {
207 maybe_sysroot: maybe_sysroot.or_else(
208 || Some(env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_path_buf())),
210 crate_types: vec![config::CrateType::Executable],
211 output_types: outputs,
213 cg: config::CodegenOptions {
217 test: as_test_harness,
218 unstable_features: UnstableFeatures::from_environment(),
219 debugging_opts: config::DebuggingOptions {
220 ..config::basic_debugging_options()
223 ..config::Options::default()
226 // Shuffle around a few input and output handles here. We're going to pass
227 // an explicit handle into rustc to collect output messages, but we also
228 // want to catch the error message that rustc prints when it fails.
230 // We take our thread-local stderr (likely set by the test runner) and replace
231 // it with a sink that is also passed to rustc itself. When this function
232 // returns the output of the sink is copied onto the output of our own thread.
234 // The basic idea is to not use a default Handler for rustc, and then also
235 // not print things by default to the actual stderr.
236 struct Sink(Arc<Mutex<Vec<u8>>>);
237 impl Write for Sink {
238 fn write(&mut self, data: &[u8]) -> io::Result<usize> {
239 Write::write(&mut *self.0.lock().unwrap(), data)
241 fn flush(&mut self) -> io::Result<()> { Ok(()) }
243 struct Bomb(Arc<Mutex<Vec<u8>>>, Box<dyn Write+Send>);
246 let _ = self.1.write_all(&self.0.lock().unwrap());
249 let data = Arc::new(Mutex::new(Vec::new()));
251 let old = io::set_panic(Some(box Sink(data.clone())));
252 let _bomb = Bomb(data.clone(), old.unwrap_or(box io::stdout()));
255 Temp(tempfile::TempDir),
260 fn path(&self) -> &std::path::Path {
262 DirState::Temp(t) => t.path(),
263 DirState::Perm(p) => p.as_path(),
268 let (libdir, outdir, compile_result) = driver::spawn_thread_pool(sessopts, |sessopts| {
269 let source_map = Lrc::new(SourceMap::new(sessopts.file_path_mapping()));
270 let emitter = errors::emitter::EmitterWriter::new(box Sink(data.clone()),
271 Some(source_map.clone()),
276 let diagnostic_handler = errors::Handler::with_emitter(true, false, box emitter);
278 let mut sess = session::build_session_(
279 sessopts, None, diagnostic_handler, source_map,
281 let codegen_backend = rustc_driver::get_codegen_backend(&sess);
282 let cstore = CStore::new(codegen_backend.metadata_loader());
283 rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
285 let outdir = Mutex::new(
286 if let Some(mut path) = persist_doctests {
287 path.push(format!("{}_{}",
296 std::fs::create_dir_all(&path)
297 .expect("Couldn't create directory for doctest executables");
301 DirState::Temp(TempFileBuilder::new()
302 .prefix("rustdoctest")
304 .expect("rustdoc needs a tempdir"))
307 let libdir = sess.target_filesearch(PathKind::All).get_lib_path();
308 let mut control = driver::CompileController::basic();
310 let mut cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs.clone()));
311 target_features::add_configuration(&mut cfg, &sess, &*codegen_backend);
312 sess.parse_sess.config = cfg;
314 let out = Some(outdir.lock().unwrap().path().join("rust_out"));
317 control.after_analysis.stop = Compilation::Stop;
320 let res = panic::catch_unwind(AssertUnwindSafe(|| {
321 driver::compile_input(
334 let compile_result = match res {
335 Ok(Ok(())) | Ok(Err(CompileIncomplete::Stopped)) => Ok(()),
336 Err(_) | Ok(Err(CompileIncomplete::Errored(_))) => Err(())
339 (libdir, outdir, compile_result)
342 match (compile_result, compile_fail) {
344 panic!("test compiled while it wasn't supposed to")
346 (Ok(()), false) => {}
348 if error_codes.len() > 0 {
349 let out = String::from_utf8(data.lock().unwrap().to_vec()).unwrap();
350 error_codes.retain(|err| !out.contains(err));
353 (Err(()), false) => {
354 panic!("couldn't compile the test")
358 if error_codes.len() > 0 {
359 panic!("Some expected error codes were not found: {:?}", error_codes);
366 // We're careful to prepend the *target* dylib search path to the child's
367 // environment to ensure that the target loads the right libraries at
368 // runtime. It would be a sad day if the *host* libraries were loaded as a
370 let mut cmd = Command::new(&outdir.lock().unwrap().path().join("rust_out"));
371 let var = DynamicLibrary::envvar();
373 let path = env::var_os(var).unwrap_or(OsString::new());
374 let mut path = env::split_paths(&path).collect::<Vec<_>>();
375 path.insert(0, libdir);
376 env::join_paths(path).unwrap()
378 cmd.env(var, &newpath);
381 Err(e) => panic!("couldn't run the test: {}{}", e,
382 if e.kind() == io::ErrorKind::PermissionDenied {
383 " - maybe your tempdir is mounted with noexec?"
386 if should_panic && out.status.success() {
387 panic!("test executable succeeded when it should have failed");
388 } else if !should_panic && !out.status.success() {
389 panic!("test executable failed:\n{}\n{}\n",
390 str::from_utf8(&out.stdout).unwrap_or(""),
391 str::from_utf8(&out.stderr).unwrap_or(""));
397 /// Makes the test file. Also returns the number of lines before the code begins
398 pub fn make_test(s: &str,
399 cratename: Option<&str>,
400 dont_insert_main: bool,
403 let (crate_attrs, everything_else, crates) = partition_source(s);
404 let everything_else = everything_else.trim();
405 let mut line_offset = 0;
406 let mut prog = String::new();
408 if opts.attrs.is_empty() && !opts.display_warnings {
409 // If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
410 // lints that are commonly triggered in doctests. The crate-level test attributes are
411 // commonly used to make tests fail in case they trigger warnings, so having this there in
412 // that case may cause some tests to pass when they shouldn't have.
413 prog.push_str("#![allow(unused)]\n");
417 // Next, any attributes that came from the crate root via #![doc(test(attr(...)))].
418 for attr in &opts.attrs {
419 prog.push_str(&format!("#![{}]\n", attr));
423 // Now push any outer attributes from the example, assuming they
424 // are intended to be crate attributes.
425 prog.push_str(&crate_attrs);
426 prog.push_str(&crates);
428 // Uses libsyntax to parse the doctest and find if there's a main fn and the extern
429 // crate already is included.
430 let (already_has_main, already_has_extern_crate, found_macro) = crate::syntax::with_globals(|| {
431 use crate::syntax::{ast, parse::{self, ParseSess}, source_map::FilePathMapping};
432 use crate::syntax_pos::FileName;
433 use errors::emitter::EmitterWriter;
436 let filename = FileName::anon_source_code(s);
437 let source = crates + &everything_else;
439 // Any errors in parsing should also appear when the doctest is compiled for real, so just
440 // send all the errors that libsyntax emits directly into a `Sink` instead of stderr.
441 let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
442 let emitter = EmitterWriter::new(box io::sink(), None, false, false);
443 let handler = Handler::with_emitter(false, false, box emitter);
444 let sess = ParseSess::with_span_handler(handler, cm);
446 let mut found_main = false;
447 let mut found_extern_crate = cratename.is_none();
448 let mut found_macro = false;
450 let mut parser = match parse::maybe_new_parser_from_source_str(&sess, filename, source) {
453 for mut err in errs {
457 return (found_main, found_extern_crate, found_macro);
462 match parser.parse_item() {
465 if let ast::ItemKind::Fn(..) = item.node {
466 if item.ident.as_str() == "main" {
472 if !found_extern_crate {
473 if let ast::ItemKind::ExternCrate(original) = item.node {
474 // This code will never be reached if `cratename` is none because
475 // `found_extern_crate` is initialized to `true` if it is none.
476 let cratename = cratename.unwrap();
479 Some(name) => found_extern_crate = name.as_str() == cratename,
480 None => found_extern_crate = item.ident.as_str() == cratename,
486 if let ast::ItemKind::Mac(..) = item.node {
491 if found_main && found_extern_crate {
503 (found_main, found_extern_crate, found_macro)
506 // If a doctest's `fn main` is being masked by a wrapper macro, the parsing loop above won't
507 // see it. In that case, run the old text-based scan to see if they at least have a main
508 // function written inside a macro invocation. See
509 // https://github.com/rust-lang/rust/issues/56898
510 let already_has_main = if found_macro && !already_has_main {
513 let comment = line.find("//");
514 if let Some(comment_begins) = comment {
515 &line[0..comment_begins]
520 .any(|code| code.contains("fn main"))
525 // Don't inject `extern crate std` because it's already injected by the
527 if !already_has_extern_crate && !opts.no_crate_inject && cratename != Some("std") {
528 if let Some(cratename) = cratename {
529 // Make sure its actually used if not included.
530 if s.contains(cratename) {
531 prog.push_str(&format!("extern crate {};\n", cratename));
537 if dont_insert_main || already_has_main {
538 prog.push_str(everything_else);
540 prog.push_str("fn main() {\n");
542 prog.push_str(everything_else);
543 prog.push_str("\n}");
546 debug!("final doctest:\n{}", prog);
551 // FIXME(aburka): use a real parser to deal with multiline attributes
552 fn partition_source(s: &str) -> (String, String, String) {
553 #[derive(Copy, Clone, PartialEq)]
554 enum PartitionState {
559 let mut state = PartitionState::Attrs;
560 let mut before = String::new();
561 let mut crates = String::new();
562 let mut after = String::new();
564 for line in s.lines() {
565 let trimline = line.trim();
567 // FIXME(misdreavus): if a doc comment is placed on an extern crate statement, it will be
568 // shunted into "everything else"
570 PartitionState::Attrs => {
571 state = if trimline.starts_with("#![") ||
572 trimline.chars().all(|c| c.is_whitespace()) ||
573 (trimline.starts_with("//") && !trimline.starts_with("///"))
575 PartitionState::Attrs
576 } else if trimline.starts_with("extern crate") ||
577 trimline.starts_with("#[macro_use] extern crate")
579 PartitionState::Crates
581 PartitionState::Other
584 PartitionState::Crates => {
585 state = if trimline.starts_with("extern crate") ||
586 trimline.starts_with("#[macro_use] extern crate") ||
587 trimline.chars().all(|c| c.is_whitespace()) ||
588 (trimline.starts_with("//") && !trimline.starts_with("///"))
590 PartitionState::Crates
592 PartitionState::Other
595 PartitionState::Other => {}
599 PartitionState::Attrs => {
600 before.push_str(line);
601 before.push_str("\n");
603 PartitionState::Crates => {
604 crates.push_str(line);
605 crates.push_str("\n");
607 PartitionState::Other => {
608 after.push_str(line);
609 after.push_str("\n");
614 debug!("before:\n{}", before);
615 debug!("crates:\n{}", crates);
616 debug!("after:\n{}", after);
618 (before, after, crates)
622 fn add_test(&mut self, test: String, config: LangString, line: usize);
623 fn get_line(&self) -> usize {
626 fn register_header(&mut self, _name: &str, _level: u32) {}
629 pub struct Collector {
630 pub tests: Vec<testing::TestDescAndFn>,
632 // The name of the test displayed to the user, separated by `::`.
634 // In tests from Rust source, this is the path to the item
635 // e.g., `["std", "vec", "Vec", "push"]`.
637 // In tests from a markdown file, this is the titles of all headers (h1~h6)
638 // of the sections that contain the code block, e.g., if the markdown file is
651 // the `names` vector of that test will be `["Title", "Subtitle"]`.
655 libs: Vec<SearchPath>,
661 maybe_sysroot: Option<PathBuf>,
663 source_map: Option<Lrc<SourceMap>>,
664 filename: Option<PathBuf>,
665 linker: Option<PathBuf>,
667 persist_doctests: Option<PathBuf>,
671 pub fn new(cratename: String, cfgs: Vec<String>, libs: Vec<SearchPath>, cg: CodegenOptions,
672 externs: Externs, use_headers: bool, opts: TestOptions,
673 maybe_sysroot: Option<PathBuf>, source_map: Option<Lrc<SourceMap>>,
674 filename: Option<PathBuf>, linker: Option<PathBuf>, edition: Edition,
675 persist_doctests: Option<PathBuf>) -> Collector {
696 fn generate_name(&self, line: usize, filename: &FileName) -> String {
697 format!("{} - {} (line {})", filename, self.names.join("::"), line)
700 pub fn set_position(&mut self, position: Span) {
701 self.position = position;
704 fn get_filename(&self) -> FileName {
705 if let Some(ref source_map) = self.source_map {
706 let filename = source_map.span_to_filename(self.position);
707 if let FileName::Real(ref filename) = filename {
708 if let Ok(cur_dir) = env::current_dir() {
709 if let Ok(path) = filename.strip_prefix(&cur_dir) {
710 return path.to_owned().into();
715 } else if let Some(ref filename) = self.filename {
716 filename.clone().into()
718 FileName::Custom("input".to_owned())
723 impl Tester for Collector {
724 fn add_test(&mut self, test: String, config: LangString, line: usize) {
725 let filename = self.get_filename();
726 let name = self.generate_name(line, &filename);
727 let cfgs = self.cfgs.clone();
728 let libs = self.libs.clone();
729 let cg = self.cg.clone();
730 let externs = self.externs.clone();
731 let cratename = self.cratename.to_string();
732 let opts = self.opts.clone();
733 let maybe_sysroot = self.maybe_sysroot.clone();
734 let linker = self.linker.clone();
735 let edition = config.edition.unwrap_or(self.edition);
736 let persist_doctests = self.persist_doctests.clone();
738 debug!("Creating test {}: {}", name, test);
739 self.tests.push(testing::TestDescAndFn {
740 desc: testing::TestDesc {
741 name: testing::DynTestName(name.clone()),
742 ignore: config.ignore,
743 // compiler failures are test failures
744 should_panic: testing::ShouldPanic::No,
745 allow_fail: config.allow_fail,
747 testfn: testing::DynTestFn(box move || {
748 let panic = io::set_panic(None);
749 let print = io::set_print(None);
751 rustc_driver::in_named_rustc_thread(name, move || with_globals(move || {
752 io::set_panic(panic);
753 io::set_print(print);
775 Err(err) => panic::resume_unwind(err),
781 fn get_line(&self) -> usize {
782 if let Some(ref source_map) = self.source_map {
783 let line = self.position.lo().to_usize();
784 let line = source_map.lookup_char_pos(BytePos(line as u32)).line;
785 if line > 0 { line - 1 } else { line }
791 fn register_header(&mut self, name: &str, level: u32) {
792 if self.use_headers {
793 // We use these headings as test names, so it's good if
794 // they're valid identifiers.
795 let name = name.chars().enumerate().map(|(i, c)| {
796 if (i == 0 && c.is_xid_start()) ||
797 (i != 0 && c.is_xid_continue()) {
802 }).collect::<String>();
804 // Here we try to efficiently assemble the header titles into the
805 // test name in the form of `h1::h2::h3::h4::h5::h6`.
807 // Suppose that originally `self.names` contains `[h1, h2, h3]`...
808 let level = level as usize;
809 if level <= self.names.len() {
810 // ... Consider `level == 2`. All headers in the lower levels
811 // are irrelevant in this new level. So we should reset
812 // `self.names` to contain headers until <h2>, and replace that
813 // slot with the new name: `[h1, name]`.
814 self.names.truncate(level);
815 self.names[level - 1] = name;
817 // ... On the other hand, consider `level == 5`. This means we
818 // need to extend `self.names` to contain five headers. We fill
819 // in the missing level (<h4>) with `_`. Thus `self.names` will
820 // become `[h1, h2, h3, "_", name]`.
821 if level - 1 > self.names.len() {
822 self.names.resize(level - 1, "_".to_owned());
824 self.names.push(name);
830 struct HirCollector<'a, 'hir: 'a> {
831 sess: &'a session::Session,
832 collector: &'a mut Collector,
833 map: &'a hir::map::Map<'hir>,
837 impl<'a, 'hir> HirCollector<'a, 'hir> {
838 fn visit_testable<F: FnOnce(&mut Self)>(&mut self,
840 attrs: &[ast::Attribute],
842 let mut attrs = Attributes::from_ast(self.sess.diagnostic(), attrs);
843 if let Some(ref cfg) = attrs.cfg {
844 if !cfg.matches(&self.sess.parse_sess, Some(&self.sess.features_untracked())) {
849 let has_name = !name.is_empty();
851 self.collector.names.push(name);
854 attrs.collapse_doc_comments();
855 attrs.unindent_doc_comments();
856 // The collapse-docs pass won't combine sugared/raw doc attributes, or included files with
857 // anything else, this will combine them for us.
858 if let Some(doc) = attrs.collapsed_doc_value() {
859 self.collector.set_position(attrs.span.unwrap_or(DUMMY_SP));
860 let res = markdown::find_testable_code(&doc, self.collector, self.codes);
861 if let Err(err) = res {
862 self.sess.diagnostic().span_warn(attrs.span.unwrap_or(DUMMY_SP),
870 self.collector.names.pop();
875 impl<'a, 'hir> intravisit::Visitor<'hir> for HirCollector<'a, 'hir> {
876 fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'hir> {
877 intravisit::NestedVisitorMap::All(&self.map)
880 fn visit_item(&mut self, item: &'hir hir::Item) {
881 let name = if let hir::ItemKind::Impl(.., ref ty, _) = item.node {
882 self.map.node_to_pretty_string(ty.id)
884 item.ident.to_string()
887 self.visit_testable(name, &item.attrs, |this| {
888 intravisit::walk_item(this, item);
892 fn visit_trait_item(&mut self, item: &'hir hir::TraitItem) {
893 self.visit_testable(item.ident.to_string(), &item.attrs, |this| {
894 intravisit::walk_trait_item(this, item);
898 fn visit_impl_item(&mut self, item: &'hir hir::ImplItem) {
899 self.visit_testable(item.ident.to_string(), &item.attrs, |this| {
900 intravisit::walk_impl_item(this, item);
904 fn visit_foreign_item(&mut self, item: &'hir hir::ForeignItem) {
905 self.visit_testable(item.ident.to_string(), &item.attrs, |this| {
906 intravisit::walk_foreign_item(this, item);
910 fn visit_variant(&mut self,
911 v: &'hir hir::Variant,
912 g: &'hir hir::Generics,
913 item_id: ast::NodeId) {
914 self.visit_testable(v.node.ident.to_string(), &v.node.attrs, |this| {
915 intravisit::walk_variant(this, v, g, item_id);
919 fn visit_struct_field(&mut self, f: &'hir hir::StructField) {
920 self.visit_testable(f.ident.to_string(), &f.attrs, |this| {
921 intravisit::walk_struct_field(this, f);
925 fn visit_macro_def(&mut self, macro_def: &'hir hir::MacroDef) {
926 self.visit_testable(macro_def.name.to_string(), ¯o_def.attrs, |_| ());
932 use super::{TestOptions, make_test};
935 fn make_test_basic() {
936 //basic use: wraps with `fn main`, adds `#![allow(unused)]`
937 let opts = TestOptions::default();
939 "assert_eq!(2+2, 4);";
945 let output = make_test(input, None, false, &opts);
946 assert_eq!(output, (expected, 2));
950 fn make_test_crate_name_no_use() {
951 // If you give a crate name but *don't* use it within the test, it won't bother inserting
952 // the `extern crate` statement.
953 let opts = TestOptions::default();
955 "assert_eq!(2+2, 4);";
961 let output = make_test(input, Some("asdf"), false, &opts);
962 assert_eq!(output, (expected, 2));
966 fn make_test_crate_name() {
967 // If you give a crate name and use it within the test, it will insert an `extern crate`
968 // statement before `fn main`.
969 let opts = TestOptions::default();
972 assert_eq!(2+2, 4);";
980 let output = make_test(input, Some("asdf"), false, &opts);
981 assert_eq!(output, (expected, 3));
985 fn make_test_no_crate_inject() {
986 // Even if you do use the crate within the test, setting `opts.no_crate_inject` will skip
988 let opts = TestOptions {
989 no_crate_inject: true,
990 display_warnings: false,
995 assert_eq!(2+2, 4);";
1002 let output = make_test(input, Some("asdf"), false, &opts);
1003 assert_eq!(output, (expected, 2));
1007 fn make_test_ignore_std() {
1008 // Even if you include a crate name, and use it in the doctest, we still won't include an
1009 // `extern crate` statement if the crate is "std" -- that's included already by the
1011 let opts = TestOptions::default();
1014 assert_eq!(2+2, 4);";
1021 let output = make_test(input, Some("std"), false, &opts);
1022 assert_eq!(output, (expected, 2));
1026 fn make_test_manual_extern_crate() {
1027 // When you manually include an `extern crate` statement in your doctest, `make_test`
1028 // assumes you've included one for your own crate too.
1029 let opts = TestOptions::default();
1033 assert_eq!(2+2, 4);";
1041 let output = make_test(input, Some("asdf"), false, &opts);
1042 assert_eq!(output, (expected, 2));
1046 fn make_test_manual_extern_crate_with_macro_use() {
1047 let opts = TestOptions::default();
1049 "#[macro_use] extern crate asdf;
1051 assert_eq!(2+2, 4);";
1054 #[macro_use] extern crate asdf;
1059 let output = make_test(input, Some("asdf"), false, &opts);
1060 assert_eq!(output, (expected, 2));
1064 fn make_test_opts_attrs() {
1065 // If you supplied some doctest attributes with `#![doc(test(attr(...)))]`, it will use
1066 // those instead of the stock `#![allow(unused)]`.
1067 let mut opts = TestOptions::default();
1068 opts.attrs.push("feature(sick_rad)".to_string());
1071 assert_eq!(2+2, 4);";
1073 "#![feature(sick_rad)]
1079 let output = make_test(input, Some("asdf"), false, &opts);
1080 assert_eq!(output, (expected, 3));
1082 // Adding more will also bump the returned line offset.
1083 opts.attrs.push("feature(hella_dope)".to_string());
1085 "#![feature(sick_rad)]
1086 #![feature(hella_dope)]
1092 let output = make_test(input, Some("asdf"), false, &opts);
1093 assert_eq!(output, (expected, 4));
1097 fn make_test_crate_attrs() {
1098 // Including inner attributes in your doctest will apply them to the whole "crate", pasting
1099 // them outside the generated main function.
1100 let opts = TestOptions::default();
1102 "#![feature(sick_rad)]
1103 assert_eq!(2+2, 4);";
1106 #![feature(sick_rad)]
1110 let output = make_test(input, None, false, &opts);
1111 assert_eq!(output, (expected, 2));
1115 fn make_test_with_main() {
1116 // Including your own `fn main` wrapper lets the test use it verbatim.
1117 let opts = TestOptions::default();
1127 let output = make_test(input, None, false, &opts);
1128 assert_eq!(output, (expected, 1));
1132 fn make_test_fake_main() {
1133 // ... but putting it in a comment will still provide a wrapper.
1134 let opts = TestOptions::default();
1136 "//Ceci n'est pas une `fn main`
1137 assert_eq!(2+2, 4);";
1140 //Ceci n'est pas une `fn main`
1144 let output = make_test(input, None, false, &opts);
1145 assert_eq!(output, (expected, 2));
1149 fn make_test_dont_insert_main() {
1150 // Even with that, if you set `dont_insert_main`, it won't create the `fn main` wrapper.
1151 let opts = TestOptions::default();
1153 "//Ceci n'est pas une `fn main`
1154 assert_eq!(2+2, 4);";
1157 //Ceci n'est pas une `fn main`
1158 assert_eq!(2+2, 4);".to_string();
1159 let output = make_test(input, None, true, &opts);
1160 assert_eq!(output, (expected, 1));
1164 fn make_test_display_warnings() {
1165 // If the user is asking to display doctest warnings, suppress the default `allow(unused)`.
1166 let mut opts = TestOptions::default();
1167 opts.display_warnings = true;
1169 "assert_eq!(2+2, 4);";
1174 let output = make_test(input, None, false, &opts);
1175 assert_eq!(output, (expected, 1));
1179 fn make_test_issues_21299_33731() {
1180 let opts = TestOptions::default();
1184 assert_eq!(2+2, 4);";
1193 let output = make_test(input, None, false, &opts);
1194 assert_eq!(output, (expected, 2));
1197 "extern crate hella_qwop;
1198 assert_eq!(asdf::foo, 4);";
1202 extern crate hella_qwop;
1205 assert_eq!(asdf::foo, 4);
1208 let output = make_test(input, Some("asdf"), false, &opts);
1209 assert_eq!(output, (expected, 3));
1213 fn make_test_main_in_macro() {
1214 let opts = TestOptions::default();
1216 "#[macro_use] extern crate my_crate;
1222 #[macro_use] extern crate my_crate;
1227 let output = make_test(input, Some("my_crate"), false, &opts);
1228 assert_eq!(output, (expected, 1));