Skip to content

Commit 4433c9f

Browse files
committed
Sync md and docx
1 parent 45a1108 commit 4433c9f

File tree

9 files changed

+79
-61
lines changed

9 files changed

+79
-61
lines changed

listings/ch11-writing-automated-tests/listing-11-07/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub fn add_two(a: usize) -> usize {
1+
pub fn add_two(a: u64) -> u64 {
22
a + 2
33
}
44

listings/ch11-writing-automated-tests/listing-11-11/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub fn add_two(a: usize) -> usize {
1+
pub fn add_two(a: u64) -> u64 {
22
a + 2
33
}
44

listings/ch11-writing-automated-tests/listing-11-12/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
pub fn add_two(a: usize) -> usize {
1+
pub fn add_two(a: u64) -> u64 {
22
internal_adder(a, 2)
33
}
44

5-
fn internal_adder(left: usize, right: usize) -> usize {
5+
fn internal_adder(left: u64, right: u64) -> u64 {
66
left + right
77
}
88

listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ANCHOR: here
2-
pub fn add_two(a: usize) -> usize {
2+
pub fn add_two(a: u64) -> u64 {
33
a + 3
44
}
55
// ANCHOR_END: here

listings/ch11-writing-automated-tests/output-only-04-running-ignored/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ $ cargo test -- --ignored
44
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
55

66
running 1 test
7-
test expensive_test ... ok
7+
test tests::expensive_test ... ok
88

99
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s
1010

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
// ANCHOR: here
2-
#[test]
3-
fn it_works() {
4-
assert_eq!(2 + 2, 4);
1+
pub fn add(left: u64, right: u64) -> u64 {
2+
left + right
53
}
64

7-
#[test]
8-
#[ignore]
9-
fn expensive_test() {
10-
// code that takes an hour to run
5+
// ANCHOR: here
6+
#[cfg(test)]
7+
mod tests {
8+
use super::*;
9+
10+
#[test]
11+
fn it_works() {
12+
let result = add(2, 2);
13+
assert_eq!(result, 4);
14+
}
15+
16+
#[test]
17+
#[ignore]
18+
fn expensive_test() {
19+
// code that takes an hour to run
20+
}
1121
}
1222
// ANCHOR_END: here

nostarch/chapter11.md

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ The `cargo test` command runs all tests in our project, as shown in Listing
139139
$ cargo test
140140
Compiling adder v0.1.0 (file:///projects/adder)
141141
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.57s
142-
Running unittests src/lib.rs (file:///projects/adder/target/debug/deps/adder-7acb243c25ffd9dc)
142+
Running unittests src/lib.rs (target/debug/deps/adder-01ad14159ff659ab)
143143
144144
running 1 test
145145
test tests::it_works ... ok
@@ -151,6 +151,7 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; fini
151151
running 0 tests
152152
153153
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
154+
154155
```
155156

156157
Listing 11-2: The output from running the automatically generated test
@@ -164,7 +165,7 @@ instance; we’ll cover that in the “Ignoring Some Tests Unless Specifically
164165
Requested” section later in this chapter. Because we
165166
haven’t done that here, the summary shows `0 ignored`. We can also pass an
166167
argument to the `cargo test` command to run only tests whose name matches a
167-
string; this is called *filtering* and we’ll cover that in the “Running a
168+
string; this is called *filtering* and we’ll cover it in the “Running a
168169
Subset of Tests by Name” section. Here we haven’t
169170
filtered the tests being run, so the end of the summary shows `0 filtered out`.
170171

@@ -274,6 +275,7 @@ test tests::exploration ... ok
274275
failures:
275276
276277
---- tests::another stdout ----
278+
277279
thread 'tests::another' panicked at src/lib.rs:17:9:
278280
Make this test fail
279281
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
@@ -297,12 +299,12 @@ check the line number of the panic matches the line number in the following para
297299
Instead of `ok`, the line `test tests::another` shows `FAILED`. Two new
298300
sections appear between the individual results and the summary: the first
299301
displays the detailed reason for each test failure. In this case, we get the
300-
details that `another` failed because it `panicked at 'Make this test fail'` on
301-
line 17 in the *src/lib.rs* file. The next section lists just the names of all
302-
the failing tests, which is useful when there are lots of tests and lots of
303-
detailed failing test output. We can use the name of a failing test to run just
304-
that test to more easily debug it; we’ll talk more about ways to run tests in
305-
the “Controlling How Tests Are Run” section.
302+
details that `tests::another` failed because it panicked with the message `Make this test fail` on line 17 in the *src/lib.rs* file. The next section lists
303+
just the names of all the failing tests, which is useful when there are lots of
304+
tests and lots of detailed failing test output. We can use the name of a
305+
failing test to run just that test to more easily debug it; we’ll talk more
306+
about ways to run tests in the “Controlling How Tests Are
307+
Run” section.
306308

307309
The summary line displays at the end: overall, our test result is `FAILED`. We
308310
had one test pass and one test fail.
@@ -489,6 +491,7 @@ test tests::smaller_cannot_hold_larger ... ok
489491
failures:
490492
491493
---- tests::larger_can_hold_smaller stdout ----
494+
492495
thread 'tests::larger_can_hold_smaller' panicked at src/lib.rs:28:9:
493496
assertion failed: larger.can_hold(&smaller)
494497
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
@@ -525,7 +528,7 @@ parameter, then we test this function using the `assert_eq!` macro.
525528
src/lib.rs
526529

527530
```
528-
pub fn add_two(a: usize) -> usize {
531+
pub fn add_two(a: u64) -> u64 {
529532
a + 2
530533
}
531534
@@ -565,15 +568,14 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; fini
565568
```
566569

567570
We create a variable named `result` that holds the result of calling
568-
`add_two(2)`. Then we pass `result` and `4` as the arguments to `assert_eq!`.
569-
The output line for this test is `test tests::it_adds_two ... ok`, and the `ok`
570-
text indicates that our test passed!
571+
`add_two(2)`. Then we pass `result` and `4` as the arguments to the
572+
`assert_eq!` macro. The output line for this test is `test tests::it_adds_two ... ok`, and the `ok` text indicates that our test passed!
571573

572574
Let’s introduce a bug into our code to see what `assert_eq!` looks like when it
573575
fails. Change the implementation of the `add_two` function to instead add `3`:
574576

575577
```
576-
pub fn add_two(a: usize) -> usize {
578+
pub fn add_two(a: u64) -> u64 {
577579
a + 3
578580
}
579581
```
@@ -592,6 +594,7 @@ test tests::it_adds_two ... FAILED
592594
failures:
593595
594596
---- tests::it_adds_two stdout ----
597+
595598
thread 'tests::it_adds_two' panicked at src/lib.rs:12:9:
596599
assertion `left == right` failed
597600
left: 5
@@ -607,20 +610,20 @@ test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out;
607610
error: test failed, to rerun pass `--lib`
608611
```
609612

610-
Our test caught the bug! The `it_adds_two` test failed, and the message tells us
611-
that the assertion that failed was ``assertion `left == right` failed`` and what
612-
the `left` and `right` values are. This message helps us start debugging: the
613-
`left` argument, where we had the result of calling `add_two(2)`, was `5` but
614-
the `right` argument was `4`. You can imagine that this would be especially
615-
helpful when we have a lot of tests going on.
613+
Our test caught the bug! The `tests::it_adds_two` test failed, and the message
614+
tells us that the assertion that failed was `left == right` and what the `left`
615+
and `right` values are. This message helps us start debugging: the `left`
616+
argument, where we had the result of calling `add_two(2)`, was `5` but the
617+
`right` argument was `4`. You can imagine that this would be especially helpful
618+
when we have a lot of tests going on.
616619

617620
Note that in some languages and test frameworks, the parameters to equality
618621
assertion functions are called `expected` and `actual`, and the order in which
619622
we specify the arguments matters. However, in Rust, they’re called `left` and
620-
`right`, and the order in which we specify the value we expect and the value the
621-
code produces doesn’t matter. We could write the assertion in this test as
623+
`right`, and the order in which we specify the value we expect and the value
624+
the code produces doesn’t matter. We could write the assertion in this test as
622625
`assert_eq!(4, result)`, which would result in the same failure message that
623-
that displays `` assertion failed: `(left == right)` ``.
626+
displays `` assertion `left == right` failed``.
624627

625628
The `assert_ne!` macro will pass if the two values we give it are not equal and
626629
fail if they’re equal. This macro is most useful for cases when we’re not sure
@@ -706,6 +709,7 @@ test tests::greeting_contains_name ... FAILED
706709
failures:
707710
708711
---- tests::greeting_contains_name stdout ----
712+
709713
thread 'tests::greeting_contains_name' panicked at src/lib.rs:12:9:
710714
assertion failed: result.contains("Carol")
711715
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
@@ -750,6 +754,7 @@ test tests::greeting_contains_name ... FAILED
750754
failures:
751755
752756
---- tests::greeting_contains_name stdout ----
757+
753758
thread 'tests::greeting_contains_name' panicked at src/lib.rs:12:9:
754759
Greeting did not contain name, value was `Hello!`
755760
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
@@ -962,6 +967,7 @@ test tests::greater_than_100 - should panic ... FAILED
962967
failures:
963968
964969
---- tests::greater_than_100 stdout ----
970+
965971
thread 'tests::greater_than_100' panicked at src/lib.rs:12:13:
966972
Guess value must be greater than or equal to 1, got 200.
967973
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
@@ -1120,6 +1126,7 @@ failures:
11201126
11211127
---- tests::this_test_will_fail stdout ----
11221128
I got the value 8
1129+
11231130
thread 'tests::this_test_will_fail' panicked at src/lib.rs:19:9:
11241131
assertion `left == right` failed
11251132
left: 10
@@ -1173,6 +1180,7 @@ failures:
11731180
11741181
---- tests::this_test_will_fail stdout ----
11751182
I got the value 8
1183+
11761184
thread 'tests::this_test_will_fail' panicked at src/lib.rs:19:9:
11771185
assertion `left == right` failed
11781186
left: 10
@@ -1201,7 +1209,7 @@ our `add_two` function, as shown in Listing 11-11, and choose which ones to run.
12011209
src/lib.rs
12021210

12031211
```
1204-
pub fn add_two(a: usize) -> usize {
1212+
pub fn add_two(a: u64) -> u64 {
12051213
a + 2
12061214
}
12071215
@@ -1366,7 +1374,7 @@ $ cargo test -- --ignored
13661374
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
13671375
13681376
running 1 test
1369-
test expensive_test ... ok
1377+
test tests::expensive_test ... ok
13701378
13711379
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s
13721380
@@ -1458,11 +1466,11 @@ Consider the code in Listing 11-12 with the private function `internal_adder`.
14581466
src/lib.rs
14591467

14601468
```
1461-
pub fn add_two(a: usize) -> usize {
1469+
pub fn add_two(a: u64) -> u64 {
14621470
internal_adder(a, 2)
14631471
}
14641472
1465-
fn internal_adder(left: usize, right: usize) -> usize {
1473+
fn internal_adder(left: u64, right: u64) -> u64 {
14661474
left + right
14671475
}
14681476

nostarch/docx/chapter11.docx

11.7 KB
Binary file not shown.

src/ch11-01-writing-tests.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ instance; we’ll cover that in the [“Ignoring Some Tests Unless Specifically
9898
Requested”][ignoring]<!-- ignore --> section later in this chapter. Because we
9999
haven’t done that here, the summary shows `0 ignored`. We can also pass an
100100
argument to the `cargo test` command to run only tests whose name matches a
101-
string; this is called _filtering_ and we’ll cover that in the [“Running a
101+
string; this is called _filtering_ and we’ll cover it in the [“Running a
102102
Subset of Tests by Name”][subset]<!-- ignore --> section. Here we haven’t
103103
filtered the tests being run, so the end of the summary shows `0 filtered out`.
104104

@@ -164,13 +164,13 @@ check the line number of the panic matches the line number in the following para
164164
Instead of `ok`, the line `test tests::another` shows `FAILED`. Two new
165165
sections appear between the individual results and the summary: the first
166166
displays the detailed reason for each test failure. In this case, we get the
167-
details that `another` failed because it `panicked at 'Make this test fail'` on
168-
line 17 in the _src/lib.rs_ file. The next section lists just the names of all
169-
the failing tests, which is useful when there are lots of tests and lots of
170-
detailed failing test output. We can use the name of a failing test to run just
171-
that test to more easily debug it; we’ll talk more about ways to run tests in
172-
the [“Controlling How Tests Are Run”][controlling-how-tests-are-run]<!-- ignore
173-
--> section.
167+
details that `tests::another` failed because it panicked with the message `Make
168+
this test fail` on line 17 in the _src/lib.rs_ file. The next section lists
169+
just the names of all the failing tests, which is useful when there are lots of
170+
tests and lots of detailed failing test output. We can use the name of a
171+
failing test to run just that test to more easily debug it; we’ll talk more
172+
about ways to run tests in the [“Controlling How Tests Are
173+
Run”][controlling-how-tests-are-run]<!-- ignore --> section.
174174

175175
The summary line displays at the end: overall, our test result is `FAILED`. We
176176
had one test pass and one test fail.
@@ -298,9 +298,9 @@ Let’s check that it passes!
298298
```
299299

300300
We create a variable named `result` that holds the result of calling
301-
`add_two(2)`. Then we pass `result` and `4` as the arguments to `assert_eq!`.
302-
The output line for this test is `test tests::it_adds_two ... ok`, and the `ok`
303-
text indicates that our test passed!
301+
`add_two(2)`. Then we pass `result` and `4` as the arguments to the
302+
`assert_eq!` macro. The output line for this test is `test tests::it_adds_two
303+
... ok`, and the `ok` text indicates that our test passed!
304304

305305
Let’s introduce a bug into our code to see what `assert_eq!` looks like when it
306306
fails. Change the implementation of the `add_two` function to instead add `3`:
@@ -315,20 +315,20 @@ Run the tests again:
315315
{{#include ../listings/ch11-writing-automated-tests/no-listing-04-bug-in-add-two/output.txt}}
316316
```
317317

318-
Our test caught the bug! The `it_adds_two` test failed, and the message tells us
319-
that the assertion that failed was ``assertion `left == right` failed`` and what
320-
the `left` and `right` values are. This message helps us start debugging: the
321-
`left` argument, where we had the result of calling `add_two(2)`, was `5` but
322-
the `right` argument was `4`. You can imagine that this would be especially
323-
helpful when we have a lot of tests going on.
318+
Our test caught the bug! The `tests::it_adds_two` test failed, and the message
319+
tells us that the assertion that failed was `left == right` and what the `left`
320+
and `right` values are. This message helps us start debugging: the `left`
321+
argument, where we had the result of calling `add_two(2)`, was `5` but the
322+
`right` argument was `4`. You can imagine that this would be especially helpful
323+
when we have a lot of tests going on.
324324

325325
Note that in some languages and test frameworks, the parameters to equality
326326
assertion functions are called `expected` and `actual`, and the order in which
327327
we specify the arguments matters. However, in Rust, they’re called `left` and
328-
`right`, and the order in which we specify the value we expect and the value the
329-
code produces doesn’t matter. We could write the assertion in this test as
330-
`assert_eq!(add_two(2), result)`, which would result in the same failure message
331-
that displays `` assertion failed: `(left == right)` ``.
328+
`right`, and the order in which we specify the value we expect and the value
329+
the code produces doesn’t matter. We could write the assertion in this test as
330+
`assert_eq!(4, result)`, which would result in the same failure message that
331+
displays `` assertion `left == right` failed``.
332332

333333
The `assert_ne!` macro will pass if the two values we give it are not equal and
334334
fail if they’re equal. This macro is most useful for cases when we’re not sure

0 commit comments

Comments
 (0)