从你的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