模块:RSpec::Matchers::Composable
- 定义在
- lib/rspec/matchers/composable.rb
概述
旨在支持 RSpec 3+ 的可组合匹配器功能的 Mixin。将它混合到您的自定义匹配器类中,以便它们可以以可组合的方式使用。
定义在命名空间下
类方法摘要 折叠
-
.should_enumerate?(item) ⇒ Boolean private
只要数组不是递归的,我们就会枚举它们。
-
.surface_descriptions_in(item) ⇒ Object
将给定数据结构(通常是哈希或数组)转换为新的数据结构,当在该结构上调用
#inspect
时,将提供任何包含匹配器的描述,而不是正常的#inspect
输出。 - .unreadable_io?(object) ⇒ Boolean private
实例方法摘要 折叠
-
#===(value) ⇒ Object
委托给
#matches?
。 -
#and(matcher) ⇒ Object (also: #&)
创建一个复合
and
期望。 -
#description_of(object) ⇒ Object
以一种了解复合匹配器的方式返回给定对象的描述。
-
#or(matcher) ⇒ Object (also: #|)
创建一个复合
or
期望。 -
#values_match?(expected, actual) ⇒ Boolean
这提供了一种通用方法,用于将预期值与实际值模糊匹配。
类方法详细信息
.should_enumerate?(item) ⇒Boolean
此方法是私有 API 的一部分。 应尽可能避免使用此方法,因为它可能会在将来被删除或更改。
只要数组不是递归的,我们就会枚举它们。
142 143 144 |
# File 'lib/rspec/matchers/composable.rb', line 142 def should_enumerate?(item) Array === item && item.none? { |subitem| subitem.equal?(item) } end |
.surface_descriptions_in(item) ⇒Object
将给定数据结构(通常是哈希或数组)转换为新的数据结构,当在该结构上调用 #inspect
时,将提供任何包含匹配器的描述,而不是正常的 #inspect
输出。
如果您支持包含匹配器的任何参数,鼓励您在自定义匹配器的 description
、failure_message
或 failure_message_when_negated
实现中使用它。
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rspec/matchers/composable.rb', line 98 def surface_descriptions_in(item) if Matchers.is_a_describable_matcher?(item) DescribableItem.new(item) elsif Hash === item Hash[surface_descriptions_in(item.to_a)] elsif Struct === item || unreadable_io?(item) RSpec::Support::ObjectFormatter.format(item) elsif should_enumerate?(item) item.map { |subitem| surface_descriptions_in(subitem) } else item end end |
.unreadable_io?(object) ⇒Boolean
此方法是私有 API 的一部分。 应尽可能避免使用此方法,因为它可能会在将来被删除或更改。
147 148 149 150 151 152 153 |
# File 'lib/rspec/matchers/composable.rb', line 147 def unreadable_io?(object) return false unless IO === object object.each {} # STDOUT is enumerable but raises an error false rescue IOError true end |
实例方法详细信息
#===(value) ⇒Object
委托给 #matches?
。允许匹配器以可组合的方式使用,并且还支持在 case 语句中使用匹配器。
45 46 47 |
# File 'lib/rspec/matchers/composable.rb', line 45 def ===(value) matches?(value) end |
#and(matcher) ⇒Object 也称为: &
目前不支持否定形式(expect(...).not_to matcher.and other
)。
创建一个复合 and
期望。只有当两个子匹配器都通过时,该匹配器才会通过。这可以链接在一起,形成任意长的匹配器链。
22 23 24 |
# File 'lib/rspec/matchers/composable.rb', line 22 def and(matcher) BuiltIn::Compound::And.new self, matcher end |
#description_of(object) ⇒Object
以一种了解复合匹配器的方式返回给定对象的描述。如果对象是带有 description
方法的匹配器,则返回描述;否则返回 object.inspect
。
如果您支持匹配器参数,鼓励您在自定义匹配器的 description
、failure_message
或 failure_message_when_negated
实现中使用它。
82 83 84 |
# File 'lib/rspec/matchers/composable.rb', line 82 def description_of(object) RSpec::Support::ObjectFormatter.format(object) end |
#or(matcher) ⇒Object 也称为: |
目前不支持否定形式(expect(...).not_to matcher.or other
)。
创建一个复合 or
期望。如果任何子匹配器通过,该匹配器将通过。这可以链接在一起,形成任意长的匹配器链。
38 39 40 |
# File 'lib/rspec/matchers/composable.rb', line 38 def or(matcher) BuiltIn::Compound::Or.new self, matcher end |
#values_match?(expected, actual) ⇒Boolean
这提供了一种通用方法,用于将预期值与实际值模糊匹配。它了解嵌套的数据结构(例如哈希和数组),并且能够匹配在任何嵌套级别用作预期值的或预期值内的匹配器。
在自定义匹配器中,鼓励您在您的匹配器需要匹配两个值时使用它,除非它需要更精确的语义。例如,eq
匹配器不使用此方法,因为它旨在使用 ==
(以及仅 ==
)进行匹配。
66 67 68 69 |
# File 'lib/rspec/matchers/composable.rb', line 66 def values_match?(expected, actual) expected = with_matchers_cloned(expected) Support::FuzzyMatcher.values_match?(expected, actual) end |