Module: RSpec::Core::Formatters::Helpers

Included in:
BaseFormatter
Defined in:
lib/rspec/core/formatters/helpers.rb

Constant Summary collapse

SUB_SECOND_PRECISION =
5
DEFAULT_PRECISION =
2

Instance Method Summary collapse

Instance Method Details

#format_duration(duration) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/rspec/core/formatters/helpers.rb', line 9

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

#format_seconds(float) ⇒ Object



20
21
22
23
24
# File 'lib/rspec/core/formatters/helpers.rb', line 20

def format_seconds(float)
  precision ||= (float < 1) ? SUB_SECOND_PRECISION : DEFAULT_PRECISION
  formatted = sprintf("%.#{precision}f", float)
  strip_trailing_zeroes(formatted)
end

#pluralize(count, string) ⇒ Object



31
32
33
# File 'lib/rspec/core/formatters/helpers.rb', line 31

def pluralize(count, string)
  "#{count} #{string}#{'s' unless count == 1}"
end

#strip_trailing_zeroes(string) ⇒ Object



26
27
28
29
# File 'lib/rspec/core/formatters/helpers.rb', line 26

def strip_trailing_zeroes(string)
  stripped = string.sub(/[^1-9]+$/, '')
  stripped.empty? ? "0" : stripped
end