Module: RSpec::Core::Formatters::Helpers
Relationships & Source Files | |
Defined in: | rspec-core/lib/rspec/core/formatters/helpers.rb |
Overview
::RSpec::Core::Formatters
helpers.
Constant Summary
-
DEFAULT_PRECISION =
Internal use only
# File 'rspec-core/lib/rspec/core/formatters/helpers.rb', line 122
-
SUB_SECOND_PRECISION =
Internal use only
# File 'rspec-core/lib/rspec/core/formatters/helpers.rb', line 95
Class Method Summary
-
.format_duration(duration) ⇒ String
Internal use only
Internal use only
Formats seconds into a human-readable string.
-
.format_seconds(float, precision = nil) ⇒ String
Internal use only
Internal use only
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.
-
.organize_ids(ids)
Internal use only
Internal use only
Given a list of example ids, organizes them into a compact, ordered list.
-
.pluralize(count, string) ⇒ String
Internal use only
Internal use only
Pluralize a word based on a count.
-
.strip_trailing_zeroes(string) ⇒ String
private
Internal use only
Internal use only
Remove trailing zeros from a string.
Class Method Details
.format_duration(duration) ⇒ String
Formats seconds into a human-readable string.
# File 'rspec-core/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
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.
# File 'rspec-core/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)
Given a list of example ids, organizes them into a compact, ordered list.
# File 'rspec-core/lib/rspec/core/formatters/helpers.rb', line 102
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
Pluralize a word based on a count.
# File 'rspec-core/lib/rspec/core/formatters/helpers.rb', line 88
def self.pluralize(count, string) pluralized_string = if count.to_f == 1 string elsif string.end_with?('s') # e.g. "process" "#{string}es" # e.g. "processes" else "#{string}s" end "#{count} #{pluralized_string}" end
.strip_trailing_zeroes(string) ⇒ String
(private)
Remove trailing zeros from a string.
Only remove trailing zeros after a decimal place. see: rubular.com/r/ojtTydOgpn
# File 'rspec-core/lib/rspec/core/formatters/helpers.rb', line 76
def self.strip_trailing_zeroes(string) string.sub(/(?:(\..*[^0])0+|\.0+)$/, '\1') end