Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport changes to chapter 11 #3969

Merged
merged 8 commits into from
Jul 6, 2024
1 change: 1 addition & 0 deletions ci/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ Rustaceans
rUsT
rustc
rustdoc
RUSTFLAGS
Rustonomicon
rustfix
rustfmt
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
pub fn add(left: usize, right: usize) -> usize {
left + right
}
Expand All @@ -18,4 +17,3 @@ mod tests {
panic!("Make this test fail");
}
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ANCHOR: here
#[derive(Debug)]
struct Rectangle {
width: u32,
Expand All @@ -10,4 +9,3 @@ impl Rectangle {
self.width > other.width && self.height > other.height
}
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub fn add_two(a: i32) -> i32 {
pub fn add_two(a: usize) -> usize {
a + 2
}

Expand All @@ -8,6 +8,7 @@ mod tests {

#[test]
fn it_adds_two() {
assert_eq!(4, add_two(2));
let result = add_two(2);
assert_eq!(result, 4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ failures:
I got the value 8
thread 'tests::this_test_will_fail' panicked at src/lib.rs:19:9:
assertion `left == right` failed
left: 5
right: 10
left: 10
right: 5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ mod tests {
#[test]
fn this_test_will_pass() {
let value = prints_and_returns_10(4);
assert_eq!(10, value);
assert_eq!(value, 10);
}

#[test]
fn this_test_will_fail() {
let value = prints_and_returns_10(8);
assert_eq!(5, value);
assert_eq!(value, 5);
}
}
11 changes: 7 additions & 4 deletions listings/ch11-writing-automated-tests/listing-11-11/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub fn add_two(a: i32) -> i32 {
pub fn add_two(a: usize) -> usize {
a + 2
}

Expand All @@ -8,16 +8,19 @@ mod tests {

#[test]
fn add_two_and_two() {
assert_eq!(4, add_two(2));
let result = add_two(2);
assert_eq!(result, 4);
}

#[test]
fn add_three_and_two() {
assert_eq!(5, add_two(3));
let result = add_two(3);
assert_eq!(result, 5);
}

#[test]
fn one_hundred() {
assert_eq!(102, add_two(100));
let result = add_two(100);
assert_eq!(result, 102);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub fn add_two(a: i32) -> i32 {
pub fn add_two(a: usize) -> usize {
internal_adder(a, 2)
}

fn internal_adder(a: i32, b: i32) -> i32 {
a + b
fn internal_adder(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
Expand All @@ -12,6 +12,7 @@ mod tests {

#[test]
fn internal() {
assert_eq!(4, internal_adder(2, 2));
let result = internal_adder(2, 2);
assert_eq!(result, 4);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub fn add_two(a: i32) -> i32 {
pub fn add_two(a: usize) -> usize {
internal_adder(a, 2)
}

fn internal_adder(a: i32, b: i32) -> i32 {
a + b
fn internal_adder(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
Expand All @@ -12,6 +12,7 @@ mod tests {

#[test]
fn internal() {
assert_eq!(4, internal_adder(2, 2));
let result = internal_adder(2, 2);
assert_eq!(result, 4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ use adder::add_two;

#[test]
fn it_adds_two() {
assert_eq!(4, add_two(2));
let result = add_two(2);
assert_eq!(result, 4);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test tests::it_adds_two ... FAILED
failures:

---- tests::it_adds_two stdout ----
thread 'tests::it_adds_two' panicked at src/lib.rs:11:9:
thread 'tests::it_adds_two' panicked at src/lib.rs:12:9:
assertion `left == right` failed
left: 5
right: 4
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ANCHOR: here
pub fn add_two(a: i32) -> i32 {
pub fn add_two(a: usize) -> usize {
a + 3
}
// ANCHOR_END: here
Expand All @@ -10,6 +10,7 @@ mod tests {

#[test]
fn it_adds_two() {
assert_eq!(add_two(2), 4);
let result = add_two(2);
assert_eq!(result, 4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ mod tests {
let result = greeting("Carol");
assert!(
result.contains("Carol"),
"Greeting did not contain name, value was `{}`",
result
"Greeting did not contain name, value was `{result}`"
);
}
// ANCHOR_END: here
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

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

// ANCHOR: here
#[test]
fn it_works() -> Result<(), String> {
if 2 + 2 == 4 {
let result = add(2, 2);

if result == 4 {
Ok(())
} else {
Err(String::from("two plus two does not equal four"))
}
}
// ANCHOR_END: here
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ $ cargo test
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)

running 2 tests
test expensive_test ... ignored
test it_works ... ok
test tests::expensive_test ... ignored
test tests::it_works ... ok

test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in 0.00s

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[test]
#[ignore]
fn expensive_test() {
// code that takes an hour to run
// ANCHOR: here
#[cfg(test)]
mod tests {
use super::*;

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

#[test]
#[ignore]
fn expensive_test() {
// code that takes an hour to run
}
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub fn add_two(a: i32) -> i32 {
pub fn add_two(a: usize) -> usize {
internal_adder(a, 2)
}

fn internal_adder(a: i32, b: i32) -> i32 {
a + b
fn internal_adder(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
Expand All @@ -12,6 +12,7 @@ mod tests {

#[test]
fn internal() {
assert_eq!(4, internal_adder(2, 2));
let result = internal_adder(2, 2);
assert_eq!(result, 4);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use adder;
use adder::add_two;

#[test]
fn it_adds_two() {
assert_eq!(4, adder::add_two(2));
let result = add_two(2);
assert_eq!(result, 4);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub fn add_two(a: i32) -> i32 {
pub fn add_two(a: usize) -> usize {
internal_adder(a, 2)
}

fn internal_adder(a: i32, b: i32) -> i32 {
a + b
fn internal_adder(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
Expand All @@ -12,6 +12,7 @@ mod tests {

#[test]
fn internal() {
assert_eq!(4, internal_adder(2, 2));
let result = internal_adder(2, 2);
assert_eq!(result, 4);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use adder;
use adder::add_two;

mod common;

#[test]
fn it_adds_two() {
common::setup();
assert_eq!(4, adder::add_two(2));

let result = add_two(2);
assert_eq!(result, 4);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub fn add_two(a: i32) -> i32 {
pub fn add_two(a: usize) -> usize {
internal_adder(a, 2)
}

fn internal_adder(a: i32, b: i32) -> i32 {
a + b
fn internal_adder(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
Expand All @@ -12,6 +12,7 @@ mod tests {

#[test]
fn internal() {
assert_eq!(4, internal_adder(2, 2));
let result = internal_adder(2, 2);
assert_eq!(result, 4);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use adder;
use adder::add_two;

#[test]
fn it_adds_two() {
assert_eq!(4, adder::add_two(2));
let result = add_two(2);
assert_eq!(result, 4);
}
Loading