Skip to content

Commit 8d764b5

Browse files
committed
Add use super::*; to unit-test examples.
rust-lang/cargo#10706 switched the `cargo init --lib`-generated src/lib.rs to use a function and `use super::*;` inside the `mod test`. This makes it easier for new users to write their own functions and add tests, as it means the tests can refer to the new functions without any extra work, and without rustc asking them to add explicit `use`s for each new thing they add. This PR updates the parts of the book that use this src/lib.rs and similar examples, to match the new output of `cargo init --lib`, and to additionally help guide users to using `use super::*;` inside their `mod test`s. There is one non-example change, which is to update the wording in src/ch11-01-writing-tests.md to better reflect the new content in the associated example.
1 parent 08965d7 commit 8d764b5

File tree

8 files changed

+77
-15
lines changed

8 files changed

+77
-15
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
pub fn add(left: usize, right: usize) -> usize {
2+
left + right
3+
}
4+
15
#[cfg(test)]
26
mod tests {
7+
use super::*;
8+
39
#[test]
410
fn it_works() {
5-
let result = 2 + 2;
11+
let result = add(2, 2);
612
assert_eq!(result, 4);
713
}
814
}

listings/ch11-writing-automated-tests/listing-11-03/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test tests::exploration ... ok
1010
failures:
1111

1212
---- tests::another stdout ----
13-
thread 'main' panicked at 'Make this test fail', src/lib.rs:10:9
13+
thread 'main' panicked at 'Make this test fail', src/lib.rs:17:9
1414
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1515

1616

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
// ANCHOR: here
2+
pub fn add(left: usize, right: usize) -> usize {
3+
left + right
4+
}
5+
26
#[cfg(test)]
37
mod tests {
8+
use super::*;
9+
410
#[test]
511
fn exploration() {
6-
assert_eq!(2 + 2, 4);
12+
let result = add(2, 2);
13+
assert_eq!(result, 4);
714
}
815

916
#[test]
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
pub fn add(left: usize, right: usize) -> usize {
2+
left + right
3+
}
4+
15
#[cfg(test)]
26
mod tests {
7+
use super::*;
8+
39
#[test]
410
fn exploration() {
5-
assert_eq!(2 + 2, 4);
11+
let result = add(2, 2);
12+
assert_eq!(result, 4);
613
}
714
}

listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
pub fn add(left: usize, right: usize) -> usize {
2+
left + right
3+
}
4+
15
#[cfg(test)]
26
mod tests {
7+
use super::*;
8+
39
#[test]
410
fn it_works() -> Result<(), String> {
5-
if 2 + 2 == 4 {
11+
if add(2, 2) == 4 {
612
Ok(())
713
} else {
814
Err(String::from("two plus two does not equal four"))
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
fn add(left: usize, right: usize) -> usize {
2+
left + right
3+
}
4+
15
#[cfg(test)]
26
mod tests {
7+
use super::*;
8+
39
#[test]
410
fn it_works() {
5-
let result = 2 + 2;
11+
let result = add(2, 2);
612
assert_eq!(result, 4);
713
}
814
}

nostarch/chapter11.md

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,18 @@ the `it_works` function to a different name, such as `exploration`, like so:
173173
Filename: src/lib.rs
174174

175175
```
176+
pub fn add(left: usize, right: usize) -> usize {
177+
left + right
178+
}
179+
176180
#[cfg(test)]
177181
mod tests {
182+
use super::*;
183+
178184
#[test]
179185
fn exploration() {
180-
assert_eq!(2 + 2, 4);
186+
let result = add(2, 2);
187+
assert_eq!(result, 4);
181188
}
182189
}
183190
```
@@ -202,11 +209,18 @@ is to call the `panic!` macro. Enter the new test as a function named
202209
Filename: src/lib.rs
203210

204211
```
212+
pub fn add(left: usize, right: usize) -> usize {
213+
left + right
214+
}
215+
205216
#[cfg(test)]
206217
mod tests {
218+
use super::*;
219+
207220
#[test]
208221
fn exploration() {
209-
assert_eq!(2 + 2, 4);
222+
let result = add(2, 2);
223+
assert_eq!(result, 4);
210224
}
211225
212226
#[test]
@@ -230,7 +244,7 @@ test tests::exploration ... ok
230244
[2] failures:
231245
232246
---- tests::another stdout ----
233-
thread 'main' panicked at 'Make this test fail', src/lib.rs:10:9
247+
thread 'main' panicked at 'Make this test fail', src/lib.rs:17:9
234248
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
235249
236250
[3] failures:
@@ -833,11 +847,17 @@ Our tests so far all panic when they fail. We can also write tests that use
833847
E>` and return an `Err` instead of panicking:
834848

835849
```
850+
pub fn add(left: usize, right: usize) -> usize {
851+
left + right
852+
}
853+
836854
#[cfg(test)]
837855
mod tests {
856+
use super::*;
857+
838858
#[test]
839859
fn it_works() -> Result<(), String> {
840-
if 2 + 2 == 4 {
860+
if add(2, 2) == 4 {
841861
Ok(())
842862
} else {
843863
Err(String::from("two plus two does not equal four"))
@@ -1217,11 +1237,18 @@ this chapter, Cargo generated this code for us:
12171237
Filename: src/lib.rs
12181238

12191239
```
1240+
pub fn add(left: usize, right: usize) -> usize {
1241+
left + right
1242+
}
1243+
12201244
#[cfg(test)]
12211245
mod tests {
1246+
use super::*;
1247+
12221248
#[test]
12231249
fn it_works() {
1224-
assert_eq!(2 + 2, 4);
1250+
let result = add(2, 2);
1251+
assert_eq!(result, 4);
12251252
}
12261253
}
12271254
```

src/ch11-01-writing-tests.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,19 @@ cd ../../..
6262
<span class="caption">Listing 11-1: The test module and function generated
6363
automatically by `cargo new`</span>
6464

65-
For now, let’s ignore the top two lines and focus on the function. Note the
65+
The file starts with an example `add` function, so that we have something
66+
to test.
67+
68+
For now, let’s ignore the next few lines and focus on the function with the
6669
`#[test]` annotation: this attribute indicates this is a test function, so the
6770
test runner knows to treat this function as a test. We might also have non-test
6871
functions in the `tests` module to help set up common scenarios or perform
6972
common operations, so we always need to indicate which functions are tests.
7073

7174
The example function body uses the `assert_eq!` macro to assert that `result`,
72-
which contains the result of adding 2 and 2, equals 4. This assertion serves as
73-
an example of the format for a typical test. Let’s run it to see that this test
74-
passes.
75+
which contains the result of calling `add` with 2 and 2, equals 4. This
76+
assertion serves as an example of the format for a typical test. Let’s run it
77+
to see that this test passes.
7578

7679
The `cargo test` command runs all tests in our project, as shown in Listing
7780
11-2.

0 commit comments

Comments
 (0)