Skip to content

Commit

Permalink
Add use super::*; to unit-test examples.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sunfishcode committed Nov 11, 2022
1 parent 3f64052 commit 48fb3a7
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = 2 + 2;
let result = add(2, 2);
assert_eq!(result, 4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test tests::exploration ... ok
failures:

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


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// ANCHOR: here
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn exploration() {
assert_eq!(2 + 2, 4);
let result = add(2, 2);
assert_eq!(result, 4);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn exploration() {
assert_eq!(2 + 2, 4);
let result = add(2, 2);
assert_eq!(result, 4);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() -> Result<(), String> {
if 2 + 2 == 4 {
if add(2, 2) == 4 {
Ok(())
} else {
Err(String::from("two plus two does not equal four"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = 2 + 2;
let result = add(2, 2);
assert_eq!(result, 4);
}
}
35 changes: 30 additions & 5 deletions nostarch/chapter11.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,17 @@ the `it_works` function to a different name, such as `exploration`, like so:
Filename: src/lib.rs

```
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exploration() {
let result = 2 + 2;
let result = add(2, 2);
assert_eq!(result, 4);
}
}
Expand All @@ -203,11 +209,18 @@ is to call the `panic!` macro. Enter the new test as a function named
Filename: src/lib.rs

```
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exploration() {
assert_eq!(2 + 2, 4);
let result = add(2, 2);
assert_eq!(result, 4);
}
#[test]
Expand All @@ -231,7 +244,7 @@ test tests::exploration ... ok
2 failures:
---- tests::another stdout ----
thread 'main' panicked at 'Make this test fail', src/lib.rs:10:9
thread 'main' panicked at 'Make this test fail', src/lib.rs:17:9
note: run with `RUST_BACKTRACE=1` environment variable to display
a backtrace
Expand Down Expand Up @@ -867,11 +880,17 @@ E>` and return an `Err` instead of panicking:
Filename: src/lib.rs

```
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() -> Result<(), String> {
if 2 + 2 == 4 {
if add(2, 2) == 4 {
Ok(())
} else {
Err(String::from("two plus two does not equal four"))
Expand Down Expand Up @@ -1265,11 +1284,17 @@ this chapter, Cargo generated this code for us:
Filename: src/lib.rs

```
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = 2 + 2;
let result = add(2, 2);
assert_eq!(result, 4);
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/ch11-01-writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,19 @@ cd ../../..
<span class="caption">Listing 11-1: The test module and function generated
automatically by `cargo new`</span>

For now, let’s ignore the top two lines and focus on the function. Note the
The file starts with an example `add` function, so that we have something
to test.

For now, let’s ignore the next few lines and focus on the function with the
`#[test]` annotation: this attribute indicates this is a test function, so the
test runner knows to treat this function as a test. We might also have non-test
functions in the `tests` module to help set up common scenarios or perform
common operations, so we always need to indicate which functions are tests.

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

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

0 comments on commit 48fb3a7

Please sign in to comment.