/
helpers.rb
39 lines (31 loc) · 966 Bytes
/
helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module RSpec
module Core
module Formatters
module Helpers
SUB_SECOND_PRECISION = 5
DEFAULT_PRECISION = 2
def format_duration(duration)
if duration > 60
minutes = duration.to_i / 60
seconds = duration - minutes * 60
"#{pluralize(minutes, 'minute')} #{format_seconds(seconds)} seconds"
else
"#{format_seconds(duration)} seconds"
end
end
def format_seconds(float)
precision ||= (float < 1) ? SUB_SECOND_PRECISION : DEFAULT_PRECISION
formatted = sprintf("%.#{precision}f", float)
strip_trailing_zeroes(formatted)
end
def strip_trailing_zeroes(string)
stripped = string.sub(/[^1-9]+$/, '')
stripped.empty? ? "0" : stripped
end
def pluralize(count, string)
"#{count} #{string}#{'s' unless count == 1}"
end
end
end
end
end