lib.rs (1772B)
1 struct Rect { 2 w: u32, 3 h: u32, 4 } 5 6 impl Rect { 7 fn fit(&self, other: &Rect) -> bool { 8 self.w > other.w && self.h > other.w 9 } 10 } 11 12 pub fn greet(name: &str) -> String { 13 format!("Hail {}!", name) 14 } 15 16 pub fn less_than_ten(val: i32) { 17 if val > 10 { 18 panic!("Value is too large"); 19 } 20 } 21 22 pub fn add_two(a: i32) -> i32 { 23 add_internal(a, 2) 24 } 25 26 fn add_internal(a: i32, b: i32) -> i32 { 27 a + b 28 } 29 30 #[cfg(test)] 31 mod tests { 32 use super::*; 33 34 #[test] 35 fn internal_test() { 36 assert_eq!(4, add_internal(2, 2)); 37 } 38 39 #[test] 40 //expected filters for specific panic messages 41 #[should_panic(expected = "Value is too large")] 42 fn too_large() { 43 less_than_ten(11); 44 } 45 46 #[test] 47 fn exploration() -> Result<(), String> { 48 if 2 + 2 == 4 { 49 Ok(()) 50 } else { 51 Err("test failure".to_string()) 52 } 53 } 54 55 #[test] 56 #[ignore] 57 fn trollius() { 58 panic!("ert+576b"); 59 } 60 61 #[test] 62 fn larger_fits_smaller() { 63 let large = Rect { 64 w: 10, 65 h: 10, 66 }; 67 68 let small = Rect { 69 w:5, 70 h:5, 71 }; 72 73 assert!(large.fit(&small)); 74 } 75 76 #[test] 77 fn smaller_excludes_larger() { 78 let large = Rect { 79 w: 10, 80 h: 10, 81 }; 82 83 let small = Rect { 84 w:5, 85 h:5, 86 }; 87 88 assert!(!small.fit(&large)); 89 } 90 91 #[test] 92 fn equality() { 93 assert_eq!(4, 2+2); 94 assert_ne!(4, 2+3); 95 } 96 97 #[test] 98 fn has_name() { 99 let result = greet("Quintus"); 100 assert!(result.contains("Quintus"), 101 "Greeting did not contain name; result={}", 102 result 103 ); 104 } 105 }