spec/ddmetrics/stopwatch_spec.rb in ddmetrics-1.0.1 vs spec/ddmetrics/stopwatch_spec.rb in ddmetrics-1.1.0
- old
+ new
@@ -8,32 +8,54 @@
it 'is zero by default' do
expect(stopwatch.duration).to eq(0.0)
end
it 'records correct duration after start+stop' do
- Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 0))
stopwatch.start
-
- Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 1))
+ sleep 0.1
stopwatch.stop
- expect(stopwatch.duration).to eq(1.0)
+ expect(stopwatch.duration).to be_within(0.01).of(0.1)
end
it 'records correct duration after start+stop+start+stop' do
- Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 0))
stopwatch.start
-
- Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 1))
+ sleep 0.1
stopwatch.stop
-
- Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 3))
+ sleep 0.1
stopwatch.start
-
- Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 6))
+ sleep 0.1
stopwatch.stop
- expect(stopwatch.duration).to eq(1.0 + 3.0)
+ expect(stopwatch.duration).to be_within(0.02).of(0.2)
+ end
+
+ it 'records correct duration with single #run call' do
+ stopwatch.run do
+ sleep 0.1
+ end
+
+ expect(stopwatch.duration).to be_within(0.01).of(0.1)
+ end
+
+ it 'records correct duration with multiple #run calls' do
+ stopwatch.run do
+ sleep 0.1
+ end
+ stopwatch.run do
+ sleep 0.2
+ end
+
+ expect(stopwatch.duration).to be_within(0.03).of(0.3)
+ end
+
+ it 'returns the right value from a #run call' do
+ result = stopwatch.run do
+ sleep 0.1
+ 'donkey'
+ end
+
+ expect(result).to eq('donkey')
end
it 'errors when stopping when not started' do
expect { stopwatch.stop }
.to raise_error(