模块:RSpec::Core::Metadata

定义于
lib/rspec/core/metadata.rb

概述

每个 ExampleGroup 类和 Example 实例都拥有一个 Metadata 实例,它是一个 Hash,扩展为支持对与键关联的值的延迟评估,这些键可能被任何示例或组使用,也可能不被使用。

除了内部使用的元数据之外,它还存储用户提供的元数据,例如:

RSpec.describe Something, :type => :ui do
  it "does something", :slow => true do
    # ...
  end
end

:type => :ui 存储在示例组拥有的 Metadata 中,而 :slow => true 存储在示例拥有的 Metadata 中。然后可以使用这些元数据来选择使用命令行上的 --tag 选项运行哪些示例,或者使用 Configuration 上的几种方法来过滤运行(例如 filter_run_includingfilter_run_excluding 等)。

类方法摘要 折叠

类方法详情

.relative_path(line) ⇒String

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

返回相对于行的位置。

参数

  • line (String)

    当前代码行

返回值

  • (String)

    相对于行的位置

44
45
46
47
48
49
50
51
52
53
# File 'lib/rspec/core/metadata.rb', line 44
def self.relative_path(line)
  line = line.sub(relative_path_regex, "\\1.\\2".freeze)
  line = line.sub(/\A([^:]+:\d+)$/, '\\1'.freeze)
  return nil if line == '-e:1'.freeze
  line
rescue SecurityError
  # :nocov: - SecurityError is no longer produced starting in ruby 2.7
  nil
  # :nocov:
end

.relative_path_regexvoid

匹配字符串,字符串要么在输入的开头,要么以空格开头,包含当前路径,要么以分隔符结尾,要么在字符串的末尾。匹配组是字符串之前和字符串之后的字符(如果有)。

http://rubular.com/r/fT0gmX6VJX http://rubular.com/r/duOrD4i3wb http://rubular.com/r/sbAMHFrOx1

36
37
38
# File 'lib/rspec/core/metadata.rb', line 36
def self.relative_path_regex
  @relative_path_regex ||= /(\A|\s)#{File.expand_path('.')}(#{File::SEPARATOR}|\s|\Z)/
end