Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Fix all apply #1796

Merged
merged 4 commits into from
Dec 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Bug Fixes:
(Myron Marston, #1771)
* Don't consider expectations from `after` hooks when generating
example descriptions. (Myron Marston, #1771)
* Don't apply metadata-filtered config hooks to examples in groups
with matching metadata when those example override the parent
metadata value to not match. (Myron Marston, #1796)

### 3.1.8 Development

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Feature: configure expectation framework
When I run `rspec example_spec.rb`
Then the examples should all pass

Scenario: Configure rspec/expecations AND minitest assertions
Scenario: Configure rspec/expectations AND minitest assertions
Given a file named "example_spec.rb" with:
"""ruby
RSpec.configure do |config|
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ def extend(mod, *filters)
# `extend`.
def configure_group(group)
include_or_extend_modules.each do |include_or_extend, mod, filters|
next unless filters.empty? || group.any_apply?(filters)
next unless filters.empty? || group.apply?(:any?, filters)
__send__("safe_#{include_or_extend}", mod, group)
end
end
Expand Down Expand Up @@ -1359,7 +1359,7 @@ def define_derived_metadata(*filters, &block)
# @private
def apply_derived_metadata_to(metadata)
@derived_metadata_blocks.each do |filter, block|
block.call(metadata) if filter.empty? || MetadataFilter.any_apply?(filter, metadata)
block.call(metadata) if filter.empty? || MetadataFilter.apply?(:any?, filter, metadata)
end
end

Expand Down
9 changes: 2 additions & 7 deletions lib/rspec/core/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,8 @@ def inspect
end

# @private
def any_apply?(filters)
MetadataFilter.any_apply?(filters, metadata)
end

# @private
def all_apply?(filters)
MetadataFilter.all_apply?(filters, metadata) || @example_group_class.all_apply?(filters)
def apply?(predicate, filters)
MetadataFilter.apply?(predicate, filters, metadata)
end

# @private
Expand Down
9 changes: 2 additions & 7 deletions lib/rspec/core/example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,8 @@ def self.fail_fast?
end

# @private
def self.any_apply?(filters)
MetadataFilter.any_apply?(filters, metadata)
end

# @private
def self.all_apply?(filters)
MetadataFilter.all_apply?(filters, metadata)
def self.apply?(predicate, filters)
MetadataFilter.apply?(predicate, filters, metadata)
end

# @private
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/core/filter_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def use(*args)
end

def include_example?(example)
@rules.empty? ? true : example.any_apply?(@rules)
@rules.empty? ? true : example.apply?(:any?, @rules)
end

def standalone?
Expand Down Expand Up @@ -252,7 +252,7 @@ class ExclusionRules < FilterRules
}.freeze

def include_example?(example)
example.any_apply?(@rules) || example.any_apply?(CONDITIONAL_FILTERS)
example.apply?(:any?, @rules) || example.apply?(:any?, CONDITIONAL_FILTERS)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def initialize(block, options)
end

def options_apply?(example_or_group)
example_or_group.all_apply?(options)
example_or_group.apply?(:all?, options)
end
end

Expand Down
9 changes: 2 additions & 7 deletions lib/rspec/core/metadata_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ module Core
module MetadataFilter
class << self
# @private
def any_apply?(filters, metadata)
filters.any? { |k, v| filter_applies?(k, v, metadata) }
end

# @private
def all_apply?(filters, metadata)
filters.all? { |k, v| filter_applies?(k, v, metadata) }
def apply?(predicate, filters, metadata)
filters.__send__(predicate) { |k, v| filter_applies?(k, v, metadata) }
end

# @private
Expand Down
13 changes: 13 additions & 0 deletions spec/rspec/core/filter_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ def example_with(*args)
filter_manager.include_with_low_priority :foo => :bar
expect(filter_manager.prune([included, excluded])).to eq([included])
end

context "with multiple inclusion filters" do
it 'includes objects that match any of them' do
examples = [
included_1 = example_with(:foo => true),
included_2 = example_with(:bar => true),
example_with(:bazz => true)
]

filter_manager.include :foo => true, :bar => true
expect(filter_manager.prune(examples)).to contain_exactly(included_1, included_2)
end
end
end

describe "#inclusions#description" do
Expand Down
43 changes: 34 additions & 9 deletions spec/rspec/core/hooks_filtering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,43 @@ def filters
end

it "does not run if some hook filters don't match the group's filters" do
sequence = []

RSpec.configure do |c|
c.before(:all, :one => 1, :four => 4) { sequence << "before all in config"}
c.around(:each, :two => 2, :four => 4) {|example| sequence << "around each in config"; example.run}
c.before(:each, :one => 1, :two => 2, :four => 4) { sequence << "before each in config"}
c.after(:each, :one => 1, :two => 2, :three => 3, :four => 4) { sequence << "after each in config"}
c.after(:all, :one => 1, :three => 3, :four => 4) { sequence << "after all in config"}
end

RSpec.describe "group", :one => 1, :two => 2, :three => 3 do
example("ex1") { sequence << "ex1" }
example("ex2", :four => 4) { sequence << "ex2" }
end.run

expect(sequence).to eq([
"ex1",
"around each in config",
"before each in config",
"ex2",
"after each in config",
])
end

it "does not run for examples that do not match, even if their group matches" do
filters = []

RSpec.configure do |c|
c.before(:all, :one => 1, :four => 4) { filters << "before all in config"}
c.around(:each, :two => 2, :four => 4) {|example| filters << "around each in config"; example.run}
c.before(:each, :one => 1, :two => 2, :four => 4) { filters << "before each in config"}
c.after(:each, :one => 1, :two => 2, :three => 3, :four => 4) { filters << "after each in config"}
c.after(:all, :one => 1, :three => 3, :four => 4) { filters << "after all in config"}
c.before(:each, :apply_it) { filters << :before_each }
end
group = ExampleGroup.describe(:one => 1, :two => 2, :three => 3)
group.example("example") {}
group.run
expect(filters).to eq([])

RSpec.describe "Group", :apply_it do
example("ex1") { filters << :matching_example }
example("ex2", :apply_it => false) { filters << :nonmatching_example }
end.run

expect(filters).to eq([:before_each, :matching_example, :nonmatching_example])
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/rspec/core/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,8 @@ def value_for(*args)
line = __LINE__ + 1
RSpec.describe("group") { meta = metadata }

applies = MetadataFilter.any_apply?(
applies = MetadataFilter.apply?(
:any?,
{ :example_group => { :line_number => line } },
meta
)
Expand Down