Skip to content

Commit

Permalink
Fix broken rustdoc unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
camelid authored and GuillaumeGomez committed Jun 7, 2024
1 parent 815c447 commit 3670ad5
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions src/librustdoc/doctest/tests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
use std::path::PathBuf;

use super::{make_test, GlobalTestOptions};
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]
fn make_test_basic() {
//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 expected = "#![allow(unused)]
fn main() {
Expand All @@ -19,7 +32,7 @@ assert_eq!(2+2, 4);
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
// the `extern crate` statement.
let opts = GlobalTestOptions::default();
let opts = default_global_opts("asdf");
let input = "assert_eq!(2+2, 4);";
let expected = "#![allow(unused)]
fn main() {
Expand All @@ -34,7 +47,7 @@ assert_eq!(2+2, 4);
fn make_test_crate_name() {
// If you give a crate name and use it within the test, it will insert an `extern crate`
// statement before `fn main`.
let opts = GlobalTestOptions::default();
let opts = default_global_opts("asdf");
let input = "use asdf::qwop;
assert_eq!(2+2, 4);";
let expected = "#![allow(unused)]
Expand All @@ -53,8 +66,7 @@ assert_eq!(2+2, 4);
fn make_test_no_crate_inject() {
// Even if you do use the crate within the test, setting `opts.no_crate_inject` will skip
// adding it anyway.
let opts =
GlobalTestOptions { no_crate_inject: true, attrs: vec![], insert_indent_space: false };
let opts = GlobalTestOptions { no_crate_inject: true, ..default_global_opts("asdf") };
let input = "use asdf::qwop;
assert_eq!(2+2, 4);";
let expected = "#![allow(unused)]
Expand All @@ -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
// `extern crate` statement if the crate is "std" -- that's included already by the
// compiler!
let opts = GlobalTestOptions::default();
let opts = default_global_opts("std");
let input = "use std::*;
assert_eq!(2+2, 4);";
let expected = "#![allow(unused)]
Expand All @@ -89,7 +101,7 @@ assert_eq!(2+2, 4);
fn make_test_manual_extern_crate() {
// When you manually include an `extern crate` statement in your doctest, `make_test`
// 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;
use asdf::qwop;
assert_eq!(2+2, 4);";
Expand All @@ -106,7 +118,7 @@ assert_eq!(2+2, 4);

#[test]
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;
use asdf::qwop;
assert_eq!(2+2, 4);";
Expand All @@ -125,7 +137,7 @@ assert_eq!(2+2, 4);
fn make_test_opts_attrs() {
// If you supplied some doctest attributes with `#![doc(test(attr(...)))]`, it will use
// 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());
let input = "use asdf::qwop;
assert_eq!(2+2, 4);";
Expand Down Expand Up @@ -159,7 +171,7 @@ assert_eq!(2+2, 4);
fn make_test_crate_attrs() {
// Including inner attributes in your doctest will apply them to the whole "crate", pasting
// them outside the generated main function.
let opts = GlobalTestOptions::default();
let opts = default_global_opts("");
let input = "#![feature(sick_rad)]
assert_eq!(2+2, 4);";
let expected = "#![allow(unused)]
Expand All @@ -175,7 +187,7 @@ assert_eq!(2+2, 4);
#[test]
fn make_test_with_main() {
// 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() {
assert_eq!(2+2, 4);
}";
Expand All @@ -191,7 +203,7 @@ fn main() {
#[test]
fn make_test_fake_main() {
// ... 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`
assert_eq!(2+2, 4);";
let expected = "#![allow(unused)]
Expand All @@ -207,7 +219,7 @@ assert_eq!(2+2, 4);
#[test]
fn make_test_dont_insert_main() {
// 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`
assert_eq!(2+2, 4);";
let expected = "#![allow(unused)]
Expand All @@ -219,8 +231,8 @@ assert_eq!(2+2, 4);"
}

#[test]
fn make_test_issues_21299_33731() {
let opts = GlobalTestOptions::default();
fn make_test_issues_21299() {
let opts = default_global_opts("");

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

let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
assert_eq!((output, len), (expected, 2));
}

#[test]
fn make_test_issues_33731() {
let opts = default_global_opts("asdf");

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

#[test]
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;
test_wrapper! {
fn main() {}
Expand All @@ -272,7 +289,7 @@ test_wrapper! {
#[test]
fn make_test_returns_result() {
// creates an inner function and unwraps it
let opts = GlobalTestOptions::default();
let opts = default_global_opts("");
let input = "use std::io;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
Expand All @@ -292,7 +309,7 @@ Ok::<(), io:Error>(())
#[test]
fn make_test_named_wrapper() {
// 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 expected = "#![allow(unused)]
fn main() { #[allow(non_snake_case)] fn _doctest_main__some_unique_name() {
Expand All @@ -307,8 +324,7 @@ assert_eq!(2+2, 4);
#[test]
fn make_test_insert_extra_space() {
// will insert indent spaces in the code block if `insert_indent_space` is true
let opts =
GlobalTestOptions { no_crate_inject: false, attrs: vec![], insert_indent_space: true };
let opts = GlobalTestOptions { insert_indent_space: true, ..default_global_opts("") };
let input = "use std::*;
assert_eq!(2+2, 4);
eprintln!(\"hello anan\");
Expand All @@ -327,8 +343,7 @@ fn main() {
#[test]
fn make_test_insert_extra_space_fn_main() {
// if input already has a fn main, it should insert a space before it
let opts =
GlobalTestOptions { no_crate_inject: false, attrs: vec![], insert_indent_space: true };
let opts = GlobalTestOptions { insert_indent_space: true, ..default_global_opts("") };
let input = "use std::*;
fn main() {
assert_eq!(2+2, 4);
Expand Down

0 comments on commit 3670ad5

Please sign in to comment.