Skip to content

Commit 48fb3a7

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 3f64052 commit 48fb3a7

File tree

8 files changed

+75
-15
lines changed

8 files changed

+75
-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: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,17 @@ the `it_works` function to a different name, such as `exploration`, like so:
172172
Filename: src/lib.rs
173173

174174
```
175+
pub fn add(left: usize, right: usize) -> usize {
176+
left + right
177+
}
178+
175179
#[cfg(test)]
176180
mod tests {
181+
use super::*;
182+
177183
#[test]
178184
fn exploration() {
179-
let result = 2 + 2;
185+
let result = add(2, 2);
180186
assert_eq!(result, 4);
181187
}
182188
}
@@ -203,11 +209,18 @@ is to call the `panic!` macro. Enter the new test as a function named
203209
Filename: src/lib.rs
204210

205211
```
212+
pub fn add(left: usize, right: usize) -> usize {
213+
left + right
214+
}
215+
206216
#[cfg(test)]
207217
mod tests {
218+
use super::*;
219+
208220
#[test]
209221
fn exploration() {
210-
assert_eq!(2 + 2, 4);
222+
let result = add(2, 2);
223+
assert_eq!(result, 4);
211224
}
212225
213226
#[test]
@@ -231,7 +244,7 @@ test tests::exploration ... ok
231244
2 failures:
232245
233246
---- tests::another stdout ----
234-
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
235248
note: run with `RUST_BACKTRACE=1` environment variable to display
236249
a backtrace
237250
@@ -867,11 +880,17 @@ E>` and return an `Err` instead of panicking:
867880
Filename: src/lib.rs
868881

869882
```
883+
pub fn add(left: usize, right: usize) -> usize {
884+
left + right
885+
}
886+
870887
#[cfg(test)]
871888
mod tests {
889+
use super::*;
890+
872891
#[test]
873892
fn it_works() -> Result<(), String> {
874-
if 2 + 2 == 4 {
893+
if add(2, 2) == 4 {
875894
Ok(())
876895
} else {
877896
Err(String::from("two plus two does not equal four"))
@@ -1265,11 +1284,17 @@ this chapter, Cargo generated this code for us:
12651284
Filename: src/lib.rs
12661285

12671286
```
1287+
pub fn add(left: usize, right: usize) -> usize {
1288+
left + right
1289+
}
1290+
12681291
#[cfg(test)]
12691292
mod tests {
1293+
use super::*;
1294+
12701295
#[test]
12711296
fn it_works() {
1272-
let result = 2 + 2;
1297+
let result = add(2, 2);
12731298
assert_eq!(result, 4);
12741299
}
12751300
}

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)