@@ -43,7 +43,7 @@ $ cd adder
4343The contents of the * src/lib.rs* file in your ` adder ` library should look like
4444Listing 11-1.
4545
46- <span class = " filename " >Filename: src/lib.rs</ span >
46+ <Listing number = " 11-1 " file-name = " src/lib.rs " caption = " The test module and function generated automatically by `cargo new` " >
4747
4848<!-- manual-regeneration
4949cd listings/ch11-writing-automated-tests
@@ -59,8 +59,7 @@ cd ../../..
5959{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-01/src/lib.rs}}
6060```
6161
62- <span class =" caption " >Listing 11-1: The test module and function generated
63- automatically by ` cargo new ` </span >
62+ </Listing >
6463
6564For now, let’s focus solely on the ` it_works() ` function. Note the
6665` #[test] ` annotation: this attribute indicates this is a test function, so the
@@ -76,12 +75,13 @@ passes.
7675The ` cargo test ` command runs all tests in our project, as shown in Listing
777611-2.
7877
78+ <Listing number =" 11-2 " caption =" The output from running the automatically generated test " >
79+
7980``` console
8081{{#include ../listings/ch11-writing-automated-tests/listing-11-01/output.txt}}
8182```
8283
83- <span class =" caption " >Listing 11-2: The output from running the automatically
84- generated test</span >
84+ </Listing >
8585
8686Cargo compiled and ran the test. We see the line ` running 1 test ` . The next
8787line shows the name of the generated test function, called ` it_works ` , and that
@@ -113,12 +113,14 @@ ignore the `Doc-tests` output.
113113Let’s start to customize the test to our own needs. First change the name of
114114the ` it_works ` function to a different name, such as ` exploration ` , like so:
115115
116- <span class = " filename " >Filename: src/lib.rs</ span >
116+ <Listing file-name = " src/lib.rs " >
117117
118118``` rust,noplayground
119119{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs}}
120120```
121121
122+ </Listing >
123+
122124Then run ` cargo test ` again. The output now shows ` exploration ` instead of
123125` it_works ` :
124126
@@ -133,24 +135,24 @@ marked as failed. In Chapter 9, we talked about how the simplest way to panic
133135is to call the ` panic! ` macro. Enter the new test as a function named
134136` another ` , so your * src/lib.rs* file looks like Listing 11-3.
135137
136- <span class = " filename " >Filename: src/lib .rs</ span >
138+ <Listing number = " 11-3 " file-name = " src/libs .rs " caption = " Adding a second test that will fail because we call the `panic!` macro " >
137139
138140``` rust,panics,noplayground
139141{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs:here}}
140142```
141143
142- <span class =" caption " >Listing 11-3: Adding a second test that will fail because
143- we call the ` panic! ` macro</span >
144+ </Listing >
144145
145146Run the tests again using ` cargo test ` . The output should look like Listing
14614711-4, which shows that our ` exploration ` test passed and ` another ` failed.
147148
149+ <Listing number =" 11-4 " caption =" Test results when one test passes and one test fails " >
150+
148151``` console
149152{{#include ../listings/ch11-writing-automated-tests/listing-11-03/output.txt}}
150153```
151154
152- <span class =" caption " >Listing 11-4: Test results when one test passes and one
153- test fails</span >
155+ </Listing >
154156
155157Instead of ` ok ` , the line ` test tests::another ` shows ` FAILED ` . Two new
156158sections appear between the individual results and the summary: the first
@@ -182,29 +184,27 @@ In Chapter 5, Listing 5-15, we used a `Rectangle` struct and a `can_hold`
182184method, which are repeated here in Listing 11-5. Let’s put this code in the
183185* src/lib.rs* file, then write some tests for it using the ` assert! ` macro.
184186
185- <span class = " filename " >Filename: src/lib.rs</ span >
187+ <Listing number = " 11-5 " file-name = " src/lib.rs " caption = " The `Rectangle` struct and its `can_hold` method from Chapter 5 " >
186188
187189``` rust,noplayground
188190{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-05/src/lib.rs:here}}
189191```
190192
191- <span class =" caption " >Listing 11-5: Using the ` Rectangle ` struct and its
192- ` can_hold ` method from Chapter 5</span >
193+ </Listing >
193194
194195The ` can_hold ` method returns a Boolean, which means it’s a perfect use case
195196for the ` assert! ` macro. In Listing 11-6, we write a test that exercises the
196197` can_hold ` method by creating a ` Rectangle ` instance that has a width of 8 and
197198a height of 7 and asserting that it can hold another ` Rectangle ` instance that
198199has a width of 5 and a height of 1.
199200
200- <span class = " filename " >Filename: src/lib.rs</ span >
201+ <Listing number = " 11-6 " file-name = " src/lib.rs " caption = " A test for `can_hold` that checks whether a larger rectangle can indeed hold a smaller rectangle " >
201202
202203``` rust,noplayground
203204{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-06/src/lib.rs:here}}
204205```
205206
206- <span class =" caption " >Listing 11-6: A test for ` can_hold ` that checks whether a
207- larger rectangle can indeed hold a smaller rectangle</span >
207+ </Listing >
208208
209209Note that we’ve added a new line inside the ` tests ` module: ` use super::*; ` .
210210The ` tests ` module is a regular module that follows the usual visibility rules
@@ -227,12 +227,14 @@ supposed to return `true`, so our test should pass. Let’s find out!
227227It does pass! Let’s add another test, this time asserting that a smaller
228228rectangle cannot hold a larger rectangle:
229229
230- <span class = " filename " >Filename: src/lib.rs</ span >
230+ <Listing file-name = " src/lib.rs " >
231231
232232``` rust,noplayground
233233{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-02-adding-another-rectangle-test/src/lib.rs:here}}
234234```
235235
236+ </Listing >
237+
236238Because the correct result of the ` can_hold ` function in this case is ` false ` ,
237239we need to negate that result before we pass it to the ` assert! ` macro. As a
238240result, our test will pass if ` can_hold ` returns ` false ` :
@@ -276,14 +278,13 @@ expression, without printing the values that led to the `false` value.
276278In Listing 11-7, we write a function named ` add_two ` that adds ` 2 ` to its
277279parameter, then we test this function using the ` assert_eq! ` macro.
278280
279- <span class = " filename " >Filename: src/lib.rs</ span >
281+ <Listing number = " 11-7 " file-name = " src/lib.rs " caption = " Testing the function `add_two` using the `assert_eq!` macro " >
280282
281283``` rust,noplayground
282284{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-07/src/lib.rs}}
283285```
284286
285- <span class =" caption " >Listing 11-7: Testing the function ` add_two ` using the
286- ` assert_eq! ` macro</span >
287+ </Listing >
287288
288289Let’s check that it passes!
289290
@@ -360,12 +361,14 @@ the problem is with the code.
360361For example, let’s say we have a function that greets people by name and we
361362want to test that the name we pass into the function appears in the output:
362363
363- <span class = " filename " >Filename: src/lib.rs</ span >
364+ <Listing file-name = " src/lib.rs " >
364365
365366``` rust,noplayground
366367{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-05-greeter/src/lib.rs}}
367368```
368369
370+ </Listing >
371+
369372The requirements for this program haven’t been agreed upon yet, and we’re
370373pretty sure the ` Hello ` text at the beginning of the greeting will change. We
371374decided we don’t want to have to update the test when the requirements change,
@@ -421,14 +424,13 @@ inside the function doesn’t panic.
421424Listing 11-8 shows a test that checks that the error conditions of ` Guess::new `
422425happen when we expect them to.
423426
424- <span class = " filename " >Filename: src/lib.rs</ span >
427+ <Listing number = " 11-8 " file-name = " src/lib.rs " caption = " Testing that a condition will cause a `panic!` " >
425428
426429``` rust,noplayground
427430{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs}}
428431```
429432
430- <span class =" caption " >Listing 11-8: Testing that a condition will cause a
431- ` panic! ` </span >
433+ </Listing >
432434
433435We place the ` #[should_panic] ` attribute after the ` #[test] ` attribute and
434436before the test function it applies to. Let’s look at the result when this test
@@ -464,14 +466,16 @@ consider the modified code for `Guess` in Listing 11-9 where the `new` function
464466panics with different messages depending on whether the value is too small or
465467too large.
466468
467- <span class =" filename " >Filename: src/lib.rs</span >
469+ <Listing number =" 11-9 " caption =" Testing for a `panic!` with a panic message containing a specified substring " >
470+
471+ ``` rust,noplayground
472+ {{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs}}
468473
469474```rust,noplayground
470475{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs:here}}
471476```
472477
473- <span class =" caption " >Listing 11-9: Testing for a ` panic! ` with a panic message
474- containing a specified substring</span >
478+ </Listing >
475479
476480This test will pass because the value we put in the ` should_panic ` attribute’s
477481` expected ` parameter is a substring of the message that the ` Guess::new `
0 commit comments