@@ -24,6 +24,10 @@ Cargo will automatically generate a simple test when you make a new project.
24
24
Here's the contents of ` src/lib.rs ` :
25
25
26
26
``` rust
27
+ # // The next line exists to trick play.rust-lang.org into running our code as a
28
+ # // test:
29
+ # // fn main
30
+ #
27
31
#[cfg(test)]
28
32
mod tests {
29
33
#[test]
@@ -79,6 +83,10 @@ So why does our do-nothing test pass? Any test which doesn't `panic!` passes,
79
83
and any test that does ` panic! ` fails. Let's make our test fail:
80
84
81
85
``` rust
86
+ # // The next line exists to trick play.rust-lang.org into running our code as a
87
+ # // test:
88
+ # // fn main
89
+ #
82
90
#[cfg(test)]
83
91
mod tests {
84
92
#[test]
@@ -153,6 +161,10 @@ This is useful if you want to integrate `cargo test` into other tooling.
153
161
We can invert our test's failure with another attribute: ` should_panic ` :
154
162
155
163
``` rust
164
+ # // The next line exists to trick play.rust-lang.org into running our code as a
165
+ # // test:
166
+ # // fn main
167
+ #
156
168
#[cfg(test)]
157
169
mod tests {
158
170
#[test]
@@ -188,6 +200,10 @@ Rust provides another macro, `assert_eq!`, that compares two arguments for
188
200
equality:
189
201
190
202
``` rust
203
+ # // The next line exists to trick play.rust-lang.org into running our code as a
204
+ # // test:
205
+ # // fn main
206
+ #
191
207
#[cfg(test)]
192
208
mod tests {
193
209
#[test]
@@ -226,6 +242,10 @@ make sure that the failure message contains the provided text. A safer version
226
242
of the example above would be:
227
243
228
244
``` rust
245
+ # // The next line exists to trick play.rust-lang.org into running our code as a
246
+ # // test:
247
+ # // fn main
248
+ #
229
249
#[cfg(test)]
230
250
mod tests {
231
251
#[test]
@@ -239,6 +259,10 @@ mod tests {
239
259
That's all there is to the basics! Let's write one 'real' test:
240
260
241
261
``` 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
+ #
242
266
pub fn add_two(a: i32) -> i32 {
243
267
a + 2
244
268
}
@@ -263,6 +287,10 @@ Sometimes a few specific tests can be very time-consuming to execute. These
263
287
can be disabled by default by using the ` ignore ` attribute:
264
288
265
289
``` rust
290
+ # // The next line exists to trick play.rust-lang.org into running our code as a
291
+ # // test:
292
+ # // fn main
293
+ #
266
294
pub fn add_two (a : i32 ) -> i32 {
267
295
a + 2
268
296
}
@@ -338,6 +366,10 @@ was missing from our last example. Let's explain what this does.
338
366
The idiomatic way of writing our example looks like this:
339
367
340
368
``` 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
+ #
341
373
pub fn add_two(a: i32) -> i32 {
342
374
a + 2
343
375
}
@@ -366,6 +398,10 @@ a large module, and so this is a common use of globs. Let's change our
366
398
` src/lib.rs ` to make use of it:
367
399
368
400
``` 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
+ #
369
405
pub fn add_two(a: i32) -> i32 {
370
406
a + 2
371
407
}
@@ -415,9 +451,14 @@ To write an integration test, let's make a `tests` directory and
415
451
put a ` tests/integration_test.rs ` file inside with this as its contents:
416
452
417
453
``` 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.
418
460
extern crate adder;
419
461
420
- # fn main() {}
421
462
#[test]
422
463
fn it_works() {
423
464
assert_eq!(4, adder::add_two(2));
@@ -478,7 +519,10 @@ running examples in your documentation (**note:** this only works in library
478
519
crates, not binary crates). Here's a fleshed-out ` src/lib.rs ` with examples:
479
520
480
521
``` 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
+ #
482
526
//! The `adder` crate provides functions that add numbers to other numbers.
483
527
//!
484
528
//! # Examples
0 commit comments