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

Fix a regression in our metadata backwards compatibility #1490

Merged
merged 2 commits into from
Apr 18, 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
15 changes: 12 additions & 3 deletions lib/rspec/core/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ def ensure_valid_user_keys
class ExampleHash < HashPopulator
def self.create(group_metadata, user_metadata, description, block)
example_metadata = group_metadata.dup
group_metadata = Hash.new(&ExampleGroupHash.backwards_compatibility_default_proc do |hash|
hash[:parent_example_group]
end)
group_metadata.update(example_metadata)

example_metadata[:example_group] = group_metadata
example_metadata.delete(:parent_example_group)

Expand Down Expand Up @@ -184,14 +189,18 @@ def self.create(parent_group_metadata, user_metadata, *args, &block)
end

def self.hash_with_backwards_compatibility_default_proc
Hash.new do |hash, key|
Hash.new(&backwards_compatibility_default_proc { |hash| hash })
end

def self.backwards_compatibility_default_proc(&example_group_selector)
Proc.new do |hash, key|
case key
when :example_group
RSpec.deprecate("The `:example_group` key in an example group's metadata hash",
:replacement => "the example group's hash directly for the " +
"computed keys and `:parent_example_group` to access the parent " +
"example group metadata")
LegacyExampleGroupHash.new(hash)
LegacyExampleGroupHash.new(example_group_selector.call(hash))
when :example_group_block
RSpec.deprecate("`metadata[:example_group_block]`",
:replacement => "`metadata[:block]`")
Expand Down Expand Up @@ -370,7 +379,7 @@ class LegacyExampleGroupHash

def initialize(metadata)
@metadata = metadata
self[:example_group] = metadata[:parent_example_group]
self[:example_group] = metadata[:parent_example_group][:example_group]
end

def to_h
Expand Down
21 changes: 20 additions & 1 deletion spec/rspec/core/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def metadata_for(*args)
end

a[:description] = "new description"

pending "Cannot maintain this and provide full `:example_group` backwards compatibility (see GH #1490):("
expect(b[:description]).to eq("new description")
end

Expand Down Expand Up @@ -460,13 +462,30 @@ def value_for(*args)
describe("nested") { child = metadata }
end

expect(child[:example_group][:example_group]).to include(
expect(child[:example_group][:example_group].to_h).to include(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, is this not a hash already?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. It's a LegacyExampleGroupHash instance.

:foo => 3,
:description => "Object group",
:line_number => parent_line
)
end

it "works properly with deep nesting" do
inner_metadata = nil

RSpec.describe "Level 1" do
describe "Level 2" do
describe "Level 3" do
inner_metadata = example("Level 4").metadata
end
end
end

expect(inner_metadata[:description]).to eq("Level 4")
expect(inner_metadata[:example_group][:description]).to eq("Level 3")
expect(inner_metadata[:example_group][:example_group][:description]).to eq("Level 2")
expect(inner_metadata[:example_group][:example_group][:example_group][:description]).to eq("Level 1")
end

it 'can mutate attributes when accessing them via [:example_group]' do
meta = nil

Expand Down