Skip to content

Add main() so that examples work #31296

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

Merged
merged 1 commit into from
Jan 30, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/doc/book/testing.md
Original file line number Diff line number Diff line change
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