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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
use std::borrow::Cow; use std::thread; #[derive(Clone)] pub enum TestResult<S: AsRef<str>> { Ok(S), Fail(S), } #[macro_export] macro_rules! assert_in { ($text:expr, $message:expr) => ({ match (&$text, &$message) { (text_val, message_val) => { if !text_val.contains(message_val) { panic!(r#"assertion failed: `text don't contain message` text: `{}`, message: `{}`"#, text_val, message_val) } } } }); ($message:expr, $expected:expr, ) => ( assert_in!($message, $expected) ); ($text:expr, $message:expr, $($arg:tt)+) => ({ match (&$text, &$message) { (text_val, message_val) => { if !text_val.contains(message_val) { panic!(r#"assertion failed: `text don't contain message` text: `{}`, message: `{}`: {}"#, text_val, message_val, format_args!($($arg)+)) } } } }); } #[macro_export] macro_rules! assert_not_in { ($text:expr, $message:expr) => ({ match (&$text, &$message) { (text_val, message_val) => { if text_val.contains(message_val) { panic!(r#"assertion failed: `text contains message` text: `{}`, message: `{}`"#, text_val, message_val) } } } }); ($message:expr, $expected:expr, ) => ( assert_not_in!($message, $expected) ); ($text:expr, $message:expr, $($arg:tt)+) => ({ match (&$text, &$message) { (text_val, message_val) => { if text_val.contains(message_val) { panic!(r#"assertion failed: `text contains message` text: `{}`, message: `{}`: {}"#, text_val, message_val, format_args!($($arg)+)) } } } }); } impl<S: AsRef<str>> TestResult<S> { pub fn is_fail(&self) -> bool { use self::TestResult::*; match *self { Fail(_) => true, _ => false, } } pub fn is_ok(&self) -> bool { use self::TestResult::*; match *self { Ok(_) => true, _ => false, } } pub fn name(&self) -> String { use self::TestResult::*; match self { &Ok(ref s) => s.as_ref().to_owned(), &Fail(ref s) => s.as_ref().to_owned(), } } pub fn msg(&self) -> &'static str { use self::TestResult::*; match *self { Ok(_) => "ok", Fail(_) => "FAILED", } } } #[derive(Default, Clone)] pub struct TestResults<S>(Vec<TestResult<S>>) where S: AsRef<str> + Clone; impl<S> TestResults<S> where S: AsRef<str> + Clone, { pub fn new() -> Self { TestResults(vec![]) } pub fn ok(self, name: S) -> Self { self.append(TestResult::Ok(name)) } pub fn fail(self, name: S) -> Self { self.append(TestResult::Fail(name)) } pub fn append(mut self, test: TestResult<S>) -> Self { self.0.push(test); self } pub fn assert(&self, output: ::std::process::Output) { let tests = &self.0; let (expected_code, msg) = if !self.should_fail() { (0, "Unexpected fails!") } else { (101, "Some test should fail!") }; assert_eq!( Some(expected_code), output.status.code(), "{}\n Console: \nOUT:\n{}\nERR:\n{}\n", msg, output.stdout.str(), output.stderr.str() ); let stderr = output.stderr.str(); let output = output.stdout.str(); if output.is_empty() { eprintln!("Stderr: {}", stderr); panic!("Empty stdout!"); } assert_in!(output, format!("running {} test", tests.len())); self.for_each(|t| assert_in!(output, format!("test {} ... {}", t.name(), t.msg()))); if self.should_fail() { assert_in!(output, format!("failures:")); } self.for_each_failed(|t| assert_in!(output, format!(" {}", t.name()))); } fn should_fail(&self) -> bool { self.0.iter().filter(|r| r.is_fail()).next().is_some() } fn for_each<F: FnMut(&TestResult<S>) -> ()>(&self, action: F) { self.0.iter().for_each(action) } fn for_each_failed<F: FnMut(&TestResult<S>) -> ()>(&self, action: F) { self.0.iter().filter(|r| r.is_fail()).for_each(action) } } pub trait Stringable { fn str(&self) -> Cow<str>; } impl<B: AsRef<[u8]>> Stringable for B { fn str(&self) -> Cow<str> { String::from_utf8_lossy(self.as_ref()) } } pub fn testname() -> String { thread::current().name().unwrap().to_string() } pub trait CountMessageOccurrence { fn count<S: AsRef<str>>(&self, message: S) -> usize; } impl<ST> CountMessageOccurrence for ST where ST: AsRef<str>, { fn count<S: AsRef<str>>(&self, message: S) -> usize { self.as_ref() .lines() .filter(|line| line.contains(message.as_ref())) .count() } } #[cfg(test)] mod test { use super::*; #[test] fn should_count_message_occurence() { let foo_occurences = " foobaz bar foobazfoo baz foo " .count("foo"); assert_eq!(3, foo_occurences); } #[test] fn should_get_test_path() { use super::*; assert_eq!("utils::test::should_get_test_path", testname()); } } pub fn sanitize_name<S: AsRef<str>>(s: S) -> String { s.as_ref().replace(":", "_").replace("__", "_") }