Skip to content

Commit d9c60ca

Browse files
committed
Instruct play.rust-lang.org to treat code as tests
Without these changes, play.rust-lang.org (as of today) would wrap our examples in `fn main() {}`. This prevents the user from being able to easily run the tests.
1 parent b1d0c5b commit d9c60ca

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

src/doc/book/testing.md

+46-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Cargo will automatically generate a simple test when you make a new project.
2424
Here's the contents of `src/lib.rs`:
2525

2626
```rust
27+
# // The next line exists to trick play.rust-lang.org into running our code as a
28+
# // test:
29+
# // fn main
30+
#
2731
#[cfg(test)]
2832
mod tests {
2933
#[test]
@@ -79,6 +83,10 @@ So why does our do-nothing test pass? Any test which doesn't `panic!` passes,
7983
and any test that does `panic!` fails. Let's make our test fail:
8084

8185
```rust
86+
# // The next line exists to trick play.rust-lang.org into running our code as a
87+
# // test:
88+
# // fn main
89+
#
8290
#[cfg(test)]
8391
mod tests {
8492
#[test]
@@ -153,6 +161,10 @@ This is useful if you want to integrate `cargo test` into other tooling.
153161
We can invert our test's failure with another attribute: `should_panic`:
154162

155163
```rust
164+
# // The next line exists to trick play.rust-lang.org into running our code as a
165+
# // test:
166+
# // fn main
167+
#
156168
#[cfg(test)]
157169
mod tests {
158170
#[test]
@@ -188,6 +200,10 @@ Rust provides another macro, `assert_eq!`, that compares two arguments for
188200
equality:
189201

190202
```rust
203+
# // The next line exists to trick play.rust-lang.org into running our code as a
204+
# // test:
205+
# // fn main
206+
#
191207
#[cfg(test)]
192208
mod tests {
193209
#[test]
@@ -226,6 +242,10 @@ make sure that the failure message contains the provided text. A safer version
226242
of the example above would be:
227243

228244
```rust
245+
# // The next line exists to trick play.rust-lang.org into running our code as a
246+
# // test:
247+
# // fn main
248+
#
229249
#[cfg(test)]
230250
mod tests {
231251
#[test]
@@ -239,6 +259,10 @@ mod tests {
239259
That's all there is to the basics! Let's write one 'real' test:
240260

241261
```rust,ignore
262+
# // The next line exists to trick play.rust-lang.org into running our code as a
263+
# // test:
264+
# // fn main
265+
#
242266
pub fn add_two(a: i32) -> i32 {
243267
a + 2
244268
}
@@ -263,6 +287,10 @@ Sometimes a few specific tests can be very time-consuming to execute. These
263287
can be disabled by default by using the `ignore` attribute:
264288

265289
```rust
290+
# // The next line exists to trick play.rust-lang.org into running our code as a
291+
# // test:
292+
# // fn main
293+
#
266294
pub fn add_two(a: i32) -> i32 {
267295
a + 2
268296
}
@@ -338,6 +366,10 @@ was missing from our last example. Let's explain what this does.
338366
The idiomatic way of writing our example looks like this:
339367

340368
```rust,ignore
369+
# // The next line exists to trick play.rust-lang.org into running our code as a
370+
# // test:
371+
# // fn main
372+
#
341373
pub fn add_two(a: i32) -> i32 {
342374
a + 2
343375
}
@@ -366,6 +398,10 @@ a large module, and so this is a common use of globs. Let's change our
366398
`src/lib.rs` to make use of it:
367399

368400
```rust,ignore
401+
# // The next line exists to trick play.rust-lang.org into running our code as a
402+
# // test:
403+
# // fn main
404+
#
369405
pub fn add_two(a: i32) -> i32 {
370406
a + 2
371407
}
@@ -415,9 +451,14 @@ To write an integration test, let's make a `tests` directory and
415451
put a `tests/integration_test.rs` file inside with this as its contents:
416452

417453
```rust,ignore
454+
# // The next line exists to trick play.rust-lang.org into running our code as a
455+
# // test:
456+
# // fn main
457+
#
458+
# // Sadly, this code will not work in play.rust-lang.org, because we have no
459+
# // crate adder to import. You'll need to try this part on your own machine.
418460
extern crate adder;
419461
420-
# fn main() {}
421462
#[test]
422463
fn it_works() {
423464
assert_eq!(4, adder::add_two(2));
@@ -478,7 +519,10 @@ running examples in your documentation (**note:** this only works in library
478519
crates, not binary crates). Here's a fleshed-out `src/lib.rs` with examples:
479520

480521
```rust,ignore
481-
# fn main() {}
522+
# // The next line exists to trick play.rust-lang.org into running our code as a
523+
# // test:
524+
# // fn main
525+
#
482526
//! The `adder` crate provides functions that add numbers to other numbers.
483527
//!
484528
//! # Examples

0 commit comments

Comments
 (0)