You've already forked book-exercises
finish testing
- convert project to a library by moving program logic into lib.rs - create ./tests to store integration tests - create ./common to store code we want shared across integration tests
This commit is contained in:
175
testing/src/lib.rs
Normal file
175
testing/src/lib.rs
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
pub fn print_operations(a: &i32, b: &i32) {
|
||||||
|
let sum = ops::add(&a, &b);
|
||||||
|
let difference = ops::sub(&a, &b);
|
||||||
|
let product = ops::mul(&a, &b);
|
||||||
|
let quotient = ops::div(&a, &b);
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"
|
||||||
|
a is {a} and b is {b} and
|
||||||
|
sum = {sum}
|
||||||
|
difference = {difference}
|
||||||
|
product = {product}
|
||||||
|
quotient = {quotient}
|
||||||
|
"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod ops {
|
||||||
|
pub fn add(a: &i32, b: &i32) -> i32 {
|
||||||
|
*a + *b
|
||||||
|
}
|
||||||
|
pub fn sub(a: &i32, b: &i32) -> i32 {
|
||||||
|
*a - *b
|
||||||
|
}
|
||||||
|
pub fn mul(a: &i32, b: &i32) -> i32 {
|
||||||
|
*a * *b
|
||||||
|
}
|
||||||
|
pub fn div(a: &i32, b: &i32) -> i32 {
|
||||||
|
*a / *b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod users {
|
||||||
|
|
||||||
|
pub fn admin_checker(user: &str) -> bool {
|
||||||
|
if *user == "admin".to_string() {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
panic!("bruh moment");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn john_checker(user: &str) -> Result<bool, String> {
|
||||||
|
if *user == "john".to_string() {
|
||||||
|
Ok(true)
|
||||||
|
} else {
|
||||||
|
Err("panicking because you're not john!".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod panic_tests {
|
||||||
|
|
||||||
|
use crate::users::{admin_checker, john_checker};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic(expected = "bruh")]
|
||||||
|
fn is_admin() {
|
||||||
|
let user = "notadmin";
|
||||||
|
admin_checker(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn is_john() -> Result<(), String> {
|
||||||
|
let user = "john";
|
||||||
|
let result = john_checker(user);
|
||||||
|
|
||||||
|
match result {
|
||||||
|
std::result::Result::Ok(true) => Ok(()),
|
||||||
|
std::result::Result::Ok(false) => {
|
||||||
|
Err("is_john test failed because it's someone else!".to_string())
|
||||||
|
}
|
||||||
|
Err(_) => Err("is_john test failed because it's someone else!".to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests_returning_results {
|
||||||
|
use crate::ops::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_add() -> Result<(), String> {
|
||||||
|
let a = 12;
|
||||||
|
let b = 14;
|
||||||
|
let result = add(&a, &b);
|
||||||
|
if result == 26 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err("12 + 14 should be 26 but it's not".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sub() -> Result<(), String> {
|
||||||
|
let a = 12;
|
||||||
|
let b = 14;
|
||||||
|
let result = sub(&a, &b);
|
||||||
|
|
||||||
|
if result == -2 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err("error calculating difference".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn test_mul() -> Result<(), String> {
|
||||||
|
let a = 14;
|
||||||
|
let b = 12;
|
||||||
|
let result = mul(&a, &b);
|
||||||
|
|
||||||
|
if result == 168 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err("error calculating product".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn test_div() -> Result<(), String> {
|
||||||
|
let a = 14;
|
||||||
|
let b = 12;
|
||||||
|
let result = div(&a, &b);
|
||||||
|
|
||||||
|
if result == 1 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err("error calculating quotient".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests_returning_bool {
|
||||||
|
|
||||||
|
use crate::ops::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_add() {
|
||||||
|
let a = 12;
|
||||||
|
let b = 14;
|
||||||
|
let result = add(&a, &b);
|
||||||
|
assert_eq!(result, a + b, "12 + 14 should be 26 but it's not");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sub() {
|
||||||
|
let a = 12;
|
||||||
|
let b = 14;
|
||||||
|
let result = sub(&a, &b);
|
||||||
|
assert_eq!(result, a - b);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn test_mul() {
|
||||||
|
let a = 12;
|
||||||
|
let b = 14;
|
||||||
|
let result = mul(&a, &b);
|
||||||
|
assert_eq!(result, a * b);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn test_div() {
|
||||||
|
let a = 12;
|
||||||
|
let b = 14;
|
||||||
|
let result = div(&a, &b);
|
||||||
|
assert_eq!(result, a / b);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
use crate::users::{admin_checker, john_checker};
|
use testing::print_operations;
|
||||||
|
use testing::users::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let a = 14;
|
let a = 14;
|
||||||
@@ -16,179 +17,3 @@ fn main() {
|
|||||||
println!("hi john!");
|
println!("hi john!");
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_operations(a: &i32, b: &i32) {
|
|
||||||
let sum = ops::add(&a, &b);
|
|
||||||
let difference = ops::sub(&a, &b);
|
|
||||||
let product = ops::mul(&a, &b);
|
|
||||||
let quotient = ops::div(&a, &b);
|
|
||||||
|
|
||||||
println!(
|
|
||||||
"
|
|
||||||
a is {a} and b is {b} and
|
|
||||||
sum = {sum}
|
|
||||||
difference = {difference}
|
|
||||||
product = {product}
|
|
||||||
quotient = {quotient}
|
|
||||||
"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
mod ops {
|
|
||||||
pub fn add(a: &i32, b: &i32) -> i32 {
|
|
||||||
*a + *b
|
|
||||||
}
|
|
||||||
pub fn sub(a: &i32, b: &i32) -> i32 {
|
|
||||||
*a - *b
|
|
||||||
}
|
|
||||||
pub fn mul(a: &i32, b: &i32) -> i32 {
|
|
||||||
*a * *b
|
|
||||||
}
|
|
||||||
pub fn div(a: &i32, b: &i32) -> i32 {
|
|
||||||
*a / *b
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod users {
|
|
||||||
|
|
||||||
pub fn admin_checker(user: &str) -> bool {
|
|
||||||
if *user == "admin".to_string() {
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
panic!("bruh moment");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn john_checker(user: &str) -> Result<bool, String> {
|
|
||||||
if *user == "john".to_string() {
|
|
||||||
Ok(true)
|
|
||||||
} else {
|
|
||||||
Err("panicking because you're not john!".to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod panic_tests {
|
|
||||||
|
|
||||||
use crate::users::{admin_checker, john_checker};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[should_panic(expected = "bruh")]
|
|
||||||
fn is_admin() {
|
|
||||||
let user = "notadmin";
|
|
||||||
admin_checker(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn is_john() -> Result<(), String> {
|
|
||||||
let user = "john";
|
|
||||||
let result = john_checker(user);
|
|
||||||
|
|
||||||
match result {
|
|
||||||
std::result::Result::Ok(true) => Ok(()),
|
|
||||||
std::result::Result::Ok(false) => {
|
|
||||||
Err("is_john test failed because it's someone else!".to_string())
|
|
||||||
}
|
|
||||||
Err(_) => Err("is_john test failed because it's someone else!".to_string()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests_returning_results {
|
|
||||||
use crate::ops::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_add() -> Result<(), String> {
|
|
||||||
let a = 12;
|
|
||||||
let b = 14;
|
|
||||||
let result = add(&a, &b);
|
|
||||||
if result == 26 {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err("12 + 14 should be 26 but it's not".to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_sub() -> Result<(), String> {
|
|
||||||
let a = 12;
|
|
||||||
let b = 14;
|
|
||||||
let result = sub(&a, &b);
|
|
||||||
|
|
||||||
if result == -2 {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err("error calculating difference".to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[ignore]
|
|
||||||
fn test_mul() -> Result<(), String> {
|
|
||||||
let a = 14;
|
|
||||||
let b = 12;
|
|
||||||
let result = mul(&a, &b);
|
|
||||||
|
|
||||||
if result == 168 {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err("error calculating product".to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[ignore]
|
|
||||||
fn test_div() -> Result<(), String> {
|
|
||||||
let a = 14;
|
|
||||||
let b = 12;
|
|
||||||
let result = div(&a, &b);
|
|
||||||
|
|
||||||
if result == 1 {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err("error calculating quotient".to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests_returning_bool {
|
|
||||||
|
|
||||||
use crate::ops::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_add() {
|
|
||||||
let a = 12;
|
|
||||||
let b = 14;
|
|
||||||
let result = add(&a, &b);
|
|
||||||
assert_eq!(result, a + b, "12 + 14 should be 26 but it's not");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_sub() {
|
|
||||||
let a = 12;
|
|
||||||
let b = 14;
|
|
||||||
let result = sub(&a, &b);
|
|
||||||
assert_eq!(result, a - b);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[ignore]
|
|
||||||
fn test_mul() {
|
|
||||||
let a = 12;
|
|
||||||
let b = 14;
|
|
||||||
let result = mul(&a, &b);
|
|
||||||
assert_eq!(result, a * b);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[ignore]
|
|
||||||
fn test_div() {
|
|
||||||
let a = 12;
|
|
||||||
let b = 14;
|
|
||||||
let result = div(&a, &b);
|
|
||||||
assert_eq!(result, a / b);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
3
testing/tests/common/mod.rs
Normal file
3
testing/tests/common/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub fn give_me_5() -> usize {
|
||||||
|
5
|
||||||
|
}
|
||||||
24
testing/tests/integration_tests.rs
Normal file
24
testing/tests/integration_tests.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
use testing::ops::*;
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn subtract_and_divide() {
|
||||||
|
let a = 20;
|
||||||
|
let b = 2;
|
||||||
|
let c = 3;
|
||||||
|
|
||||||
|
let difference = sub(&a, &b);
|
||||||
|
let quotient = div(&difference, &c);
|
||||||
|
assert_eq!(quotient, 6);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn add_and_multiply() {
|
||||||
|
let a = 10;
|
||||||
|
let b = common::give_me_5() as i32;
|
||||||
|
let c = 3;
|
||||||
|
|
||||||
|
let sum = add(&a, &b);
|
||||||
|
let product = mul(&sum, &c);
|
||||||
|
assert_eq!(product, 45);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user