Fix broken rustdoc unit tests
This commit is contained in:
parent
815c447680
commit
3670ad59ad
@ -1,10 +1,23 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use super::{make_test, GlobalTestOptions};
|
use super::{make_test, GlobalTestOptions};
|
||||||
use rustc_span::edition::DEFAULT_EDITION;
|
use rustc_span::edition::DEFAULT_EDITION;
|
||||||
|
|
||||||
|
/// Default [`GlobalTestOptions`] for these unit tests.
|
||||||
|
fn default_global_opts(crate_name: impl Into<String>) -> GlobalTestOptions {
|
||||||
|
GlobalTestOptions {
|
||||||
|
crate_name: crate_name.into(),
|
||||||
|
no_crate_inject: false,
|
||||||
|
insert_indent_space: false,
|
||||||
|
attrs: vec![],
|
||||||
|
args_file: PathBuf::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn make_test_basic() {
|
fn make_test_basic() {
|
||||||
//basic use: wraps with `fn main`, adds `#![allow(unused)]`
|
//basic use: wraps with `fn main`, adds `#![allow(unused)]`
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("");
|
||||||
let input = "assert_eq!(2+2, 4);";
|
let input = "assert_eq!(2+2, 4);";
|
||||||
let expected = "#![allow(unused)]
|
let expected = "#![allow(unused)]
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -19,7 +32,7 @@ assert_eq!(2+2, 4);
|
|||||||
fn make_test_crate_name_no_use() {
|
fn make_test_crate_name_no_use() {
|
||||||
// If you give a crate name but *don't* use it within the test, it won't bother inserting
|
// If you give a crate name but *don't* use it within the test, it won't bother inserting
|
||||||
// the `extern crate` statement.
|
// the `extern crate` statement.
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("asdf");
|
||||||
let input = "assert_eq!(2+2, 4);";
|
let input = "assert_eq!(2+2, 4);";
|
||||||
let expected = "#![allow(unused)]
|
let expected = "#![allow(unused)]
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -34,7 +47,7 @@ assert_eq!(2+2, 4);
|
|||||||
fn make_test_crate_name() {
|
fn make_test_crate_name() {
|
||||||
// If you give a crate name and use it within the test, it will insert an `extern crate`
|
// If you give a crate name and use it within the test, it will insert an `extern crate`
|
||||||
// statement before `fn main`.
|
// statement before `fn main`.
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("asdf");
|
||||||
let input = "use asdf::qwop;
|
let input = "use asdf::qwop;
|
||||||
assert_eq!(2+2, 4);";
|
assert_eq!(2+2, 4);";
|
||||||
let expected = "#![allow(unused)]
|
let expected = "#![allow(unused)]
|
||||||
@ -53,8 +66,7 @@ assert_eq!(2+2, 4);
|
|||||||
fn make_test_no_crate_inject() {
|
fn make_test_no_crate_inject() {
|
||||||
// Even if you do use the crate within the test, setting `opts.no_crate_inject` will skip
|
// Even if you do use the crate within the test, setting `opts.no_crate_inject` will skip
|
||||||
// adding it anyway.
|
// adding it anyway.
|
||||||
let opts =
|
let opts = GlobalTestOptions { no_crate_inject: true, ..default_global_opts("asdf") };
|
||||||
GlobalTestOptions { no_crate_inject: true, attrs: vec![], insert_indent_space: false };
|
|
||||||
let input = "use asdf::qwop;
|
let input = "use asdf::qwop;
|
||||||
assert_eq!(2+2, 4);";
|
assert_eq!(2+2, 4);";
|
||||||
let expected = "#![allow(unused)]
|
let expected = "#![allow(unused)]
|
||||||
@ -72,7 +84,7 @@ fn make_test_ignore_std() {
|
|||||||
// Even if you include a crate name, and use it in the doctest, we still won't include an
|
// Even if you include a crate name, and use it in the doctest, we still won't include an
|
||||||
// `extern crate` statement if the crate is "std" -- that's included already by the
|
// `extern crate` statement if the crate is "std" -- that's included already by the
|
||||||
// compiler!
|
// compiler!
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("std");
|
||||||
let input = "use std::*;
|
let input = "use std::*;
|
||||||
assert_eq!(2+2, 4);";
|
assert_eq!(2+2, 4);";
|
||||||
let expected = "#![allow(unused)]
|
let expected = "#![allow(unused)]
|
||||||
@ -89,7 +101,7 @@ assert_eq!(2+2, 4);
|
|||||||
fn make_test_manual_extern_crate() {
|
fn make_test_manual_extern_crate() {
|
||||||
// When you manually include an `extern crate` statement in your doctest, `make_test`
|
// When you manually include an `extern crate` statement in your doctest, `make_test`
|
||||||
// assumes you've included one for your own crate too.
|
// assumes you've included one for your own crate too.
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("asdf");
|
||||||
let input = "extern crate asdf;
|
let input = "extern crate asdf;
|
||||||
use asdf::qwop;
|
use asdf::qwop;
|
||||||
assert_eq!(2+2, 4);";
|
assert_eq!(2+2, 4);";
|
||||||
@ -106,7 +118,7 @@ assert_eq!(2+2, 4);
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn make_test_manual_extern_crate_with_macro_use() {
|
fn make_test_manual_extern_crate_with_macro_use() {
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("asdf");
|
||||||
let input = "#[macro_use] extern crate asdf;
|
let input = "#[macro_use] extern crate asdf;
|
||||||
use asdf::qwop;
|
use asdf::qwop;
|
||||||
assert_eq!(2+2, 4);";
|
assert_eq!(2+2, 4);";
|
||||||
@ -125,7 +137,7 @@ assert_eq!(2+2, 4);
|
|||||||
fn make_test_opts_attrs() {
|
fn make_test_opts_attrs() {
|
||||||
// If you supplied some doctest attributes with `#![doc(test(attr(...)))]`, it will use
|
// If you supplied some doctest attributes with `#![doc(test(attr(...)))]`, it will use
|
||||||
// those instead of the stock `#![allow(unused)]`.
|
// those instead of the stock `#![allow(unused)]`.
|
||||||
let mut opts = GlobalTestOptions::default();
|
let mut opts = default_global_opts("asdf");
|
||||||
opts.attrs.push("feature(sick_rad)".to_string());
|
opts.attrs.push("feature(sick_rad)".to_string());
|
||||||
let input = "use asdf::qwop;
|
let input = "use asdf::qwop;
|
||||||
assert_eq!(2+2, 4);";
|
assert_eq!(2+2, 4);";
|
||||||
@ -159,7 +171,7 @@ assert_eq!(2+2, 4);
|
|||||||
fn make_test_crate_attrs() {
|
fn make_test_crate_attrs() {
|
||||||
// Including inner attributes in your doctest will apply them to the whole "crate", pasting
|
// Including inner attributes in your doctest will apply them to the whole "crate", pasting
|
||||||
// them outside the generated main function.
|
// them outside the generated main function.
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("");
|
||||||
let input = "#![feature(sick_rad)]
|
let input = "#![feature(sick_rad)]
|
||||||
assert_eq!(2+2, 4);";
|
assert_eq!(2+2, 4);";
|
||||||
let expected = "#![allow(unused)]
|
let expected = "#![allow(unused)]
|
||||||
@ -175,7 +187,7 @@ assert_eq!(2+2, 4);
|
|||||||
#[test]
|
#[test]
|
||||||
fn make_test_with_main() {
|
fn make_test_with_main() {
|
||||||
// Including your own `fn main` wrapper lets the test use it verbatim.
|
// Including your own `fn main` wrapper lets the test use it verbatim.
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("");
|
||||||
let input = "fn main() {
|
let input = "fn main() {
|
||||||
assert_eq!(2+2, 4);
|
assert_eq!(2+2, 4);
|
||||||
}";
|
}";
|
||||||
@ -191,7 +203,7 @@ fn main() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn make_test_fake_main() {
|
fn make_test_fake_main() {
|
||||||
// ... but putting it in a comment will still provide a wrapper.
|
// ... but putting it in a comment will still provide a wrapper.
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("");
|
||||||
let input = "//Ceci n'est pas une `fn main`
|
let input = "//Ceci n'est pas une `fn main`
|
||||||
assert_eq!(2+2, 4);";
|
assert_eq!(2+2, 4);";
|
||||||
let expected = "#![allow(unused)]
|
let expected = "#![allow(unused)]
|
||||||
@ -207,7 +219,7 @@ assert_eq!(2+2, 4);
|
|||||||
#[test]
|
#[test]
|
||||||
fn make_test_dont_insert_main() {
|
fn make_test_dont_insert_main() {
|
||||||
// Even with that, if you set `dont_insert_main`, it won't create the `fn main` wrapper.
|
// Even with that, if you set `dont_insert_main`, it won't create the `fn main` wrapper.
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("");
|
||||||
let input = "//Ceci n'est pas une `fn main`
|
let input = "//Ceci n'est pas une `fn main`
|
||||||
assert_eq!(2+2, 4);";
|
assert_eq!(2+2, 4);";
|
||||||
let expected = "#![allow(unused)]
|
let expected = "#![allow(unused)]
|
||||||
@ -219,8 +231,8 @@ assert_eq!(2+2, 4);"
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn make_test_issues_21299_33731() {
|
fn make_test_issues_21299() {
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("");
|
||||||
|
|
||||||
let input = "// fn main
|
let input = "// fn main
|
||||||
assert_eq!(2+2, 4);";
|
assert_eq!(2+2, 4);";
|
||||||
@ -234,6 +246,11 @@ assert_eq!(2+2, 4);
|
|||||||
|
|
||||||
let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
|
let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
|
||||||
assert_eq!((output, len), (expected, 2));
|
assert_eq!((output, len), (expected, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn make_test_issues_33731() {
|
||||||
|
let opts = default_global_opts("asdf");
|
||||||
|
|
||||||
let input = "extern crate hella_qwop;
|
let input = "extern crate hella_qwop;
|
||||||
assert_eq!(asdf::foo, 4);";
|
assert_eq!(asdf::foo, 4);";
|
||||||
@ -253,7 +270,7 @@ assert_eq!(asdf::foo, 4);
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn make_test_main_in_macro() {
|
fn make_test_main_in_macro() {
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("my_crate");
|
||||||
let input = "#[macro_use] extern crate my_crate;
|
let input = "#[macro_use] extern crate my_crate;
|
||||||
test_wrapper! {
|
test_wrapper! {
|
||||||
fn main() {}
|
fn main() {}
|
||||||
@ -272,7 +289,7 @@ test_wrapper! {
|
|||||||
#[test]
|
#[test]
|
||||||
fn make_test_returns_result() {
|
fn make_test_returns_result() {
|
||||||
// creates an inner function and unwraps it
|
// creates an inner function and unwraps it
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("");
|
||||||
let input = "use std::io;
|
let input = "use std::io;
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
io::stdin().read_line(&mut input)?;
|
io::stdin().read_line(&mut input)?;
|
||||||
@ -292,7 +309,7 @@ Ok::<(), io:Error>(())
|
|||||||
#[test]
|
#[test]
|
||||||
fn make_test_named_wrapper() {
|
fn make_test_named_wrapper() {
|
||||||
// creates an inner function with a specific name
|
// creates an inner function with a specific name
|
||||||
let opts = GlobalTestOptions::default();
|
let opts = default_global_opts("");
|
||||||
let input = "assert_eq!(2+2, 4);";
|
let input = "assert_eq!(2+2, 4);";
|
||||||
let expected = "#![allow(unused)]
|
let expected = "#![allow(unused)]
|
||||||
fn main() { #[allow(non_snake_case)] fn _doctest_main__some_unique_name() {
|
fn main() { #[allow(non_snake_case)] fn _doctest_main__some_unique_name() {
|
||||||
@ -307,8 +324,7 @@ assert_eq!(2+2, 4);
|
|||||||
#[test]
|
#[test]
|
||||||
fn make_test_insert_extra_space() {
|
fn make_test_insert_extra_space() {
|
||||||
// will insert indent spaces in the code block if `insert_indent_space` is true
|
// will insert indent spaces in the code block if `insert_indent_space` is true
|
||||||
let opts =
|
let opts = GlobalTestOptions { insert_indent_space: true, ..default_global_opts("") };
|
||||||
GlobalTestOptions { no_crate_inject: false, attrs: vec![], insert_indent_space: true };
|
|
||||||
let input = "use std::*;
|
let input = "use std::*;
|
||||||
assert_eq!(2+2, 4);
|
assert_eq!(2+2, 4);
|
||||||
eprintln!(\"hello anan\");
|
eprintln!(\"hello anan\");
|
||||||
@ -327,8 +343,7 @@ fn main() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn make_test_insert_extra_space_fn_main() {
|
fn make_test_insert_extra_space_fn_main() {
|
||||||
// if input already has a fn main, it should insert a space before it
|
// if input already has a fn main, it should insert a space before it
|
||||||
let opts =
|
let opts = GlobalTestOptions { insert_indent_space: true, ..default_global_opts("") };
|
||||||
GlobalTestOptions { no_crate_inject: false, attrs: vec![], insert_indent_space: true };
|
|
||||||
let input = "use std::*;
|
let input = "use std::*;
|
||||||
fn main() {
|
fn main() {
|
||||||
assert_eq!(2+2, 4);
|
assert_eq!(2+2, 4);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user