Separate librustcdoc module

This commit is contained in:
chansuke 2019-06-06 03:33:39 +09:00 committed by Vadim Petrochenkov
parent 638f63b6e4
commit 46e622beb9
6 changed files with 507 additions and 510 deletions

View File

@ -1050,130 +1050,4 @@ fn test_unique_id() {
}
#[cfg(test)]
mod tests {
use super::{ErrorCodes, LangString, Markdown, MarkdownHtml, IdMap};
use super::plain_summary_line;
use std::cell::RefCell;
use syntax::edition::{Edition, DEFAULT_EDITION};
#[test]
fn test_lang_string_parse() {
fn t(s: &str,
should_panic: bool, no_run: bool, ignore: bool, rust: bool, test_harness: bool,
compile_fail: bool, allow_fail: bool, error_codes: Vec<String>,
edition: Option<Edition>) {
assert_eq!(LangString::parse(s, ErrorCodes::Yes), LangString {
should_panic,
no_run,
ignore,
rust,
test_harness,
compile_fail,
error_codes,
original: s.to_owned(),
allow_fail,
edition,
})
}
fn v() -> Vec<String> {
Vec::new()
}
// ignore-tidy-linelength
// marker | should_panic | no_run | ignore | rust | test_harness
// | compile_fail | allow_fail | error_codes | edition
t("", false, false, false, true, false, false, false, v(), None);
t("rust", false, false, false, true, false, false, false, v(), None);
t("sh", false, false, false, false, false, false, false, v(), None);
t("ignore", false, false, true, true, false, false, false, v(), None);
t("should_panic", true, false, false, true, false, false, false, v(), None);
t("no_run", false, true, false, true, false, false, false, v(), None);
t("test_harness", false, false, false, true, true, false, false, v(), None);
t("compile_fail", false, true, false, true, false, true, false, v(), None);
t("allow_fail", false, false, false, true, false, false, true, v(), None);
t("{.no_run .example}", false, true, false, true, false, false, false, v(), None);
t("{.sh .should_panic}", true, false, false, false, false, false, false, v(), None);
t("{.example .rust}", false, false, false, true, false, false, false, v(), None);
t("{.test_harness .rust}", false, false, false, true, true, false, false, v(), None);
t("text, no_run", false, true, false, false, false, false, false, v(), None);
t("text,no_run", false, true, false, false, false, false, false, v(), None);
t("edition2015", false, false, false, true, false, false, false, v(), Some(Edition::Edition2015));
t("edition2018", false, false, false, true, false, false, false, v(), Some(Edition::Edition2018));
}
#[test]
fn test_header() {
fn t(input: &str, expect: &str) {
let mut map = IdMap::new();
let output = Markdown(input, &[], RefCell::new(&mut map),
ErrorCodes::Yes, DEFAULT_EDITION).to_string();
assert_eq!(output, expect, "original: {}", input);
}
t("# Foo bar", "<h1 id=\"foo-bar\" class=\"section-header\">\
<a href=\"#foo-bar\">Foo bar</a></h1>");
t("## Foo-bar_baz qux", "<h2 id=\"foo-bar_baz-qux\" class=\"section-\
header\"><a href=\"#foo-bar_baz-qux\">Foo-bar_baz qux</a></h2>");
t("### **Foo** *bar* baz!?!& -_qux_-%",
"<h3 id=\"foo-bar-baz--qux-\" class=\"section-header\">\
<a href=\"#foo-bar-baz--qux-\"><strong>Foo</strong> \
<em>bar</em> baz!?!&amp; -<em>qux</em>-%</a></h3>");
t("#### **Foo?** & \\*bar?!* _`baz`_ ❤ #qux",
"<h4 id=\"foo--bar--baz--qux\" class=\"section-header\">\
<a href=\"#foo--bar--baz--qux\"><strong>Foo?</strong> &amp; *bar?!* \
<em><code>baz</code></em> #qux</a></h4>");
}
#[test]
fn test_header_ids_multiple_blocks() {
let mut map = IdMap::new();
fn t(map: &mut IdMap, input: &str, expect: &str) {
let output = Markdown(input, &[], RefCell::new(map),
ErrorCodes::Yes, DEFAULT_EDITION).to_string();
assert_eq!(output, expect, "original: {}", input);
}
t(&mut map, "# Example", "<h1 id=\"example\" class=\"section-header\">\
<a href=\"#example\">Example</a></h1>");
t(&mut map, "# Panics", "<h1 id=\"panics\" class=\"section-header\">\
<a href=\"#panics\">Panics</a></h1>");
t(&mut map, "# Example", "<h1 id=\"example-1\" class=\"section-header\">\
<a href=\"#example-1\">Example</a></h1>");
t(&mut map, "# Main", "<h1 id=\"main\" class=\"section-header\">\
<a href=\"#main\">Main</a></h1>");
t(&mut map, "# Example", "<h1 id=\"example-2\" class=\"section-header\">\
<a href=\"#example-2\">Example</a></h1>");
t(&mut map, "# Panics", "<h1 id=\"panics-1\" class=\"section-header\">\
<a href=\"#panics-1\">Panics</a></h1>");
}
#[test]
fn test_plain_summary_line() {
fn t(input: &str, expect: &str) {
let output = plain_summary_line(input);
assert_eq!(output, expect, "original: {}", input);
}
t("hello [Rust](https://www.rust-lang.org) :)", "hello Rust :)");
t("hello [Rust](https://www.rust-lang.org \"Rust\") :)", "hello Rust :)");
t("code `let x = i32;` ...", "code `let x = i32;` ...");
t("type `Type<'static>` ...", "type `Type<'static>` ...");
t("# top header", "top header");
t("## header", "header");
}
#[test]
fn test_markdown_html_escape() {
fn t(input: &str, expect: &str) {
let mut idmap = IdMap::new();
let output = MarkdownHtml(input, RefCell::new(&mut idmap),
ErrorCodes::Yes, DEFAULT_EDITION).to_string();
assert_eq!(output, expect, "original: {}", input);
}
t("`Struct<'a, T>`", "<p><code>Struct&lt;'a, T&gt;</code></p>\n");
t("Struct<'a, T>", "<p>Struct&lt;'a, T&gt;</p>\n");
t("Struct<br>", "<p>Struct&lt;br&gt;</p>\n");
}
}
mod tests;

View File

@ -0,0 +1,125 @@
use super::{ErrorCodes, LangString, Markdown, MarkdownHtml, IdMap};
use super::plain_summary_line;
use std::cell::RefCell;
use syntax::edition::{Edition, DEFAULT_EDITION};
#[test]
fn test_lang_string_parse() {
fn t(s: &str,
should_panic: bool, no_run: bool, ignore: bool, rust: bool, test_harness: bool,
compile_fail: bool, allow_fail: bool, error_codes: Vec<String>,
edition: Option<Edition>) {
assert_eq!(LangString::parse(s, ErrorCodes::Yes), LangString {
should_panic,
no_run,
ignore,
rust,
test_harness,
compile_fail,
error_codes,
original: s.to_owned(),
allow_fail,
edition,
})
}
fn v() -> Vec<String> {
Vec::new()
}
// ignore-tidy-linelength
// marker | should_panic | no_run | ignore | rust | test_harness
// | compile_fail | allow_fail | error_codes | edition
t("", false, false, false, true, false, false, false, v(), None);
t("rust", false, false, false, true, false, false, false, v(), None);
t("sh", false, false, false, false, false, false, false, v(), None);
t("ignore", false, false, true, true, false, false, false, v(), None);
t("should_panic", true, false, false, true, false, false, false, v(), None);
t("no_run", false, true, false, true, false, false, false, v(), None);
t("test_harness", false, false, false, true, true, false, false, v(), None);
t("compile_fail", false, true, false, true, false, true, false, v(), None);
t("allow_fail", false, false, false, true, false, false, true, v(), None);
t("{.no_run .example}", false, true, false, true, false, false, false, v(), None);
t("{.sh .should_panic}", true, false, false, false, false, false, false, v(), None);
t("{.example .rust}", false, false, false, true, false, false, false, v(), None);
t("{.test_harness .rust}", false, false, false, true, true, false, false, v(), None);
t("text, no_run", false, true, false, false, false, false, false, v(), None);
t("text,no_run", false, true, false, false, false, false, false, v(), None);
t("edition2015", false, false, false, true, false, false, false, v(), Some(Edition::Edition2015));
t("edition2018", false, false, false, true, false, false, false, v(), Some(Edition::Edition2018));
}
#[test]
fn test_header() {
fn t(input: &str, expect: &str) {
let mut map = IdMap::new();
let output = Markdown(input, &[], RefCell::new(&mut map),
ErrorCodes::Yes, DEFAULT_EDITION).to_string();
assert_eq!(output, expect, "original: {}", input);
}
t("# Foo bar", "<h1 id=\"foo-bar\" class=\"section-header\">\
<a href=\"#foo-bar\">Foo bar</a></h1>");
t("## Foo-bar_baz qux", "<h2 id=\"foo-bar_baz-qux\" class=\"section-\
header\"><a href=\"#foo-bar_baz-qux\">Foo-bar_baz qux</a></h2>");
t("### **Foo** *bar* baz!?!& -_qux_-%",
"<h3 id=\"foo-bar-baz--qux-\" class=\"section-header\">\
<a href=\"#foo-bar-baz--qux-\"><strong>Foo</strong> \
<em>bar</em> baz!?!&amp; -<em>qux</em>-%</a></h3>");
t("#### **Foo?** & \\*bar?!* _`baz`_ ❤ #qux",
"<h4 id=\"foo--bar--baz--qux\" class=\"section-header\">\
<a href=\"#foo--bar--baz--qux\"><strong>Foo?</strong> &amp; *bar?!* \
<em><code>baz</code></em> #qux</a></h4>");
}
#[test]
fn test_header_ids_multiple_blocks() {
let mut map = IdMap::new();
fn t(map: &mut IdMap, input: &str, expect: &str) {
let output = Markdown(input, &[], RefCell::new(map),
ErrorCodes::Yes, DEFAULT_EDITION).to_string();
assert_eq!(output, expect, "original: {}", input);
}
t(&mut map, "# Example", "<h1 id=\"example\" class=\"section-header\">\
<a href=\"#example\">Example</a></h1>");
t(&mut map, "# Panics", "<h1 id=\"panics\" class=\"section-header\">\
<a href=\"#panics\">Panics</a></h1>");
t(&mut map, "# Example", "<h1 id=\"example-1\" class=\"section-header\">\
<a href=\"#example-1\">Example</a></h1>");
t(&mut map, "# Main", "<h1 id=\"main\" class=\"section-header\">\
<a href=\"#main\">Main</a></h1>");
t(&mut map, "# Example", "<h1 id=\"example-2\" class=\"section-header\">\
<a href=\"#example-2\">Example</a></h1>");
t(&mut map, "# Panics", "<h1 id=\"panics-1\" class=\"section-header\">\
<a href=\"#panics-1\">Panics</a></h1>");
}
#[test]
fn test_plain_summary_line() {
fn t(input: &str, expect: &str) {
let output = plain_summary_line(input);
assert_eq!(output, expect, "original: {}", input);
}
t("hello [Rust](https://www.rust-lang.org) :)", "hello Rust :)");
t("hello [Rust](https://www.rust-lang.org \"Rust\") :)", "hello Rust :)");
t("code `let x = i32;` ...", "code `let x = i32;` ...");
t("type `Type<'static>` ...", "type `Type<'static>` ...");
t("# top header", "top header");
t("## header", "header");
}
#[test]
fn test_markdown_html_escape() {
fn t(input: &str, expect: &str) {
let mut idmap = IdMap::new();
let output = MarkdownHtml(input, RefCell::new(&mut idmap),
ErrorCodes::Yes, DEFAULT_EDITION).to_string();
assert_eq!(output, expect, "original: {}", input);
}
t("`Struct<'a, T>`", "<p><code>Struct&lt;'a, T&gt;</code></p>\n");
t("Struct<'a, T>", "<p>Struct&lt;'a, T&gt;</p>\n");
t("Struct<br>", "<p>Struct&lt;br&gt;</p>\n");
}

View File

@ -188,85 +188,4 @@ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
}
#[cfg(test)]
mod tests {
use super::{TocBuilder, Toc, TocEntry};
#[test]
fn builder_smoke() {
let mut builder = TocBuilder::new();
// this is purposely not using a fancy macro like below so
// that we're sure that this is doing the correct thing, and
// there's been no macro mistake.
macro_rules! push {
($level: expr, $name: expr) => {
assert_eq!(builder.push($level,
$name.to_string(),
"".to_string()),
$name);
}
}
push!(2, "0.1");
push!(1, "1");
{
push!(2, "1.1");
{
push!(3, "1.1.1");
push!(3, "1.1.2");
}
push!(2, "1.2");
{
push!(3, "1.2.1");
push!(3, "1.2.2");
}
}
push!(1, "2");
push!(1, "3");
{
push!(4, "3.0.0.1");
{
push!(6, "3.0.0.1.0.1");
}
push!(4, "3.0.0.2");
push!(2, "3.1");
{
push!(4, "3.1.0.1");
}
}
macro_rules! toc {
($(($level: expr, $name: expr, $(($sub: tt))* )),*) => {
Toc {
entries: vec![
$(
TocEntry {
level: $level,
name: $name.to_string(),
sec_number: $name.to_string(),
id: "".to_string(),
children: toc!($($sub),*)
}
),*
]
}
}
}
let expected = toc!(
(2, "0.1", ),
(1, "1",
((2, "1.1", ((3, "1.1.1", )) ((3, "1.1.2", ))))
((2, "1.2", ((3, "1.2.1", )) ((3, "1.2.2", ))))
),
(1, "2", ),
(1, "3",
((4, "3.0.0.1", ((6, "3.0.0.1.0.1", ))))
((4, "3.0.0.2", ))
((2, "3.1", ((4, "3.1.0.1", ))))
)
);
assert_eq!(expected, builder.into_toc());
}
}
mod tests;

View File

@ -0,0 +1,80 @@
use super::{TocBuilder, Toc, TocEntry};
#[test]
fn builder_smoke() {
let mut builder = TocBuilder::new();
// this is purposely not using a fancy macro like below so
// that we're sure that this is doing the correct thing, and
// there's been no macro mistake.
macro_rules! push {
($level: expr, $name: expr) => {
assert_eq!(builder.push($level,
$name.to_string(),
"".to_string()),
$name);
}
}
push!(2, "0.1");
push!(1, "1");
{
push!(2, "1.1");
{
push!(3, "1.1.1");
push!(3, "1.1.2");
}
push!(2, "1.2");
{
push!(3, "1.2.1");
push!(3, "1.2.2");
}
}
push!(1, "2");
push!(1, "3");
{
push!(4, "3.0.0.1");
{
push!(6, "3.0.0.1.0.1");
}
push!(4, "3.0.0.2");
push!(2, "3.1");
{
push!(4, "3.1.0.1");
}
}
macro_rules! toc {
($(($level: expr, $name: expr, $(($sub: tt))* )),*) => {
Toc {
entries: vec![
$(
TocEntry {
level: $level,
name: $name.to_string(),
sec_number: $name.to_string(),
id: "".to_string(),
children: toc!($($sub),*)
}
),*
]
}
}
}
let expected = toc!(
(2, "0.1", ),
(1, "1",
((2, "1.1", ((3, "1.1.1", )) ((3, "1.1.2", ))))
((2, "1.2", ((3, "1.2.1", )) ((3, "1.2.2", ))))
),
(1, "2", ),
(1, "3",
((4, "3.0.0.1", ((6, "3.0.0.1.0.1", ))))
((4, "3.0.0.2", ))
((2, "3.1", ((4, "3.1.0.1", ))))
)
);
assert_eq!(expected, builder.into_toc());
}

View File

@ -975,304 +975,4 @@ fn visit_macro_def(&mut self, macro_def: &'hir hir::MacroDef) {
}
#[cfg(test)]
mod tests {
use super::{TestOptions, make_test};
use syntax::edition::DEFAULT_EDITION;
#[test]
fn make_test_basic() {
//basic use: wraps with `fn main`, adds `#![allow(unused)]`
let opts = TestOptions::default();
let input =
"assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, None, false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
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 = TestOptions::default();
let input =
"assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
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 = TestOptions::default();
let input =
"use asdf::qwop;
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
extern crate asdf;
fn main() {
use asdf::qwop;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 3));
}
#[test]
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 = TestOptions {
no_crate_inject: true,
display_warnings: false,
attrs: vec![],
};
let input =
"use asdf::qwop;
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
fn main() {
use asdf::qwop;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
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 = TestOptions::default();
let input =
"use std::*;
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
fn main() {
use std::*;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("std"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
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 = TestOptions::default();
let input =
"extern crate asdf;
use asdf::qwop;
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
extern crate asdf;
fn main() {
use asdf::qwop;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
fn make_test_manual_extern_crate_with_macro_use() {
let opts = TestOptions::default();
let input =
"#[macro_use] extern crate asdf;
use asdf::qwop;
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
#[macro_use] extern crate asdf;
fn main() {
use asdf::qwop;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
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 = TestOptions::default();
opts.attrs.push("feature(sick_rad)".to_string());
let input =
"use asdf::qwop;
assert_eq!(2+2, 4);";
let expected =
"#![feature(sick_rad)]
extern crate asdf;
fn main() {
use asdf::qwop;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 3));
// Adding more will also bump the returned line offset.
opts.attrs.push("feature(hella_dope)".to_string());
let expected =
"#![feature(sick_rad)]
#![feature(hella_dope)]
extern crate asdf;
fn main() {
use asdf::qwop;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 4));
}
#[test]
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 = TestOptions::default();
let input =
"#![feature(sick_rad)]
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
#![feature(sick_rad)]
fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, None, false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
fn make_test_with_main() {
// Including your own `fn main` wrapper lets the test use it verbatim.
let opts = TestOptions::default();
let input =
"fn main() {
assert_eq!(2+2, 4);
}";
let expected =
"#![allow(unused)]
fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, None, false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 1));
}
#[test]
fn make_test_fake_main() {
// ... but putting it in a comment will still provide a wrapper.
let opts = TestOptions::default();
let input =
"//Ceci n'est pas une `fn main`
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
//Ceci n'est pas une `fn main`
fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, None, false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[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 = TestOptions::default();
let input =
"//Ceci n'est pas une `fn main`
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
//Ceci n'est pas une `fn main`
assert_eq!(2+2, 4);".to_string();
let output = make_test(input, None, true, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 1));
}
#[test]
fn make_test_display_warnings() {
// If the user is asking to display doctest warnings, suppress the default `allow(unused)`.
let mut opts = TestOptions::default();
opts.display_warnings = true;
let input =
"assert_eq!(2+2, 4);";
let expected =
"fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, None, false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 1));
}
#[test]
fn make_test_issues_21299_33731() {
let opts = TestOptions::default();
let input =
"// fn main
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
// fn main
fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, None, false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
let input =
"extern crate hella_qwop;
assert_eq!(asdf::foo, 4);";
let expected =
"#![allow(unused)]
extern crate hella_qwop;
extern crate asdf;
fn main() {
assert_eq!(asdf::foo, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 3));
}
#[test]
fn make_test_main_in_macro() {
let opts = TestOptions::default();
let input =
"#[macro_use] extern crate my_crate;
test_wrapper! {
fn main() {}
}";
let expected =
"#![allow(unused)]
#[macro_use] extern crate my_crate;
test_wrapper! {
fn main() {}
}".to_string();
let output = make_test(input, Some("my_crate"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 1));
}
}
mod tests;

View File

@ -0,0 +1,299 @@
use super::{TestOptions, make_test};
use syntax::edition::DEFAULT_EDITION;
#[test]
fn make_test_basic() {
//basic use: wraps with `fn main`, adds `#![allow(unused)]`
let opts = TestOptions::default();
let input =
"assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, None, false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
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 = TestOptions::default();
let input =
"assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
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 = TestOptions::default();
let input =
"use asdf::qwop;
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
extern crate asdf;
fn main() {
use asdf::qwop;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 3));
}
#[test]
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 = TestOptions {
no_crate_inject: true,
display_warnings: false,
attrs: vec![],
};
let input =
"use asdf::qwop;
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
fn main() {
use asdf::qwop;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
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 = TestOptions::default();
let input =
"use std::*;
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
fn main() {
use std::*;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("std"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
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 = TestOptions::default();
let input =
"extern crate asdf;
use asdf::qwop;
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
extern crate asdf;
fn main() {
use asdf::qwop;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
fn make_test_manual_extern_crate_with_macro_use() {
let opts = TestOptions::default();
let input =
"#[macro_use] extern crate asdf;
use asdf::qwop;
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
#[macro_use] extern crate asdf;
fn main() {
use asdf::qwop;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
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 = TestOptions::default();
opts.attrs.push("feature(sick_rad)".to_string());
let input =
"use asdf::qwop;
assert_eq!(2+2, 4);";
let expected =
"#![feature(sick_rad)]
extern crate asdf;
fn main() {
use asdf::qwop;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 3));
// Adding more will also bump the returned line offset.
opts.attrs.push("feature(hella_dope)".to_string());
let expected =
"#![feature(sick_rad)]
#![feature(hella_dope)]
extern crate asdf;
fn main() {
use asdf::qwop;
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 4));
}
#[test]
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 = TestOptions::default();
let input =
"#![feature(sick_rad)]
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
#![feature(sick_rad)]
fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, None, false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[test]
fn make_test_with_main() {
// Including your own `fn main` wrapper lets the test use it verbatim.
let opts = TestOptions::default();
let input =
"fn main() {
assert_eq!(2+2, 4);
}";
let expected =
"#![allow(unused)]
fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, None, false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 1));
}
#[test]
fn make_test_fake_main() {
// ... but putting it in a comment will still provide a wrapper.
let opts = TestOptions::default();
let input =
"//Ceci n'est pas une `fn main`
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
//Ceci n'est pas une `fn main`
fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, None, false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
}
#[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 = TestOptions::default();
let input =
"//Ceci n'est pas une `fn main`
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
//Ceci n'est pas une `fn main`
assert_eq!(2+2, 4);".to_string();
let output = make_test(input, None, true, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 1));
}
#[test]
fn make_test_display_warnings() {
// If the user is asking to display doctest warnings, suppress the default `allow(unused)`.
let mut opts = TestOptions::default();
opts.display_warnings = true;
let input =
"assert_eq!(2+2, 4);";
let expected =
"fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, None, false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 1));
}
#[test]
fn make_test_issues_21299_33731() {
let opts = TestOptions::default();
let input =
"// fn main
assert_eq!(2+2, 4);";
let expected =
"#![allow(unused)]
// fn main
fn main() {
assert_eq!(2+2, 4);
}".to_string();
let output = make_test(input, None, false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 2));
let input =
"extern crate hella_qwop;
assert_eq!(asdf::foo, 4);";
let expected =
"#![allow(unused)]
extern crate hella_qwop;
extern crate asdf;
fn main() {
assert_eq!(asdf::foo, 4);
}".to_string();
let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 3));
}
#[test]
fn make_test_main_in_macro() {
let opts = TestOptions::default();
let input =
"#[macro_use] extern crate my_crate;
test_wrapper! {
fn main() {}
}";
let expected =
"#![allow(unused)]
#[macro_use] extern crate my_crate;
test_wrapper! {
fn main() {}
}".to_string();
let output = make_test(input, Some("my_crate"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 1));
}