從你的GitHub庫,我發現這個測試的活動指示器類:
should 'work correctly' do
ostream = StringIO.new
thread = Thread.new { sleep 1 }
throbber = Throbber.new(ostream, thread)
thread.join
throbber.join
assert_equal " \b-\b\\\b|\b/\b", ostream.string
end
我假設一個活動指示器迭代['-', '\', '|', '/']
,退格每次寫之前,每秒一次。請看下面的測試:
should 'work correctly' do
ostream = StringIO.new
started_at = Time.now
ended_at = nil
thread = Thread.new { sleep 1; ended_at = Time.now }
throbber = Throbber.new(ostream, thread)
thread.join
throbber.join
duration = ended_at - started_at
iterated_chars = " -\\|/"
expected = ""
if duration >= 1
# After n seconds we should have n copies of " -\\|/", excluding \b for now
expected << iterated_chars * duration.to_i
end
# Next append the characters we'd get from working for fractions of a second:
remainder = duration - duration.to_i
expected << iterated_chars[0..((iterated_chars.length*remainder).to_i)] if remainder > 0.0
expected = expected.split('').join("\b") + "\b"
assert_equal expected, ostream.string
end
的expected
最後的分配是有點不愉快,但我的假設是,活動指示器會寫字符/退格對原子。如果不是這樣,您應該能夠將\ b轉義序列插入到iterated_chars字符串中,並完全刪除最後一個分配。
聽起來不錯,我會檢查一下。 – Koraktor 2009-11-17 21:44:17