diff options
author | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2012-01-11 18:56:14 +0000 |
---|---|---|
committer | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2012-01-12 14:28:42 +0000 |
commit | 53096912571b5218452b8490cf7b04e9ac8e5a15 (patch) | |
tree | f0b62ed57536fe4dcfee49b1803678f6da977e30 /morphlib/stopwatch_tests.py | |
parent | 718a928de8e2e8e0744879c991d63cedf8c92aba (diff) | |
download | morph-53096912571b5218452b8490cf7b04e9ac8e5a15.tar.gz |
make a Stopwatch usable with the "with" statement
This simplifies its use. Previously:
w.start('foo')
...
w.stop('foo')
Now:
with w('foo'):
...
With "with", it is immediately clear from the code,
and its indentation, what is being measured. With
the old code, you have to hunt for the start and stop
method calls.
Diffstat (limited to 'morphlib/stopwatch_tests.py')
-rw-r--r-- | morphlib/stopwatch_tests.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/morphlib/stopwatch_tests.py b/morphlib/stopwatch_tests.py index 1a899f41..d4f1e3dd 100644 --- a/morphlib/stopwatch_tests.py +++ b/morphlib/stopwatch_tests.py @@ -56,3 +56,20 @@ class StopwatchTests(unittest.TestCase): self.assertEqual(our_delta, watch_delta) assert self.stopwatch.start_stop_seconds('start-stop') > 0 + + def test_with(self): + with self.stopwatch('foo'): + pass + self.assert_(self.stopwatch.start_stop_seconds('foo') < 1.0) + + def test_with_within_with(self): + with self.stopwatch('foo'): + with self.stopwatch('bar'): + pass + self.assert_(self.stopwatch.start_time('foo')) + self.assert_(self.stopwatch.stop_time('foo')) + self.assert_(self.stopwatch.start_time('bar')) + self.assert_(self.stopwatch.stop_time('bar')) + self.assert_(self.stopwatch.start_stop_seconds('foo') < 1.0) + self.assert_(self.stopwatch.start_stop_seconds('bar') < 1.0) + |