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

### Home Classes Methods

### In Files

- lib/rspec/core/formatters/helpers.rb

### Methods

- #format_duration
- #format_seconds
- #pluralize
- #strip_trailing_zeroes

### Included Modules

- RSpec::Core::BacktraceFormatter

### Class/Module Index

Quicksearch

- RSpec
- RSpec::Core
- RSpec::Core::BacktraceCleaner
- RSpec::Core::BacktraceFormatter
- RSpec::Core::CommandLine
- RSpec::Core::Configuration
- RSpec::Core::Configuration::MustBeConfiguredBeforeExampleGroupsError
- RSpec::Core::ConfigurationOptions
- RSpec::Core::ConstMissing
- RSpec::Core::DRbCommandLine
- RSpec::Core::DSL
- RSpec::Core::Deprecation
- RSpec::Core::DrbOptions
- RSpec::Core::Example
- RSpec::Core::Example::NotPendingExampleFixed
- RSpec::Core::Example::Procsy
- RSpec::Core::ExampleGroup
- RSpec::Core::Extensions
- RSpec::Core::Extensions::InstanceEvalWithArgs
- RSpec::Core::Extensions::ModuleEvalWithArgs
- RSpec::Core::Extensions::Ordered
- RSpec::Core::Extensions::Ordered::ExampleGroups
- RSpec::Core::Extensions::Ordered::Examples
- RSpec::Core::FilterManager
- RSpec::Core::FilterManager::BackwardCompatibility
- RSpec::Core::FilterManager::Describable
- RSpec::Core::Formatters
- RSpec::Core::Formatters::BaseFormatter
- RSpec::Core::Formatters::BaseTextFormatter
- RSpec::Core::Formatters::DeprecationFormatter
- RSpec::Core::Formatters::DocumentationFormatter
- RSpec::Core::Formatters::Helpers
- RSpec::Core::Formatters::HtmlFormatter
- RSpec::Core::Formatters::HtmlPrinter
- RSpec::Core::Formatters::JsonFormatter
- RSpec::Core::Formatters::ProgressFormatter
- RSpec::Core::Formatters::SnippetExtractor
- RSpec::Core::Formatters::SnippetExtractor::NullConverter
- RSpec::Core::Formatters::TextMateFormatter
- RSpec::Core::Hooks
- RSpec::Core::Hooks::AfterAllHook
- RSpec::Core::Hooks::AfterHook
- RSpec::Core::Hooks::AroundHook
- RSpec::Core::Hooks::AroundHookCollection
- RSpec::Core::Hooks::BeforeHook
- RSpec::Core::Hooks::GroupHookCollection
- RSpec::Core::Hooks::Hook
- RSpec::Core::Hooks::HookCollection
- RSpec::Core::Hooks::HookCollectionAliases
- RSpec::Core::Hooks::RegistersGlobals
- RSpec::Core::MemoizedHelpers
- RSpec::Core::MemoizedHelpers::BeforeAllMemoizedHash
- RSpec::Core::MemoizedHelpers::ClassMethods
- RSpec::Core::Metadata
- RSpec::Core::Metadata::ExampleMetadataHash
- RSpec::Core::Metadata::GroupMetadataHash
- RSpec::Core::Metadata::MetadataHash
- RSpec::Core::MetadataHashBuilder
- RSpec::Core::MetadataHashBuilder::Common
- RSpec::Core::MetadataHashBuilder::WithConfigWarning
- RSpec::Core::MetadataHashBuilder::WithDeprecationWarning
- RSpec::Core::MockFrameworkAdapter
- RSpec::Core::Parser
- RSpec::Core::Pending
- RSpec::Core::Pending::PendingDeclaredInExample
- RSpec::Core::Pending::PendingExampleFixedError
- RSpec::Core::ProjectInitializer
- RSpec::Core::RakeTask
- RSpec::Core::Reporter
- RSpec::Core::RubyProject
- RSpec::Core::Runner
- RSpec::Core::SharedContext
- RSpec::Core::SharedExampleGroup
- RSpec::Core::SharedExampleGroup::Collection
- RSpec::Core::SharedExampleGroup::Registry
- RSpec::Core::SharedExampleGroup::TopLevelDSL
- RSpec::Core::Time
- RSpec::Core::Version
- RSpec::Core::World
- RSpec::Rake
- RSpec::Runner
- Autotest
- Autotest::Rspec2
- Kernel

No matching classes.

# RSpec::Core::Formatters::Helpers

### Constants

DEFAULT_PRECISION
SUB_SECOND_PRECISION

### Public Instance Methods

format_duration(duration) click to toggle source

@api private

Formats seconds into a human-readable string.

@param [Float, Fixnum] duration in seconds @return [String] human-readable time

@example

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

    # 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
        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) click to toggle source

@api 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.

@param [Float] float @return [String] formatted float

@example

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

The precision used is set in {Helpers::SUB_SECOND_PRECISION} and {Helpers::DEFAULT_PRECISION}.

@see strip_trailing_zeroes

    # 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) click to toggle source

@api private

Pluralize a word based on a count.

@param [Fixnum] count number of objects @param [String] string word to be pluralized @return [String] pluralized word

    # 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) click to toggle source

@api private

Remove trailing zeros from a string.

@param [String] string string with trailing zeros @return [String] string with trailing zeros removed

    # File lib/rspec/core/formatters/helpers.rb, line 91
    def strip_trailing_zeroes(string)
      stripped = string.sub(/[^1-9]+$/, '')
      stripped.empty? ? "0" : stripped
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.