tbender_testing 0.1.0

Example of unit testing
Documentation
pub struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    pub fn can_hold(&self, other: &Rectangle) -> bool {
        self.width > other.width && self.height > other.height
    }
}

/// Adds two to the number given.
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = tbender_testing::add_two(arg);
/// assert_eq!(7, answer);
/// ```
pub fn add_two(a: i32) -> i32 {
    a + 2
}

pub fn greeting(name: &str) -> String {
    format!("Hello {}!", name)
    //String::from("Hello!")
}

pub struct Guess {
    value: i32,
}

impl Guess {
    pub fn new(value: i32) -> Guess {
        if value < 1 || value > 100 {
            panic!("Guess value must be between 1 and 100, got {}.", value);
        }
        Guess {
            value
        }
    }
}

pub fn prints_and_returns_10(a: i32) -> i32 {
    println!("I got the value {}", a);
    10
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }

    #[test]
    fn another() {
        // panic!("Make this test fail");
    }

    // NOTE: tests are just a submodule, so we need to bring the outer module into scope
    use super::*;

    #[test]
    fn larger_can_hold_smaller() {
        let larger = Rectangle{ width: 8, height: 7 };
        let smaller = Rectangle{ width: 5, height: 1 };
        assert!(larger.can_hold(&smaller));
    }

    #[test]
    fn smaller_cannot_hold_larger() {
        let larger = Rectangle{ width: 8, height: 7 };
        let smaller = Rectangle{ width: 5, height: 1 };
        assert!(!smaller.can_hold(&larger));
    }

    #[test]
    fn it_adds_two() {
        assert_eq!(4, add_two(2));
    }

    #[test]
    fn greeting_contains_name() {
        let result = greeting("Trevor");
        // The failure message doesn't give enough info
        // assert!(result.contains("Trevor"));
        assert!(
            result.contains("Trevor"),
            "Greeting did not contain name, was {}", result
        );
    }

    #[test]
    #[should_panic(expected = "Guess value must be between 1 and 100, got 101.")]
    fn greater_than_100() {
        Guess::new(101);
    }

    #[test]
    fn result_type() -> Result<(), String> {
        if 2 + 2 == 4 {
            Ok(())
        } else {
            Err(String::from("two plus two does not equal four?"))
        }
    }

    #[test]
    fn passing() {
        let value = prints_and_returns_10(4);
        assert_eq!(10, value);
    }

    #[test]
    #[ignore] // intentionally ignore failing test
    fn failing() {
        let value = prints_and_returns_10(4);
        assert_eq!(5, value);
    }

    #[test]
    #[ignore]
    fn expensive_test() {
    }
}