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

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

Overview

Formatters helpers.

Class Method Summary collapse

Class Method Details

.format_duration(duration) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Formats seconds into a human-readable string.

Examples:

format_duration(1) #=>  "1 minute 1 second"
format_duration(135.14) #=> "2 minutes 15.14 seconds"

Parameters:

  • duration (Float, Fixnum)

    in seconds

Returns:

  • (String)

    human-readable time



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rspec/core/formatters/helpers.rb', line 24

def self.format_duration(duration)
  precision = case
              when duration < 1 then    SUB_SECOND_PRECISION
              when duration < 120 then  DEFAULT_PRECISION
              when duration < 300 then  1
              else                  0
              end

  if duration > 60
    minutes = (duration.round / 60).to_i
    seconds = (duration - minutes * 60)

    "#{pluralize(minutes, 'minute')} #{pluralize(format_seconds(seconds, precision), 'second')}"
  else
    pluralize(format_seconds(duration, precision), 'second')
  end
end

.format_seconds(float, precision = nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Formats seconds to have 5 digits of precision with trailing zeros removed if the number is less than 1 or with 2 digits of precision if the number is greater than zero.

The precision used is set in SUB_SECOND_PRECISION and DEFAULT_PRECISION.

Examples:

format_seconds(0.000006) #=> "0.00001"
format_seconds(0.020000) #=> "0.02"
format_seconds(1.00000000001) #=> "1"

Parameters:

  • float (Float)

Returns:

  • (String)

    formatted float

See Also:

  • #strip_trailing_zeroes


60
61
62
63
64
65
# File 'lib/rspec/core/formatters/helpers.rb', line 60

def self.format_seconds(float, precision=nil)
  return '0' if float < 0
  precision ||= (float < 1) ? SUB_SECOND_PRECISION : DEFAULT_PRECISION
  formatted = "%.#{precision}f" % float
  strip_trailing_zeroes(formatted)
end

.organize_ids(ids) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Given a list of example ids, organizes them into a compact, ordered list.



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rspec/core/formatters/helpers.rb', line 94

def self.organize_ids(ids)
  grouped = ids.inject(Hash.new { |h, k| h[k] = [] }) do |hash, id|
    file, id = Example.parse_id(id)
    hash[file] << id
    hash
  end

  grouped.sort_by(&:first).map do |file, grouped_ids|
    grouped_ids = grouped_ids.sort_by { |id| id.split(':').map(&:to_i) }
    id = Metadata.id_from(:rerun_file_path => file, :scoped_id => grouped_ids.join(','))
    ShellEscape.conditionally_quote(id)
  end
end

.pluralize(count, string) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Pluralize a word based on a count.

Parameters:

  • count (Fixnum)

    number of objects

  • string (String)

    word to be pluralized

Returns:

  • (String)

    pluralized word



88
89
90
# File 'lib/rspec/core/formatters/helpers.rb', line 88

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