Module: RSpec::Core::Formatters::Helpers
- Includes:
- BacktraceFormatter
- 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
-
#format_duration(duration) ⇒ String
private
Formats seconds into a human-readable string.
-
#format_seconds(float, precision = nil) ⇒ String
private
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.
-
#pluralize(count, string) ⇒ String
private
Pluralize a word based on a count.
-
#strip_trailing_zeroes(string) ⇒ String
private
Remove trailing zeros from a string.
Methods included from BacktraceFormatter
Instance 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.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rspec/core/formatters/helpers.rb', line 45 def format_duration(duration) precision = case when duration < 1; SUB_SECOND_PRECISION when duration < 120; DEFAULT_PRECISION when duration < 300; 1 else 0 end if duration > 60 minutes = (duration.to_i / 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.
79 80 81 82 83 |
# File 'lib/rspec/core/formatters/helpers.rb', line 79 def format_seconds(float, precision = nil) precision ||= (float < 1) ? SUB_SECOND_PRECISION : DEFAULT_PRECISION formatted = sprintf("%.#{precision}f", float) strip_trailing_zeroes(formatted) 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.
103 104 105 |
# File 'lib/rspec/core/formatters/helpers.rb', line 103 def pluralize(count, string) "#{count} #{string}#{'s' unless count.to_f == 1}" end |
#strip_trailing_zeroes(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.
Remove trailing zeros from a string.
91 92 93 94 |
# File 'lib/rspec/core/formatters/helpers.rb', line 91 def strip_trailing_zeroes(string) stripped = string.sub(/[^1-9]+$/, '') stripped.empty? ? "0" : stripped end |