模块: RSpec::Core::Formatters::Helpers

定义于
lib/rspec/core/formatters/helpers.rb

概述

格式化程序辅助函数。

类方法摘要 折叠

类方法详情

.format_duration(duration) ⇒String

此方法是私有 API 的一部分。 应尽可能避免使用此方法,因为它可能会在将来被删除或更改。

将秒数格式化为人类可读的字符串。

示例

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

参数

  • duration (Float, Fixnum)

    以秒为单位

返回值

  • (String)

    人类可读的时间

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

此方法是私有 API 的一部分。 应尽可能避免使用此方法,因为它可能会在将来被删除或更改。

将秒数格式化为 5 位小数精度,如果数字小于 1 则移除尾随零,如果数字大于零则格式化为 2 位小数精度。

使用的精度在 SUB_SECOND_PRECISIONDEFAULT_PRECISION 中设置。

示例

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

参数

  • float (Float)

返回值

  • (String)

    格式化的浮点数

另请参见

  • #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

此方法是私有 API 的一部分。 应尽可能避免使用此方法,因为它可能会在将来被删除或更改。

给定一个示例 ID 列表,将其组织成一个紧凑、有序的列表。

102
103
104
105
106
107
108
109
110
111
112
113
114
# File '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

此方法是私有 API 的一部分。 应尽可能避免使用此方法,因为它可能会在将来被删除或更改。

根据计数对单词进行复数化。

参数

  • count (Fixnum)

    对象数量

  • string (String)

    要进行复数化的单词

返回值

  • (String)

    复数化的单词

88
89
90
91
92
93
94
95
96
97
98
# File '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