forked from rust-lang/book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
3f64052
commit 48fb3a7
Showing
8 changed files
with
75 additions
and
15 deletions.
There are no files selected for viewing
8 changes: 7 additions & 1 deletion
8
listings/ch11-writing-automated-tests/listing-11-01/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 8 additions & 1 deletion
9
listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 8 additions & 1 deletion
9
listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
8 changes: 7 additions & 1 deletion
8
listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletion
8
listings/ch14-more-about-cargo/output-only-02-add-one/add/add_one/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters