Mercurial > lbo > hg > time_test
changeset 2:c21d8b7b61cb
Feature: Name timers to allow for several timers per case.
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 02 Sep 2017 19:52:31 +0200 |
parents | d4a954e8992a |
children | 09cdb220b85c |
files | Cargo.toml example/src/example.rs src/lib.rs |
diffstat | 3 files changed, 49 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/Cargo.toml Sat Sep 02 19:39:59 2017 +0200 +++ b/Cargo.toml Sat Sep 02 19:52:31 2017 +0200 @@ -1,6 +1,6 @@ [package] name = "time-test" -version = "0.1.0" +version = "0.2.0" authors = ["Lewin Bormann <lewin@lewin-bormann.info>"] license = "MIT" description = "Measure how long your test cases take with one simple macro."
--- a/example/src/example.rs Sat Sep 02 19:39:59 2017 +0200 +++ b/example/src/example.rs Sat Sep 02 19:52:31 2017 +0200 @@ -7,4 +7,38 @@ assert!(true); assert_eq!(1, 1); } + + #[test] + fn my_test_with_initialization() { + // Do possibly expensive preparation before. + let a = 1; + let b = 2; + + time_test!(); + + assert!(true); + assert_eq!(1, 1); + } + + #[test] + fn my_test_with_multiple_timers() { + time_test!(); + + { + time_test!("all"); + { + // sub-test 1 + time_test!("sub1"); + assert_eq!(1, 1); + } + + { + // sub-test 2 + time_test!("sub2"); + assert!(true); + } + } + + // The order of timers will be sub1, sub2, all, <unnamed>. + } }
--- a/src/lib.rs Sat Sep 02 19:39:59 2017 +0200 +++ b/src/lib.rs Sat Sep 02 19:52:31 2017 +0200 @@ -3,17 +3,22 @@ extern crate time; /// TestTimer allows to easily time tests. It's recommended to use the time_test!() macro. -pub struct TestTimer(time::Timespec); +pub struct TestTimer(time::Timespec, String); impl TestTimer { - pub fn new() -> TestTimer { - TestTimer(time::get_time()) + pub fn new(desc: String) -> TestTimer { + TestTimer(time::get_time(), desc) } } impl Drop for TestTimer { fn drop(&mut self) { - write!(io::stderr(), "(took {}) ", time::get_time() - self.0).is_ok(); + let dur = time::get_time() - self.0; + if self.1.is_empty() { + write!(io::stderr(), "(took {}) ", dur).is_ok(); + } else { + write!(io::stderr(), "({} took {}) ", self.1, dur).is_ok(); + } } } @@ -21,6 +26,10 @@ macro_rules! time_test { () => { use time_test::TestTimer; - let _tt = TestTimer::new(); + let _tt = TestTimer::new(String::new()); + }; + ($desc:expr) => { + use time_test::TestTimer; + let _tt = TestTimer::new($desc.to_string()); } }