Skip to content

Commit

Permalink
Add main() so that examples work
Browse files Browse the repository at this point in the history
Rustdoc will automatically wrap things in main, but this doesn't work
here.

Fixes #31249
  • Loading branch information
steveklabnik committed Jan 29, 2016
1 parent 552bf75 commit 919ea47
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/doc/book/testing.md
Expand Up @@ -24,6 +24,7 @@ Cargo will automatically generate a simple test when you make a new project.
Here's the contents of `src/lib.rs`:

```rust
# fn main() {}
#[test]
fn it_works() {
}
Expand Down Expand Up @@ -75,6 +76,7 @@ So why does our do-nothing test pass? Any test which doesn't `panic!` passes,
and any test that does `panic!` fails. Let's make our test fail:

```rust
# fn main() {}
#[test]
fn it_works() {
assert!(false);
Expand Down Expand Up @@ -145,6 +147,7 @@ This is useful if you want to integrate `cargo test` into other tooling.
We can invert our test's failure with another attribute: `should_panic`:

```rust
# fn main() {}
#[test]
#[should_panic]
fn it_works() {
Expand Down Expand Up @@ -175,6 +178,7 @@ Rust provides another macro, `assert_eq!`, that compares two arguments for
equality:

```rust
# fn main() {}
#[test]
#[should_panic]
fn it_works() {
Expand Down Expand Up @@ -209,6 +213,7 @@ make sure that the failure message contains the provided text. A safer version
of the example above would be:

```rust
# fn main() {}
#[test]
#[should_panic(expected = "assertion failed")]
fn it_works() {
Expand All @@ -219,6 +224,7 @@ fn it_works() {
That's all there is to the basics! Let's write one 'real' test:

```rust,ignore
# fn main() {}
pub fn add_two(a: i32) -> i32 {
a + 2
}
Expand All @@ -238,6 +244,7 @@ Sometimes a few specific tests can be very time-consuming to execute. These
can be disabled by default by using the `ignore` attribute:

```rust
# fn main() {}
#[test]
fn it_works() {
assert_eq!(4, add_two(2));
Expand Down Expand Up @@ -299,6 +306,7 @@ missing the `tests` module. The idiomatic way of writing our example
looks like this:

```rust,ignore
# fn main() {}
pub fn add_two(a: i32) -> i32 {
a + 2
}
Expand Down Expand Up @@ -327,6 +335,7 @@ a large module, and so this is a common use of globs. Let's change our
`src/lib.rs` to make use of it:

```rust,ignore
# fn main() {}
pub fn add_two(a: i32) -> i32 {
a + 2
}
Expand Down Expand Up @@ -377,6 +386,7 @@ put a `tests/lib.rs` file inside, with this as its contents:
```rust,ignore
extern crate adder;
# fn main() {}
#[test]
fn it_works() {
assert_eq!(4, adder::add_two(2));
Expand Down Expand Up @@ -432,6 +442,7 @@ running examples in your documentation (**note:** this only works in library
crates, not binary crates). Here's a fleshed-out `src/lib.rs` with examples:

```rust,ignore
# fn main() {}
//! The `adder` crate provides functions that add numbers to other numbers.
//!
//! # Examples
Expand Down

0 comments on commit 919ea47

Please sign in to comment.