Skip to content

Commit

Permalink
Increase consistency with what cargo new --lib generates
Browse files Browse the repository at this point in the history
  • Loading branch information
carols10cents committed Jul 6, 2024
1 parent d5670bf commit 024ee95
Show file tree
Hide file tree
Showing 22 changed files with 128 additions and 85 deletions.
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

0 comments on commit 024ee95

Please sign in to comment.