Sha256: c5478d91bfba5fc296ec2daef8c75a349f68189872af465b2a79f5b50e6beae4

Contents?: true

Size: 1.51 KB

Versions: 10

Compression:

Stored size: 1.51 KB

Contents

describe Nanoc::Telemetry::Stopwatch do
  subject(:stopwatch) { described_class.new }

  after { Timecop.return }

  it 'is zero by default' do
    expect(stopwatch.duration).to eq(0.0)
  end

  # TODO: if running, raise error when asking for #duration

  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))
    stopwatch.stop

    expect(stopwatch.duration).to eq(1.0)
  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))
    stopwatch.stop

    Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 3))
    stopwatch.start

    Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 6))
    stopwatch.stop

    expect(stopwatch.duration).to eq(1.0 + 3.0)
  end

  it 'errors when stopping when not started' do
    expect { stopwatch.stop }.to raise_error(Nanoc::Telemetry::Stopwatch::NotRunningError)
  end

  it 'errors when starting when already started' do
    stopwatch.start
    expect { stopwatch.start }.to raise_error(Nanoc::Telemetry::Stopwatch::AlreadyRunningError)
  end

  it 'reports running status' do
    expect(stopwatch).not_to be_running
    expect(stopwatch).to be_stopped

    stopwatch.start

    expect(stopwatch).to be_running
    expect(stopwatch).not_to be_stopped

    stopwatch.stop

    expect(stopwatch).not_to be_running
    expect(stopwatch).to be_stopped
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
nanoc-4.7.9 spec/nanoc/telemetry/stopwatch_spec.rb
nanoc-4.7.8 spec/nanoc/telemetry/stopwatch_spec.rb
nanoc-4.7.7 spec/nanoc/telemetry/stopwatch_spec.rb
nanoc-4.7.6 spec/nanoc/telemetry/stopwatch_spec.rb
nanoc-4.7.5 spec/nanoc/telemetry/stopwatch_spec.rb
nanoc-4.7.4 spec/nanoc/telemetry/stopwatch_spec.rb
nanoc-4.7.3 spec/nanoc/telemetry/stopwatch_spec.rb
nanoc-4.7.2 spec/nanoc/telemetry/stopwatch_spec.rb
nanoc-4.7.1 spec/nanoc/telemetry/stopwatch_spec.rb
nanoc-4.7.0 spec/nanoc/telemetry/stopwatch_spec.rb