109 lines
3.0 KiB
Ruby
109 lines
3.0 KiB
Ruby
# typed: false
|
|
require "rails_helper"
|
|
|
|
RSpec.describe Stopwatch do
|
|
describe ".start" do
|
|
it "creates a new stopwatch with current time as start time" do
|
|
before_time = Time.current
|
|
stopwatch = Stopwatch.start
|
|
after_time = Time.current
|
|
|
|
expect(stopwatch.start_time).to be_between(before_time, after_time)
|
|
end
|
|
end
|
|
|
|
describe "#elapsed" do
|
|
it "calculates elapsed time correctly" do
|
|
start_time = Time.parse("2023-01-01 10:00:00")
|
|
stopwatch = Stopwatch.new(start_time)
|
|
|
|
# Mock Time.current for this specific stopwatch instance
|
|
allow(stopwatch).to receive(:elapsed) { 2.5 }
|
|
|
|
expect(stopwatch.elapsed).to eq(2.5)
|
|
end
|
|
|
|
it "returns non-negative elapsed time" do
|
|
stopwatch = Stopwatch.start
|
|
expect(stopwatch.elapsed).to be >= 0
|
|
end
|
|
end
|
|
|
|
describe "#elapsed_ms" do
|
|
it "formats milliseconds correctly" do
|
|
stopwatch = Stopwatch.start
|
|
allow(stopwatch).to receive(:elapsed) { 1.2345 }
|
|
|
|
expect(stopwatch.elapsed_ms).to eq("1234.5ms")
|
|
end
|
|
|
|
it "handles very small times" do
|
|
stopwatch = Stopwatch.start
|
|
allow(stopwatch).to receive(:elapsed) { 0.001 }
|
|
|
|
expect(stopwatch.elapsed_ms).to eq("1.0ms")
|
|
end
|
|
end
|
|
|
|
describe "#elapsed_s" do
|
|
it "formats seconds with 3 decimal places" do
|
|
stopwatch = Stopwatch.start
|
|
allow(stopwatch).to receive(:elapsed) { 1.2345 }
|
|
|
|
expect(stopwatch.elapsed_s).to eq("1.234s")
|
|
end
|
|
|
|
it "formats small times to 3 decimal places" do
|
|
stopwatch = Stopwatch.start
|
|
allow(stopwatch).to receive(:elapsed) { 0.001234 }
|
|
|
|
expect(stopwatch.elapsed_s).to eq("0.001s")
|
|
end
|
|
|
|
it "formats zero time correctly" do
|
|
stopwatch = Stopwatch.start
|
|
allow(stopwatch).to receive(:elapsed) { 0.0 }
|
|
|
|
expect(stopwatch.elapsed_s).to eq("0.000s")
|
|
end
|
|
end
|
|
|
|
describe "#start_time" do
|
|
it "returns the time when the stopwatch was started" do
|
|
start_time = Time.parse("2023-01-01 10:00:00")
|
|
stopwatch = Stopwatch.new(start_time)
|
|
|
|
expect(stopwatch.start_time).to eq(start_time)
|
|
end
|
|
end
|
|
|
|
describe "practical usage" do
|
|
it "provides the expected API usage pattern" do
|
|
# Test the exact usage pattern the user requested
|
|
download_time = Stopwatch.start
|
|
|
|
# The elapsed method should return a float representing seconds
|
|
duration = download_time.elapsed
|
|
expect(duration).to be_a(Float)
|
|
expect(duration).to be >= 0
|
|
|
|
# Should provide convenient formatting methods
|
|
expect(download_time.elapsed_s).to match(/^\d+\.\d{3}s$/)
|
|
expect(download_time.elapsed_ms).to match(/^\d+\.\d+ms$/)
|
|
end
|
|
|
|
it "can be used for timing operations" do
|
|
# Simulate timing an operation
|
|
operation_timer = Stopwatch.start
|
|
|
|
# Simulate work (just check the timing interface works)
|
|
# In real usage: do_some_work()
|
|
sleep(0.001) # Minimal sleep to ensure some time passes
|
|
|
|
execution_time = operation_timer.elapsed
|
|
expect(execution_time).to be > 0
|
|
expect(execution_time).to be < 1 # Should be very fast
|
|
end
|
|
end
|
|
end
|