summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-11-writing-automated-tests/adder/src/lib.rs
blob: 55c024c173f379a47c15c27b50228d103ccd8ecd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

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

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

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

    #[test]
    fn exception() {
        let result = 2+2;
        assert_eq!(result, 4);
    }
    #[test]
    fn another() {
        println!("this one will fail");
    }

    #[test]
    fn hold() {
        let larger = Rectangle {
            width: 7,
            height: 8,
        };
        
        let smaller = Rectangle {
            width: 5,
            height: 4,
        };

        assert!(larger.can_hold(&smaller));
    }

    #[test]
    fn greeting_contain_names() {
        let result = greeting("caol");
        assert!(result.contains("carol"), "gretting did not contain name, the value is {}", result);
    }
}