Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How use extern crate in a doctest? #960

Closed
FranklinChen opened this issue Nov 22, 2014 · 7 comments
Closed

How use extern crate in a doctest? #960

FranklinChen opened this issue Nov 22, 2014 · 7 comments

Comments

@FranklinChen
Copy link

I tried the following in a doctest and it failed. I do not know a workaround.

/// ```rust{.example}
/// extern crate core;
/// // stuff needing core...
/// ```
@steveklabnik
Copy link
Member

I think Rustdoc doesn't allow this :/

@alexcrichton
Copy link
Member

Can you paste the full error log from what you were trying to do? Rustdoc tests definitely allow for extern crate directives.

@FranklinChen
Copy link
Author

$ cargo test
   Doc-tests tau

running 1 test
test TAU_0 ... FAILED

failures:

---- TAU_0 stdout ----
    <anon>:5:5: 5:23 error: "extern crate" declarations are not allowed here
    <anon>:5     extern crate core;
                 ^~~~~~~~~~~~~~~~~~
    error: aborting due to previous error
    task 'TAU_0' panicked at 'Box<Any>', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/libsyntax/diagnostic.rs:142

@alexcrichton
Copy link
Member

Ah that's just part of how rustdoc wraps all tests in a fn main wrapper. You can get around that like so:

# extern crate core;
# fn main() {
// code to test
# }

@FranklinChen
Copy link
Author

But I want to have multiple doctest examples in multiple places. It would be nice to be able to specify a "prelude" of declarations for use in multiple doctests. In any case, it would be nice to have an explicit semantics of how doctest is supposed to work. I confess that all I could do was look at random Rust source code and http://doc.rust-lang.org/rustdoc.html#testing-the-documentation and guess what to do. I really like the doctest concept.

@alexcrichton
Copy link
Member

Yes unfortunately you'll have to duplicate the logic across doc tests, and we also do not have a formal specification for this as-is, but you can see the function here

@felixc
Copy link
Contributor

felixc commented Mar 1, 2015

Related to the request to specify a "prelude": rust-lang/rfcs#919

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants